extracting hex mant+exp from double?
Author Message
extracting hex mant+exp from double?

python docs say floats are all doubles...which usually (i think always) is the
IEEE spec for 8-byte float.  are there any routines out there to extract the
binary mantissa and exponent...or better hexadecimal mantissa and exponent out
of the float?  i want to do some statistics but don't want to use decimal.  i
know you can access the 8 bytes with struct lib...then do some bit manipulations
to do the trick.  has anybody done this already?

Thu, 27 Nov 2003 09:18:15 GMT
extracting hex mant+exp from double?
[thinkit]

Quote:
> python docs say floats are all doubles...which usually (i think
> always) is the IEEE spec for 8-byte float.  are there any routines
> out there to extract the binary mantissa and exponent...or better
> hexadecimal mantissa and exponent out of the float?  i want to do
> some statistics but don't want to use decimal.  i know you can
> access the 8 bytes with struct lib...then do some bit manipulations
> to do the trick.  has anybody done this already?

Sorry, it's unclear what you want.  How about you pretend the routine you
want already existed, and give us a specific example showing exactly what it
is you want this routine to accept as argument(s) and return as result(s)?

It's not even clear to me that you really want to pick at the bits.  Have
you tried, for example, math.frexp?

Quote:
>>> import math
>>> math.frexp(1.0)
(0.5, 1)
>>> math.frexp(2.**100)
(0.5, 101)
>>> math.frexp(.125)
(0.5, -2)

That picks apart the mantissa and exponent in a useful way.  If you then
want, say, the mantissa as a 53-bit integer shown as a hex string,

Quote:
>>> math.frexp(.1)

(0.80000000000000004, -3)
Quote:
>>> m = _[0]
>>> m
0.80000000000000004
>>> m * 2L**53
7205759403792794.0
>>> long(_)
7205759403792794L
>>> hex(_)
'0x1999999999999AL'

Thu, 27 Nov 2003 12:12:42 GMT
extracting hex mant+exp from double?
[Mike C. Fletcher, after staring at
>>> math.frexp(.1)
(0.80000000000000004, -3)
>>> m = _[0]
]

Quote:
> It's a bleeding anonymous magic variable man!  Who let that into the
> language!  The variable is _magic_ I say.  Wizardry! Sorcery!  Appearing
> without explicit assignment!
> Pact-with-the-devil-and-dancing-at-midnight-curses-and-hexes!
> Spawn-of-PERL!
> Magic, I say!

> [I spent 2 minutes trying to figure out why you were translating 0, what
> language you were using (base-2-eese?), and what special meaning 0 had
> for translation tables.  How did I miss something so... icky...

> Ick,
> Mike

Hunting this down in CVS, Guido added it in ceval.c, Revision 2.64.2.6, on
Thu Apr 28 12:51:01 1994 UTC.  I agree it's *so* un-Pythonic that Python
really wouldn't be Python without it <wink>.

practicality-beats-purity-ly y'rs  - tim

Thu, 27 Nov 2003 13:49:02 GMT
extracting hex mant+exp from double?
It's a bleeding anonymous magic variable man!  Who let that into the
language!  The variable is _magic_ I say.  Wizardry! Sorcery!  Appearing
without explicit assignment!
Pact-with-the-devil-and-dancing-at-midnight-curses-and-hexes! Spawn-of-PERL!
Magic, I say!

[I spent 2 minutes trying to figure out why you were translating 0, what
language you were using (base-2-eese?), and what special meaning 0 had for
translation tables.  How did I miss something so... icky... getting added?]

Ick,
Mike

Quote:
-----Original Message-----

Sent: June 10, 2001 00:13

Subject: RE: extracting hex mant+exp from double?
...
>>> math.frexp(.1)
(0.80000000000000004, -3)
>>> m = _[0]
...

Thu, 27 Nov 2003 13:34:03 GMT
extracting hex mant+exp from double?
Has anyone ever used it since it was slipped in (save you, in this one
case)?  I must be suspicious when a time machine is{*filter*} around.  I've
never noticed code using it (that I can recall).

"practicality-beats-purity-ly y'rs" how does it apply?  I mean, I can see
maybe in interactive environments you might get cases where you forgot to
put x = in front of the last value and could save a few seconds
recalculation, but otherwise it just seems like pointless magic.  Oh well,
once the time machine's been used I suppose the decision's done.  I'll just
console my offended sensibilities with the hope that no-one actually uses
it.

Of course, trying to strip it out without access to the time machine would
be impractical, so I suppose that could be the practicality argument...  or

Ick still, even if it were hidden in there the whole time and not just more
Time Machine fiddling,
Mike

Quote:
-----Original Message-----

Sent: June 10, 2001 01:49

Subject: RE: extracting hex mant+exp from double?
...
> Ick,
> Mike

Hunting this down in CVS, Guido added it in ceval.c, Revision 2.64.2.6, on
Thu Apr 28 12:51:01 1994 UTC.  I agree it's *so* un-Pythonic that Python
really wouldn't be Python without it <wink>.

practicality-beats-purity-ly y'rs  - tim

--
http://www.*-*-*.com/

Thu, 27 Nov 2003 14:15:22 GMT
extracting hex mant+exp from double?
[Mike C. Fletcher, on "_"]

Quote:
> Has anyone ever used it since it was slipped in (save you, in this one
> case)?

I expect it's used heavily.  For example, the first complaint of people who
use sys.displayhook is "hey! '_' doesn't work anymore!" (setting
__builtin__._ is done by the default display hook, so if you provide your
own display hook you have to do that yourself -- if you want it).

Quote:
> I must be suspicious when a time machine is{*filter*} around.  I've
> never noticed code using it (that I can recall).

You'll never see a program using it.  There's nothing special about "_" in
batch code (it's just another identifier there), it only has this meaning in
an interactive shell (specifically, the display hook is called only from the
body of the PRINT_EXPR opcode, which opcode is generated only in response to
interactive input).

Quote:
> "practicality-beats-purity-ly y'rs" how does it apply?  I mean, I can
> see maybe in interactive environments you might get cases where you
> forgot to put x = in front of the last value and could save a few
> seconds recalculation,

Bingo, and remember that not all shells support history -- reproducing the
last line typed can be a real pain in those.  Some expressions have
side-effects too.  In addition, in an interactive shell you often don't
*know* that you're going to want to do something to the last result until
you see its value, and then

Quote:
>>> x = whatever

doesn't display the value.

Quote:
> but otherwise it just seems like pointless magic.

There is no otherwise -- it's an interactive gimmick, and that's all.  You
should try it!  It's handy.

live-and-let-underscore-ly y'rs  - tim

Thu, 27 Nov 2003 14:50:29 GMT
extracting hex mant+exp from double?

Quote:
> It's a bleeding anonymous magic variable man!  Who let that into the
> language!  The variable is _magic_ I say.  Wizardry! Sorcery!  Appearing
> without explicit assignment!

But it's not in the language, only in the interactive interpreter.

--
Remco Gerlich

Thu, 27 Nov 2003 18:43:01 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages