Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26232

Re: Extracting bit fields from an IEEE-784 float

From Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Newsgroups comp.lang.python
Subject Re: Extracting bit fields from an IEEE-784 float
Date 2012-07-30 08:42 +0200
Message-ID <i0nhe9-9du.ln1@satorlaser.homedns.org> (permalink)
References <5015d8d4$0$11120$c3e8da3@news.astraweb.com>

Show all headers | View raw


Am 30.07.2012 02:44, schrieb Steven D'Aprano:
> I wish to extract the bit fields from a Python float, call it x. First I
> cast the float to 8-bytes:
>
> s = struct.pack('=d', x)
> i = struct.unpack('=q', s)[0]
>
> Then I extract the bit fields from the int, e.g. to grab the sign bit:
>
> (i & 0x8000000000000000) >> 63
>
>
> Questions:
>
> 1) Are there any known implementations or platforms where Python floats
> are not C doubles? If so, what are they?

The struct docs refer to C's double type, so it depends on that type 
probably. However, regardless of C's double type, the same docs refer to 
the IEEE form when packed into a byte array. Is it just the 
representation you are after or some specific behaviour?


> 2) If the platform byte-order is reversed, do I need to take any special
> action? I don't think I do, because even though the float is reversed, so
> will be the bit mask. Is this correct?

Yes, the code is fine. If you have doubts, I have a big-endian system at 
home (Linux/PowerPC) where I could run tests.


> 3) Any other problems with the way I am doing this?

Python docs refer to IEEE-754, not 784? Typo?


Uli

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Extracting bit fields from an IEEE-784 float Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-30 00:44 +0000
  PyPI question, or, maybe I'm just stupid Chris Gonnerman <chris@gonnerman.org> - 2012-07-29 20:26 -0500
    Re: PyPI question, or, maybe I'm just stupid Ben Finney <ben+python@benfinney.id.au> - 2012-07-30 14:00 +1000
      Re: [Python] Re: PyPI question, or, maybe I'm just stupid Chris Gonnerman <chris@gonnerman.org> - 2012-07-30 07:46 -0500
  Re: Extracting bit fields from an IEEE-784 float Dan Sommers <dan@tombstonezero.net> - 2012-07-29 18:08 -0700
  Re: Extracting bit fields from an IEEE-784 float Terry Reedy <tjreedy@udel.edu> - 2012-07-30 01:17 -0400
  Re: Extracting bit fields from an IEEE-784 float Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-07-30 08:42 +0200
  Re: Extracting bit fields from an IEEE-784 float Mark Dickinson <dickinsm@gmail.com> - 2012-07-30 00:57 -0700
  Re: PyPI question, or, maybe I'm just stupid Dieter Maurer <dieter@handshake.de> - 2012-07-30 11:20 +0200
  Re: Extracting bit fields from an IEEE-784 float Grant Edwards <invalid@invalid.invalid> - 2012-07-30 14:16 +0000
    Re: Extracting bit fields from an IEEE-784 float Roy Smith <roy@panix.com> - 2012-07-30 10:28 -0400
    Re: Extracting bit fields from an IEEE-784 float Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-07-30 15:50 +0100
      Re: Extracting bit fields from an IEEE-784 float Grant Edwards <invalid@invalid.invalid> - 2012-07-30 15:47 +0000
        Re: Extracting bit fields from an IEEE-784 float Roy Smith <roy@panix.com> - 2012-07-30 16:50 -0400
    Re: Extracting bit fields from an IEEE-784 float Mark Dickinson <dickinsm@gmail.com> - 2012-07-30 09:14 -0700

csiph-web