Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'float': 0.05; 'python': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:fields': 0.09; 'terry': 0.09; 'do,': 0.15; 'doubles.': 0.16; 'message-id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:bit': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'platforms': 0.18; 'math': 0.20; 'bit': 0.21; 'int,': 0.22; 'questions:': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; "d'aprano": 0.29; 'steven': 0.29; 'probably': 0.29; 'e.g.': 0.30; 'extract': 0.33; 'to:addr:python-list': 0.33; 'so,': 0.35; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'modules': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'subject:-': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'first': 0.61; 'different': 0.63; 'grab': 0.64; 'cast': 0.65; 'wish': 0.70; 'special': 0.73; 'float,': 0.84; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Extracting bit fields from an IEEE-784 float Date: Mon, 30 Jul 2012 01:17:22 -0400 References: <5015d8d4$0$11120$c3e8da3@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 In-Reply-To: <5015d8d4$0$11120$c3e8da3@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343625462 news.xs4all.nl 6892 [2001:888:2000:d::a6]:41957 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26227 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