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


Groups > comp.lang.python > #100996

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

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: Is it safe to assume floats always have a 53-bit mantissa?
Date 2015-12-30 16:19 +0200
Organization A noiseless patient Spider
Message-ID <87h9j03tot.fsf@elektro.pacujo.net> (permalink)
References <5683d9aa$0$1618$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


Steven D'Aprano <steve@pearwood.info>:

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

You'd need to have it in writing, wouldn't you?

The only spec I know of promises no such thing:

   Floating point numbers are usually implemented using double in C;
   information about the precision and internal representation of
   floating point numbers for the machine on which your program is
   running is available in sys.float_info.

   <URL: https://docs.python.org/3/library/stdtypes.html#typesnumeric>

> As an optimization, I want to write:
>
> def func(n):
>     if n <= 2**53:
>         # use the floating point fast implementation
>     else:
>         # fall back on the slower, but exact, int algorithm
>
> [...]
>
> But I wonder whether I need to write this instead?
>
> def func(n):
>     if n <= 2**sys.float_info.mant_dig:
>         # ...float
>     else:
>         # ...int
>
> I don't suppose it really makes any difference performance-wise, but I
> can't help but wonder if it is really necessary. If
> sys.float_info.mant_dig is guaranteed to always be 53, why not just
> write 53?

Mainly because

   2**sys.float_info.mant_dig

looks much better than

   2**53


Marko

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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