liveBook · Manning

IEEE 754

As explained in 100 Go Mistakes and How to Avoid Them

IEEE-754 is the standard defining representations and arithmetic behavior for floating-point numbers.

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

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Descends to sign, exponent, mantissa, and accumulation effects, but stops short of explaining conversion mechanics.
Examples
Uses multiplication, repeated addition, tolerance comparisons, special values, and distributive-law calculations.
What it assumes
Assumes basic Go numeric types while introducing floating-point approximation and IEEE 754 representation.
What you can do after
Diagnose precision surprises, choose tolerance-based comparisons, and recognize special floating-point results.
How it connects
Connects IEEE 754 with float32, float64, precision, comparisons, special values, and arithmetic associativity.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.