liveBook · Manning

FP32

As explained in Learn Go with Pocket-Sized Projects

float32 is Go's 32-bit floating-point type, typically providing approximately six to seven decimal significant digits of precision.

FP32, or Go's float32, stores floating-point values in 4 bytes under IEEE-754. It offers approximately six to seven reliable decimal significant digits, so values beyond that range may change during representation or arithmetic. section 6.2.1

Use FP32 with these limitations in mind:

  • Expect most decimal fractions to be approximate; values such as 0.625 are exactly representable, but many others are not. section 6.2.1
  • Treat additions and subtractions involving very different magnitudes cautiously: subtracting 1 from float32(100_000_000) leaves the stored value unchanged. section 6.2.3
  • Avoid == for floating-point results; compare values within an appropriate precision range instead. section 6.2.3

Precision loss in practice

Formatting float32(123_456_789) with %.f produces 123456792, while formatting float32(0.0123456789) with %.10f produces 0.0123456791. The first reliable digits remain useful; later digits should not be trusted. section 6.2.1

fmt.Printf("%.8f", float32(1)/float32(41)*float32(41))

Evaluate a float32 division and multiplication chain while inspecting its formatted result. section 6.2.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains IEEE-754 representation, significant-digit limits, arithmetic loss, and tolerance-based comparison through concrete examples.
Examples
Demonstrates rounded large integers, altered decimal fractions, lost currency units, and sine-of-pi tolerance checks.
What you can do after
Diagnose precision loss and apply tolerance-based comparisons instead of exact equality for FP32 results.
How it connects
Connects FP32 with IEEE-754, float64, floating-point arithmetic, and approximate equality.

Also explained in

Compare how 6 books explain FP32 →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.