我认为我不必写这本书,但是我至少找不到一个减去大数的例子
所以我有两个数字,我想将它们相减,库说只有一个内存限制,但是,当我尝试将两个256位数字相减时,我得到一个错误。
告诉我该怎么办以及如何解决? 我开始学习Go,一切似乎都很棒,但是我经常遇到类似的问题。怎么了............
package main
import (
"fmt"
"math/big"
)
func main() {
a := big.NewInt(113792089237316195423570985008687907853269984665640564039457584007908834671645)
b := big.NewInt(20277110887056303803699431755396003735040374760118964734768299847012543114150)
c := big.NewInt(0).Sub(a, b)
fmt.Println("c =", c)
}
输出:
*prog.go:9:18: constant 113792089237316195423570985008687907853269984665640564039457584007908834671645 overflows int64
prog.go:10:18: constant 20277110887056303803699431755396003735040374760118964734768299847012543114150 overflows int64*
我正在使用go版本go1.12.4 linux / amd64
答案 0 :(得分:2)
好像您需要在代码中使用big.Int.SetString
。这是一个游乐场链接,在其中起作用:https://play.golang.org/p/HvEke4g7e8V
对于那些不想单击链接的人:
package main
import (
"fmt"
"math/big"
)
func main() {
a, _ := new(big.Int).SetString("113792089237316195423570985008687907853269984665640564039457584007908834671645", 10)
b, _ := new(big.Int).SetString("20277110887056303803699431755396003735040374760118964734768299847012543114150", 10)
c := big.NewInt(0).Sub(a, b)
fmt.Println("c =", c)
}
答案 1 :(得分:0)
mde .... ................
package main
import (
"fmt"
"math/big"
)
func main() {
i := new(big.Int)
i.SetString("113792089237316195423570985008687907853269984665640564039457584007908834671645", 10)
k := new(big.Int)
k.SetString("20277110887056303803699431755396003735040374760118964734768299847012543114150", 10)
c := big.NewInt(0).Sub(i, k)
fmt.Println("c =", c)
}