floating-point number

A number with a fractional part stored in binary, which can only approximate most decimal values.

In more detail

Most languages store floats in 64 bits using one common international standard. Because values such as 0.1 have no exact binary form, 0.1 + 0.2 is not exactly 0.3, so compare floats with a tolerance and keep money in whole cents.

Examples

Counting and radix sort do not apply directly to float keys.
echo "scale=2; 10 / 3" | bc
double bad = 0.1 + 0.2; // 0.30000000000000004
float64(used) / float64(total) * factor
JavaScript numbers are 64-bit floating-point numbers; Float64Array stores them in raw memory.
0.1 + 0.2 == 0.3   # False
SQLite calls this storage class REAL.
SELECT 0.1 + 0.2 = 0.3;  -- 0 (false)
In this course: Because 1.178 cannot be stored exactly, 7 * 1.178 comes out as 8.245999999999999 rather than 8.246. Comparing floats with == is therefore fragile; use pytest.approx in tests, and keep money in whole pence or a decimal type in production code.

Related terms

Taught in

References

Certification courses