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


Groups > comp.lang.python > #26222

Re: Extracting bit fields from an IEEE-784 float

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <dan@tombstonezero.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'float': 0.05; 'python': 0.09; 'subject:fields': 0.09; 'cc:addr:python-list': 0.10; 'subject:bit': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.17; 'math': 0.20; 'bit': 0.21; 'import': 0.21; 'int,': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'this?': 0.28; "d'aprano": 0.29; 'dan': 0.29; 'steven': 0.29; 'no,': 0.29; 'e.g.': 0.30; 'extract': 0.33; 'doing': 0.35; 'there': 0.35; 'but': 0.36; 'charset:us-ascii': 0.36; 'problems': 0.36; 'subject:: ': 0.38; 'subject:-': 0.40; 'content-disposition:inline': 0.60; 'first': 0.61; 'more': 0.63; 'grab': 0.64; 'cast': 0.65; 'wish': 0.70; 'float,': 0.84
DKIM-Signature v=1; a=rsa-sha1; c=relaxed; d=tombstonezero.net; h=date:from:to:cc:subject:message-id:references:mime-version:content-type:in-reply-to; s=key1; bh=MFpHTiCZVCdwxrCxwp80lfLXSVI=; b=SCWPlrPp5ifUKz+8S/qyy6XmY2FTT5DzpfuSGGTc/iGdOjiIuHFlBf3rouEGxD9HFeBUIHpVqQAWgvDPjOH8OQ==
DomainKey-Signature a=rsa-sha1; c=nofws; d=tombstonezero.net; q=dns; s=key1; b=m2pQLxWPRLxr+57ET0iwllWtoKfFmLjwCSqRbWP0YqVxn+bKRckgcnLdjy4bDdqCjsXJ5ljHwyVh97Y4XpoYEw==
X-Virus-Scanned Debian amavisd-new at 5013.mail.vm.your-site.com
Date Sun, 29 Jul 2012 18:08:14 -0700
From Dan Sommers <dan@tombstonezero.net>
To Steven D'Aprano <steve+comp.lang.python@pearwood.info>
Subject Re: Extracting bit fields from an IEEE-784 float
References <5015d8d4$0$11120$c3e8da3@news.astraweb.com>
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <5015d8d4$0$11120$c3e8da3@news.astraweb.com>
User-Agent Mutt/1.5.21 (2010-09-15)
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2709.1343611680.4697.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1343611680 news.xs4all.nl 6903 [2001:888:2000:d::a6]:40851
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:26222

Show key headers only | View raw


On 2012-07-30 at 00:44:04 +0000,
Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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

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

No, but perhaps this would be clearer:

    import math
    sign = math.copysign(1.0, x)

There are solutions that use math.frexp, too, but IMO they're more
obtuse.

HTH,
Dan

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