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


Groups > comp.lang.python > #29664

Re: Exact integer-valued floats

From Jussi Piitulainen <jpiitula@ling.helsinki.fi>
Newsgroups comp.lang.python
Subject Re: Exact integer-valued floats
Date 2012-09-21 22:47 +0300
Organization University of Helsinki
Message-ID <qot8vc3jgzo.fsf@ruuvi.it.helsinki.fi> (permalink)
References <505ca3e9$0$29981$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


Steven D'Aprano writes:

> Python floats can represent exact integer values (e.g. 42.0), but above a 
> certain value (see below), not all integers can be represented. For 
> example:
> 
> py> 1e16 == 1e16 + 1  # no such float as 10000000000000001.0
> True
> py> 1e16 + 3 == 1e16 + 4  # or 10000000000000003.0
> True
> 
> So some integers are missing from the floats. For large enough values, 
> the gap between floats is rather large, and many numbers are missing:
> 
> py> 1e200 + 1e10 == 1e200
> True
> 
> The same applies for large enough negative values.
> 
> The question is, what is the largest integer number N such that every 
> whole number between -N and N inclusive can be represented as a float?
> 
> If my tests are correct, that value is 9007199254740992.0 = 2**53.
> 
> Have I got this right? Is there a way to work out the gap between one 
> float and the next?

There is a way to find the distance between two IEEE floats in "ulps",
or "units in the last position", computable from the bit pattern using
integer arithmetic. I think it's then also possible to find the next
float by adding one.

I don't have a link at hand, I'm too tired to search at the moment,
and I'm no expert on floats, but you might find an answer by looking
for ulps.

> (I haven't tried to exhaustively check every float because, even at one 
> nanosecond per number, it will take over 200 days.)

Come to think of it, the difference between adjacent floats is exactly
one ulp. Just use the right unit :)

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


Thread

Exact integer-valued floats Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-21 17:29 +0000
  Re: Exact integer-valued floats Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-21 12:13 -0600
  Re: Exact integer-valued floats Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-09-21 22:47 +0300
  Re: Exact integer-valued floats Nobody <nobody@nowhere.com> - 2012-09-21 20:59 +0100
  Re: Exact integer-valued floats Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-21 16:26 -0400
    Re: Exact integer-valued floats Hans Mulder <hansmu@xs4all.nl> - 2012-09-21 23:04 +0200
      Re: Exact integer-valued floats Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-21 20:54 -0400
  Re: Exact integer-valued floats Alister <alister.ware@ntlworld.com> - 2012-09-21 21:14 +0000
  Re: Exact integer-valued floats Paul Rubin <no.email@nospam.invalid> - 2012-09-21 15:23 -0700
    Re: Exact integer-valued floats Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-22 01:36 +0000
      Re: Exact integer-valued floats Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-22 01:01 -0400
        Re: Exact integer-valued floats Tim Roberts <timr@probo.com> - 2012-09-22 14:05 -0700
          Re: Exact integer-valued floats Dave Angel <d@davea.name> - 2012-09-22 19:06 -0400
            Re: Exact integer-valued floats Hans Mulder <hansmu@xs4all.nl> - 2012-09-23 09:45 +0200
          Re: Exact integer-valued floats wrw@mac.com - 2012-09-24 11:29 -0400
    Re: Exact integer-valued floats Nobody <nobody@nowhere.com> - 2012-09-22 11:19 +0100

csiph-web