-2

Golang's default float type is float64, but I got two different results for:

1.005 * 100            // 100.5
float64(1.005) * 100   // 100.49

Besides, I got another problem:

var n = 1.005
const m = 1.005
rn := n * n    // 1.01002499
rm := m * m    // 1.010025
t.Log(rn, rm)

Anyone kindly explain it?

Laisky
  • 55
  • 5

1 Answers1

1

When multiplying 2 constants together, the math is done at compile time with arbitrary precision arithmetic[1]. This means that the result can have a higher accuracy than regular float operations, which are done with the standard floating point operations in your CPU and subject to precision limits.

  1. According to the spec, an implementation may limit the precision of constants, but in any case it must be well above the max precision of either float32 or float64.
Hymns For Disco
  • 7,530
  • 2
  • 17
  • 33