*big.Int的最大值和*big.Rat的最大精度是多少?
最佳答案
以下是结构定义:
// A Word represents a single digit of a multi-precision unsigned integer.
type Word uintptr
type nat []Word
type Int struct {
neg bool // sign
abs nat // absolute value of the integer
}
type Rat struct {
// To make zero values for Rat work w/o initialization,
// a zero value of b (len(b) == 0) acts like b == 1.
// a.neg determines the sign of the Rat, b.neg is ignored.
a, b Int
}
没有明确的限制。限制将是您的内存,或者理论上是最大数组大小(2^31 或 2^63,具体取决于您的平台)。
如果您有实际问题,您可能会对 http://golang.org/src/pkg/math/big/nat_test.go 中的测试感兴趣,例如以 10^100000 为基准的那个。
你可以轻松运行这种程序:
package main
import (
"fmt"
"math/big"
)
func main() {
verybig := big.NewInt(1)
ten := big.NewInt(10)
for i:=0; i<100000; i++ {
verybig.Mul(verybig, ten)
}
fmt.Println(verybig)
}
(如果你想让它跑得足够快,可以玩 Go Playground,use a smaller exponent than 100000
)
问题不在于最大大小,而在于使用的内存和此类计算所花费的时间。
关于Golang,数学/大 : what is the max value of *big. Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17564335/