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?

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
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 Wed, 30 Dec 2015 16:19:30 +0200
Organization A noiseless patient Spider
Lines 50
Message-ID <87h9j03tot.fsf@elektro.pacujo.net> (permalink)
References <5683d9aa$0$1618$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx02.eternal-september.org; posting-host="b7cb1518d23ec19d482dcc9c31d30fdd"; logging-data="6998"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19J08xKc9wJJwpJQKodhKSh"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:jfhj8PIATE1nbHOj0S4Vj9qaLAo= sha1:QyRfeF9Eb7ugLB/wtvpo65XxoqU=
Xref csiph.com comp.lang.python:100996

Show key headers only | 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