In the Tour of Go, step 13, I changed a bit the script
package main
import (
"fmt"
"math"
)
func main() {
var x, y int = 3, 4
var f float64 = math.Sqrt(float64(x*x + y*y))
var z uint = uint(f)
fmt.Println(x, y, z, f)
}
This returns: 3 4 5 5, fine.
But when I do this change: negating f
:
func main() {
var x, y int = 3, 4
var f float64 = - math.Sqrt(float64(x*x + y*y))
var z uint = uint(f)
fmt.Println(x, y, z, f)
}
It responds: 3 4 18446744073709551611 -5
Why does the -5.0 being converted to 18446744073709551611 by the uint
method?
Isn't there something - an algorithm? - trying to check what it is doing beneath?
It's like the uint
method was brutally casting at binary level from float with mantissa and exponent to an integer?
What is happening?
If I am misled, and that methods uint
aren't conversion functions like I believed,
but cast methods like you could find (uint)f
in C,
what is the conversion function that ensure that:
uint z = anotherFunction(any float64)
will return the positive integer value of that float number?