What is the biggest non-integer float value in Python?
Question:
Floats, in any language or platform, have limited precision. They must have a finite number of digits "after the comma" when written in base 2, and that number is bound by the number of digits used as the mantissa (see floating-point arithmetics for more information).
When you get float numbers of increasing values, there comes a point where you can’t write enough digits for any to end up after the comma – I suppose this may be false for a very long mantissa and very short exponent implementation, but I doubt it exists in practice.
How can I calculate that number, the largest float
number b
, such that b.is_integer()
is False?
I’m asking for a calculation which would work on any implementation of Python, so presumably using sys.float_info
.
(obviously, inf doesn’t count)
Answers:
The largest finite non-integer float value would be math.nextafter(sys.float_info.radix**(sys.float_info.mant_dig - 1), 0)
:
In [1]: import sys
In [2]: import math
In [3]: math.nextafter(sys.float_info.radix**(sys.float_info.mant_dig - 1), 0)
Out[3]: 4503599627370495.5
sys.float_info.mant_dig
is the number of significant figures in a float
value in the floating-point representation’s radix.
sys.float_info.radix**(sys.float_info.mant_dig - 1)
is the smallest number where all these significant figures would be before the decimal point as a float, and the math.nextafter
call produces the largest value where one of these significant figures is after the decimal point. (sys.float_info.radix**(sys.float_info.mant_dig - 1)
happens to be an int rather than a float, but that’s fine.)
Floats, in any language or platform, have limited precision. They must have a finite number of digits "after the comma" when written in base 2, and that number is bound by the number of digits used as the mantissa (see floating-point arithmetics for more information).
When you get float numbers of increasing values, there comes a point where you can’t write enough digits for any to end up after the comma – I suppose this may be false for a very long mantissa and very short exponent implementation, but I doubt it exists in practice.
How can I calculate that number, the largest float
number b
, such that b.is_integer()
is False?
I’m asking for a calculation which would work on any implementation of Python, so presumably using sys.float_info
.
(obviously, inf doesn’t count)
The largest finite non-integer float value would be math.nextafter(sys.float_info.radix**(sys.float_info.mant_dig - 1), 0)
:
In [1]: import sys
In [2]: import math
In [3]: math.nextafter(sys.float_info.radix**(sys.float_info.mant_dig - 1), 0)
Out[3]: 4503599627370495.5
sys.float_info.mant_dig
is the number of significant figures in a float
value in the floating-point representation’s radix.
sys.float_info.radix**(sys.float_info.mant_dig - 1)
is the smallest number where all these significant figures would be before the decimal point as a float, and the math.nextafter
call produces the largest value where one of these significant figures is after the decimal point. (sys.float_info.radix**(sys.float_info.mant_dig - 1)
happens to be an int rather than a float, but that’s fine.)