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

Dmitriy  Blokhin picture Dmitriy Blokhin · Jul 10, 2013 · Viewed 7.9k times · Source

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

Answer

Denys Séguret picture Denys Séguret · Jul 10, 2013

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.