liveBook · Manning

Conversion error

As explained in 100 Go Mistakes and How to Avoid Them

A conversion error is the numerical discrepancy introduced when converting a decimal or real value into a finite floating-point representation.

A conversion error is the discrepancy introduced when a real or decimal value is stored in finite floating-point form. Go's float32 and float64 values approximate real arithmetic because infinitely many real values must fit into a finite number of bits. section 3.3

Multiplication exposes the approximation

Although 1.0001 × 1.0001 equals 1.00020001 mathematically, multiplying a float32 value can print 1.0002. The stored value is already slightly different from 1.0001, so the resulting arithmetic inherits that discrepancy. section 3.3

var n float32 = 1.0001
fmt.Println(n * n)

Run the float32 multiplication to observe conversion error directly. section 3.3

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains finite representation, IEEE-754 fields, and multiplication effects while stopping short of a full conversion algorithm.
Examples
Uses float32 storage of 1.0001 and its multiplication result to expose approximation.
What you can do after
Evaluate unexpected floating-point results by recognizing that stored decimal values may already be approximations.
How it connects
Connects conversion error to floating-point arithmetic, IEEE-754, mantissa, exponent, and precision loss.

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.