What is the max value of *big.Int and max precision of *big.Rat?
Asked
Active
Viewed 1.3k times
23
-
1who is downvoting all these questions and answers on the go tag? this question is totally valid! – thwd Jul 10 '13 at 07:53
1 Answers
19
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 (
"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)
}
(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.

Denys Séguret
- 372,613
- 87
- 782
- 758
-
1When using `math/big` you should try and avoid unnecessary `big.Int` allocations (in particular if they escape to the heap). The above can be made faster and simpler by eliminating `temp` making the loop body just: `verybig.Mul(verybig, ten)`. – Dave C Oct 29 '17 at 16:04
-