0 day streak
3. Complexity and mathFloating point

Floating point

Floating point is how computers store numbers with fractions: a fixed number of bits used like scientific notation in binary, with a sign, a set of significant digits (the mantissa), and an exponent saying where the binary point goes. That covers a vast range, from tiny to astronomical, but only a finite set of values, so most decimals can’t be stored exactly. 0.1 is a repeating fraction in binary, like 1/3 in decimal, so it gets rounded, which is why 0.1 + 0.2 prints as 0.30000000000000004. Precision is relative, too: big numbers have bigger gaps between them. So you compare floats with a tolerance, and store money as whole cents or in a decimal type.

In this lesson: A bit-level explorer: type 0.1 and see the sign, exponent, mantissa and actual stored value. Includes a money bug demo.