负的 big.Int 在使用 Int64() 转换后变为正数,这是由于整数溢出引起的。

huangapple go评论87阅读模式
英文:

Negative big.Int turns positive after using Int64() conversion due integer overflow

问题

我有一个简单的if语句,用于比较两个数字。由于编译错误,我无法使用big.Int与零进行比较,因此我尝试将其转换为int64和float32。问题是,在调用Int64float32(diff.Int64())之后,diff会被转换为一个正数,这可能是整数溢出的结果。如果有人能够向我展示如何安全地准备diff变量以与零进行比较,我将不胜感激。

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 << 这是应该与0进行比较的正确数字

	fmt.Println(diff.Int64())
	// 2566553871551071330 << 这是if语句与0进行比较的结果
    if diff.Int64() > 0 {
       fmt.Println(float64(diff.Int64()), "大于0")
    }

}
英文:

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 (
	&quot;fmt&quot;
	&quot;math/big&quot;
)

func main() {
	var amount1 = &amp;big.Int{}
	var amount2 = &amp;big.Int{}
	amount1.SetString(&quot;465673065724131968098&quot;, 10)
	amount2.SetString(&quot;500000000000000000000&quot;, 10)

	diff := big.NewInt(0).Sub(amount1, amount2)
	fmt.Println(diff)
	// -34326934275868031902 &lt;&lt; this is the correct number which should be compared to 0

	fmt.Println(diff.Int64())
	// 2566553871551071330 &lt;&lt; this is what the if statement compares to 0
    if diff.Int64() &gt; 0 {
       fmt.Println(float64(diff.Int64()), &quot;is bigger than 0&quot;)
    }

}

答案1

得分: 2

使用Int.Cmp()将其与另一个表示为0big.Int值进行比较。

例如:

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

这将输出:

-34326934275868031902 is less than 0

当与特殊的0进行比较时,你也可以使用Int.Sign(),它根据与0的比较结果返回-1、0、+1。

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

这将输出:

-34326934275868031902 is negative

Go Playground上尝试这些示例。

参考资料:

https://stackoverflow.com/questions/64257065/is-there-another-way-of-testing-if-a-big-int-is-0/64257532#64257532

英文:

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, &quot;is less than&quot;, zero)
case 0:
	fmt.Println(diff, &quot;is&quot;, zero)
case 1:
	fmt.Println(diff, &quot;is greater than&quot;, 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, &quot;is negative&quot;)
case 0:
	fmt.Println(diff, &quot;is 0&quot;)
case 1:
	fmt.Println(diff, &quot;is positive&quot;)
}

This will output:

-34326934275868031902 is negative

Try the examples on the Go Playground.

See related:

https://stackoverflow.com/questions/64257065/is-there-another-way-of-testing-if-a-big-int-is-0/64257532#64257532

huangapple
  • 本文由 发表于 2022年7月8日 21:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/72912380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定