-2

Could somebody explain me next rows:

var a, b = 2.0000001, 3.00000
fmt.Println(a + b)
fmt.Println(2.0000001 + 3.00000)

results of prints:

first print: 5.000000099999999

second print: 5.0000001

Why result of first print as above?

godvlpr
  • 115
  • 2
  • 9
  • 1
    The first `fmt.Println()` adds 2 `float64` non-constant values, the second one adds 2 constant values, using constant arithmetic, which uses arbitrary precision, and only converts the result to `float64`. – icza May 13 '22 at 08:40
  • @icza okay, but why non-constant arithmetic uses arbitrary precision? – godvlpr May 13 '22 at 08:47
  • Non-constant arithmetic uses the [IEEE 754 standard](https://en.wikipedia.org/wiki/IEEE_754), like most programming languages. – icza May 13 '22 at 09:10
  • See [Go Blog: Constants](https://go.dev/blog/constants) – icza May 13 '22 at 09:11

0 Answers0