Why are the following unequal in Go? Is this a bug, or is it by design? If it's by design, why does this occur and is this type of behavior documented anywhere?
https://play.golang.org/p/itEV9zwV2a
package main
import (
"fmt"
)
func main() {
x := 10.1
fmt.Println("x == 10.1: ", x == 10.1)
fmt.Println("x*3.0 == 10.1*3.0:", x*3.0 == 10.1*3.0)
fmt.Println("x*3.0: ", x*3.0)
fmt.Println("10.1*3.0: ", 10.1*3.0)
}
Produces:
x == 10.1: true
x*3.0 == 10.1*3.0: false
x*3.0: 30.299999999999997
10.1*3.0: 30.3
Note that the same floating point math is being performed, just with different syntax. So why is the result different? I would expect 10.1*3.0
to equal 30.29999...
as in the x*3.0
example.