A floating-point value represents a real number with a decimal part, such as 6.73 or 5.32. It helps describe measurements that do not fit neatly into whole-number counts: half a gallon of milk or part of a mile on a bike ride. section 2.11
Floating-point values can represent rational numbers, including fractions, finite decimals, and repeating decimals. They can also approximate irrational numbers such as pi, e, and the square root of 2. Because computers reserve a fixed number of bits—typically 64—for the digits, these values have limited precision. section 2.11 section 2.8
Practical consequences include:
- Expect small rounding errors when representing or calculating with irrational numbers. section 2.11
- Use symbolic libraries such as SymPy when exact symbolic values matter. section 2.11
- Watch for floating-point underflow when multiplying very small values; numerical libraries such as NumPy may provide suitable safeguards. section 2.11
Coffee measurements
A coffee scale may record whole grams, such as 27, even though the amount of coffee is conceptually continuous and could fall between those measurements. Treating the variable as a floating-point real number supports a continuous distribution, such as a normal distribution. section 4.3
import math irrational_numbers = [ math.pi , math.sqrt(2), math.e ] for x in irrational_numbers: print(x) # 3.141592653589793 # 1.4142135623730951 # 2.718281828459045
Inspect how Python displays approximations of pi, the square root of 2, and e. section 2.11
