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


Groups > comp.lang.python > #26227

Re: Extracting bit fields from an IEEE-784 float

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Extracting bit fields from an IEEE-784 float
Date 2012-07-30 01:17 -0400
References <5015d8d4$0$11120$c3e8da3@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.2712.1343625462.4697.python-list@python.org> (permalink)

Show all headers | View raw


On 7/29/2012 8:44 PM, Steven D'Aprano wrote:
> 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?

CPython floats are C doubles, which should be IEEE doubles. Other 
implementations have a different to probably the same thing.

> 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?

The math modules functions to disassemble floats will not care.

-- 
Terry Jan Reedy


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