Go float vs uint64 comparison issue

Viewed 154

Working on a problem that compares a float and a uint64, in which the float equals MaxUint64+1. The comparison works fine with a float literal. However when the float is assigned to a variable, the comparison breaks.

func main() {
    x := 18446744073709551616.0
    fmt.Println(x == 18446744073709551616.0) //true
    fmt.Println(18446744073709551616.0 > math.MaxUint64) //true
    fmt.Println(x > math.MaxUint64) //false
}

https://play.golang.org/p/O65padWxV8L

Further digging shows this issue does not manifest for all floats > MaxUint64. When x is assigned to anything from 18446744073709553665.0 (or MaxUint64+2050), the comparison works.

0 Answers
Related