A common mistake is expecting floating-point arithmetic to preserve every decimal exactly. Go follows IEEE 754, storing float32 and float64 as finite-bit approximations with sign, exponent, and mantissa fields. section 3.3
Why the result is not exact
Multiplying 1.0001 by itself may print 1.0002 rather than 1.00020001. A float32 stores 1.0001 approximately as 1.000100016593933, so later arithmetic inherits that error. Accumulation order also matters: adding 1.0001 repeatedly before adding 10,000 differs from adding 10,000 first. section 3.3
Practical consequences include:
- Avoid direct == comparisons when small representation errors are acceptable; compare the difference against an appropriate error threshold. section 3.3
- Remember that arithmetic is not necessarily associative: a × (b + c) can differ slightly from a × b + a × c. section 3.3
- Floating-point operations can produce +Inf, -Inf, and NaN, including division by zero or zero by zero. section 3.3
