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?

Value of
fis a negative number:-5.0. What would you expect when you convert this to a value of an unsigned type? Unsigned types have a valid range / representation where there's no room for-5.Go (like most other languages) uses 2's complement to represent integers. When you convert a signed integer to an unsigned integer, the representation (binary data in memory) is kept, but the number will be interpreted differently.
In general, when converting a negative signed value to an unsigned value, you can calculate the result using:
So for example when converting
-5of typeint8touint8, the result will be:The same goes for converting
-5of typeint64touint64:(Note: size of
intanduintis platform dependent, on the Go Playground it is of size 64-bit, same asint64anduint64.)The last missing piece: your
fis of typefloat64. Convertingfloat64to integer from Spec: Conversions: Conversions between numeric types:So
-5.0is converted to-5, then the above algorithm can be used how this will look like when interpreted as a value of typeuint.