Home > front end >  Negative big.Int turns positive after using Int64() conversion due integer overflow
Negative big.Int turns positive after using Int64() conversion due integer overflow

Time:07-09

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")
    }

}

CodePudding user response:

Use Int.Cmp() to compare it to another big.Int value, one representing 0.

For example:

zero := new(big.Int)

switch result := diff.Cmp(zero); result {
case -1:
    fmt.Println(diff, "is less than", zero)
case 0:
    fmt.Println(diff, "is", zero)
case 1:
    fmt.Println(diff, "is greater than", zero)
}

This will output:

-34326934275868031902 is less than 0

When comparing to the special 0, instead you could also use Int.Sign() which returns -1, 0, 1 depending on the result of the comparison to 0.

switch sign := diff.Sign(); sign {
case -1:
    fmt.Println(diff, "is negative")
case 0:
    fmt.Println(diff, "is 0")
case 1:
    fmt.Println(diff, "is positive")
}

This will output:

-34326934275868031902 is negative

Try the examples on the Go Playground.

See related:

Is there another way of testing if a big.Int is 0?

  • Related