I have a simple if statement which compares two numbers. I couldn't use big.Int
to compare with zero due to compile error, therefore I tried to convert to an int64 and to a float32. The problem is that after calling Int64
or float32(diff.Int64())
, diff
gets converted into a positive number which is a result of an integer overflow I suspect. I would appreciate if somebody could show me what is the safe way to prepare the diff
variable for the comparison with zero.
package main
import (
"fmt"
"math/big"
)
func main() {
var amount1 = &big.Int{}
var amount2 = &big.Int{}
amount1.SetString("465673065724131968098", 10)
amount2.SetString("500000000000000000000", 10)
diff := big.NewInt(0).Sub(amount1, amount2)
fmt.Println(diff)
// -34326934275868031902 << this is the correct number which should be compared to 0
fmt.Println(diff.Int64())
// 2566553871551071330 << this is what the if statement compares to 0
if diff.Int64() > 0 {
fmt.Println(float64(diff.Int64()), "is bigger than 0")
}
}