Python Glossary Русский

all Python 3Data typesterm

float

numbers

Double-precision floating-point number type (64-bit, IEEE 754). Accepts numbers, strings and the special values 'inf', '-inf' and 'nan'.

Syntax

float([x])

Because of binary representation, comparing floats with == is unreliable (0.1 + 0.2 != 0.3) — use math.isclose. The special value nan equals nothing, not even itself, so x == x is False for a nan — test it with math.isnan(x) instead.

Examples

x = 3.14  # литерал float
y = 1e6  # 1000000.0 (экспоненциальная запись)
z = float("2.71")  # из строки → 2.71
print(round(0.1 + 0.2, 10))  # → 0.3 (ошибка точности IEEE 754)
import math; print(math.inf)  # → inf
print(float("nan"))  # → nan
print(0.1 + 0.2 == 0.3)  # → False (проблема точности float)
print(float(42))  # → 42.0 (int → float)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy