Featureintro
The float type
Also called Python float, double precision in Python, float(), sys.float_info, float.hex, NaN and infinity in Python.
float is a binary floating-point number, usually a C double. float() reads decimal strings, "inf" and "nan", and hex() and fromhex() give exact text forms. NaN is not equal to itself.
- Language
- Python
Synopsis
import sys
print(sys.float_info.mant_dig, sys.float_info.dig, sys.float_info.epsilon)
print(sys.float_info.max)
print(0.1 + 0.2, 1e308 * 10, float("-inf"), float(" 1_000.5 "))
53 15 2.220446049250313e-16
1.7976931348623157e+308
0.30000000000000004 inf -inf 1000.5
A float has a 53-bit significand. 0.1 + 0.2 is not exactly 0.3, and a result that is too large becomes inf.
Semantics
r1. Floating-point numbers "are usually implemented using double in C", and sys.float_info gives the precision and the internal representation on the current machine.1 Its fields come from the C header float.h. For example, epsilon is the difference between 1.0 and the next larger float, and max is the largest finite float.4 Example: float-basics.
r2. A float represents a "machine-level double precision" number, and the range and the handling of overflow depend on the machine. Python has no single-precision float type.5
r3. A numeric literal with a decimal point or an exponent gives a float.1 float() reads a decimal number with an optional sign and whitespace around it, and also the strings for NaN and positive or negative infinity, in any case, such as inf, Infinity and nan. With an integer or a float, it returns a float with the same value "within Python's floating-point precision", and a value outside the float range raises OverflowError.3 Example: float-basics.
r4. as_integer_ratio() returns a pair of integers whose ratio is exactly the float, and is_integer() tests for a finite integral value. A decimal string usually has a small rounding error, but hexadecimal strings "allow exact representation and specification of floating-point numbers": hex() writes one and the class method float.fromhex() reads one.2 Example: exact-forms.
r5. "Any ordered comparison of a number to a not-a-number value is false", and a NaN is not equal to itself, as IEEE 754 requires.6 Example: nan-compare.
r6. Conversion from float to int "truncates, discarding the fractional part".1 round() rounds to the closer multiple of the power of ten and, on a tie, "toward the even choice".7 Example: round-half-even.
Examples
The exact value of 0.1 is a ratio with a power of two in the denominator. The hexadecimal forms are exact (rule r4).
print((0.25).as_integer_ratio(), (0.1).as_integer_ratio())
print((-2.0).is_integer(), (3.2).is_integer())
print(float.fromhex("0x3.a7p10"), (3740.0).hex(), (0.1).hex())
(1, 4) (3602879701896397, 36028797018963968)
True False
3740.0 0x1.d380000000000p+11 0x1.999999999999ap-4
Pitfalls
NaN is not equal to itself
x == x is false for a NaN, and every ordered comparison with it is false (rule r5). x != x is true only for a NaN.
This code runs but gives a wrong answer on purpose.
x = float("nan")
print(x == x, x != x, 3 < x, x < 3, x > 3)
False True False False False
round() and halves
round(2.5) is 2, because a tie goes to the even neighbor (rule r6). round(2.675, 2) is 2.67 and not 2.68. Built-in Functions says that this is not a bug: most decimal fractions cannot be represented exactly as a float.7
This code runs but gives a wrong answer on purpose.
print(round(0.5), round(1.5), round(2.5), round(3.5))
print(round(2.675, 2))
0 2 2 4
2.67
Comparing results of float arithmetic with ==
0.1 + 0.2 == 0.3 is false (see float-basics). Compare with a tolerance, for example with math.isclose(), or use decimal.Decimal or fractions.Fraction when exact decimal or rational values matter.
Comparison
Go has two floating-point types, float32 and float64, "the set of all IEEE 754 32-bit floating-point numbers" and the 64-bit set.8 Python has one float type, usually a C double, and no single-precision type like float32 (rules r1 and r2).
References
- Built-in Types, Python 3.15 documentation, Numeric Types - int, float, complex
- Built-in Types, Python 3.15 documentation, Additional Methods on Float
- Built-in Functions, Python 3.15 documentation, float()
- sys, System-specific parameters and functions, Python 3.15 documentation, sys.float_info
- The Python Language Reference, Python 3.15, 3.2.4.2. numbers.Real (float)
- The Python Language Reference, Python 3.15, 6.10.1. Value comparisons
- Built-in Functions, Python 3.15 documentation, round()
- The Go Programming Language Specification, Numeric types
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag SXWBDY Edit this page History Report an error Markdown