Golang,math/big:*big.Int的最大值是多少?

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

Golang, math/big: what is the max value of *big.Int

问题

big.Int的最大值是多少?big.Rat的最大精度是多少?

英文:

What is the max value of *big.Int and max precision of *big.Rat?

答案1

得分: 19

这里是结构定义:

// Word表示多精度无符号整数的单个数字。
type Word uintptr

type nat []Word

type Int struct {
neg bool // 符号
abs nat // 整数的绝对值
}

type Rat struct {
// 为了使Rat的零值在没有初始化的情况下工作,
// b的零值(len(b) == 0)的行为类似于b == 1。
// a.neg确定Rat的符号,b.neg被忽略。
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上运行得足够快,请使用比100000更小的指数)

问题不在于最大大小,而在于使用的内存和这些计算所需的时间。

英文:

Here are the structure definitions :

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

There is no explicit limit. The limit will be your memory or, theoretically, the max array size (2^31 or 2^63, depending on your platform).


If you have practical concerns, you might be interested by the tests made in http://golang.org/src/pkg/math/big/nat_test.go, for example the one where 10^100000 is benchmarked.

And you can easily run this kind of program :

package main

import (
	&quot;fmt&quot;
	&quot;math/big&quot;
)

func main() {
	verybig := big.NewInt(1)
	ten := big.NewInt(10)
	for i:=0; i&lt;100000; i++ {
	   verybig.Mul(verybig, ten)
	}
	fmt.Println(verybig)
}

(if you want it to run fast enough for Go Playground, use a smaller exponent than 100000)

The problem won't be the max size but the used memory and the time such computations take.

huangapple
  • 本文由 发表于 2013年7月10日 15:02:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/17564335.html
匿名

发表评论

匿名网友

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

确定