英文:
Negative big.Int turns positive after using Int64() conversion due integer overflow
问题
我有一个简单的if语句,用于比较两个数字。由于编译错误,我无法使用big.Int
与零进行比较,因此我尝试将其转换为int64和float32。问题是,在调用Int64
或float32(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 (
"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")
}
}
答案1
得分: 2
使用Int.Cmp()
将其与另一个表示为0
的big.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上尝试这些示例。
参考资料:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论