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


Groups > comp.lang.python > #100998

Re: Is it safe to assume floats always have a 53-bit mantissa?

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: Is it safe to assume floats always have a 53-bit mantissa?
Date Wed, 30 Dec 2015 09:35:29 -0500
Lines 67
Message-ID <mailman.75.1451486141.11925.python-list@python.org> (permalink)
References <5683d9aa$0$1618$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de mFH5yEc48qzw2mHyfVA9JgjbO6VX1mOFWapVUQk8MA/g==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'float': 0.05; 'converts': 0.07; 'only,': 0.07; 'defined.': 0.09; 'doubles': 0.09; 'int.': 0.09; 'processing,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'assume': 0.11; 'jan': 0.11; 'def': 0.13; 'doubles.': 0.16; 'naming': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'released.': 0.16; 'subject:bit': 0.16; 'wrote:': 0.16; 'integer': 0.18; '(in': 0.18; 'algorithm': 0.20; '(the': 0.22; '64-bit': 0.22; 'constant': 0.22; 'am,': 0.23; 'bit': 0.23; '(or': 0.23; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'sense': 0.26; 'equivalent': 0.27; 'wonder': 0.27; 'function': 0.28; 'fine': 0.28; 'allows': 0.30; 'code': 0.30; 'computing': 0.32; 'point': 0.33; "d'aprano": 0.33; 'int': 0.33; 'steven': 0.33; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:71': 0.36; 'smaller': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'itself': 0.38; 'version': 0.38; 'does': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'easy': 0.60; 'documents': 0.61; 'real': 0.62; 'back': 0.62; 'yes': 0.62; 'skip:n 10': 0.62; '500': 0.63; 'safe': 0.63; 'times': 0.63; 'strictly': 0.64; 'believe': 0.66; 'fall': 0.66; 'promise': 0.66; 'subject:have': 0.80; 'actually,': 0.84; 'float,': 0.84; 'maths': 0.84; 'once).': 0.84; 'subject:always': 0.84; 'exposing': 0.91; 'received:fios.verizon.net': 0.91; 'write:': 0.91; 'divided': 0.93
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host pool-71-185-227-36.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0
In-Reply-To <5683d9aa$0$1618$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100998

Show key headers only | View raw


On 12/30/2015 8:18 AM, Steven D'Aprano wrote:
> We know that Python floats are equivalent to C doubles,

Yes

> which are 64-bit IEEE-754 floating point numbers.

I believe that this was not true on all systems when Python was first 
released.  Not all 64-bit floats divided them the same way.  I believe 
there has been some discussion on pydev whether the python code itself 
should assume IEEE now.  I do not believe that there are currently any 
buildbots that are not IEEE.  Does the standard allow exposing the 80 
bit floats of FP processors?

> Well, actually, C doubles are not strictly defined. The only promise the C
> standard makes is that double is no smaller than float. (That's C float,
> not Python float.) And of course, not all Python implementations use C.
>
> Nevertheless, it's well known (in the sense that "everybody knows") that
> Python floats are equivalent to C 64-bit IEEE-754 doubles. How safe is that
> assumption?
>
> I have a function with two implementations: a fast implementation that
> converts an int to a float, does some processing, then converts it back to
> int. That works fine so long as the int can be represented exactly as a
> float.
>
> The other implementation uses integer maths only, and is much slower but
> exact.
>
> As an optimization, I want to write:
>
>
> def func(n):
>      if n <= 2**53:

The magic number 53 should be explained in the code.

>          # use the floating point fast implementation
>      else:
>          # fall back on the slower, but exact, int algorithm
>
>
> (The optimization makes a real difference: for large n, the float version is
> about 500 times faster.)
>
> But I wonder whether I need to write this instead?
>
> def func(n):
>      if n <= 2**sys.float_info.mant_dig:
>          # ...float
>      else:
>          # ...int

Pull the calculation of the constant out of the function.  Naming the 
constant documents it and allows easy change.  There is pretty standard 
in scientific computing (or was once).

finmax = 2 ** sys.float_info.mant_dig  # -1?

def func(n):
     if n <= finmax:
         ...

-- 
Terry Jan Reedy

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


Thread

Is it safe to assume floats always have a 53-bit mantissa? Steven D'Aprano <steve@pearwood.info> - 2015-12-31 00:18 +1100
  Re: Is it safe to assume floats always have a 53-bit mantissa? Marko Rauhamaa <marko@pacujo.net> - 2015-12-30 16:19 +0200
  Re: Is it safe to assume floats always have a 53-bit mantissa? Terry Reedy <tjreedy@udel.edu> - 2015-12-30 09:35 -0500

csiph-web