Floating-point numbers
Числа с плавающей точкой
Floating-point is a way to store and calculate with numbers that can have a fractional part. It covers a wide range of magnitudes, but rounds values that do not fit exactly.
Also known as: float, IEEE 754
What makes the point float?
Think of scientific notation: 6.25 × 10³ and 6.25 × 10⁻³ use the same digits at different scales. The exponent determines where the decimal point lands when you write the number out. Binary floating-point uses the same idea with powers of two. That lets a single format store tiny fractions and huge numbers without reserving a fixed number of digits after the point.
Range is not precision
IEEE 754 defines floating-point formats and arithmetic rules, including how to round results. In its binary formats, a finite value is determined by a sign, a significand (the significant digits), and an exponent. The common formats binary32 and binary64 occupy 32 and 64 bits and offer 24 and 53 bits of precision for normal numbers, respectively. A type name alone doesn't tell you the format: Java's float is binary32, while JavaScript's Number is binary64.
The exponent gives you range, but the significand limits how finely you can distinguish nearby values. Representable numbers are farther apart at larger magnitudes. With binary64 and the default rounding rule, adding 1 to 1e16 produces 1e16 again: the exact sum falls between representable values and rounds back down.
Why 0.1 + 0.2 isn't exactly 0.3
A fraction like 0.5 is exact in binary because it's 1/2. The binary expansion of 0.1 repeats, much like the decimal expansion of 1/3. A finite format must round that expansion, and an arithmetic operation may round its result again. This is why the computed sum and the literal 0.3 can end up as different stored values.
import math
total = 0.1 + 0.2
print(total) # 0.30000000000000004
print(total == 0.3) # False
print(math.isclose(total, 0.3, rel_tol=1e-9)) # True
Use a tolerance when the requirement is numerical closeness rather than exact equality. A relative tolerance scales with the values being compared; an absolute tolerance is useful near zero. Choose them based on how much error is acceptable in the application, not just what makes a test pass.
When to use it
Binary floating-point is a good fit for simulations, graphics, and machine learning, where a small approximation is often acceptable and fast arithmetic matters. For prices or balances with a fixed unit, consider integers that count cents or other minor units. If the domain calls for decimal fractions and explicit rounding rules, consider decimal arithmetic. In Python, build Decimal('0.1') from a string rather than passing 0.1 as a float and preserving its binary approximation.
What interviewers ask
You may be asked to explain the 0.1 + 0.2 example or write a test for a numerical result. Distinguish rounding when values are stored from rounding during arithmetic. A common follow-up is whether changing the order of addition can change the result. It can: rounding an intermediate sum can discard a contribution that would have survived in a different order.