If you need to deal with numbers beyond modern CPU can handle (as in, beyond uint64
or int64
), you need the big number package. This package handles mainly 3 types of big numbers:
There are something to note is that these big numbers are a type of structure objects. Hence, normal arithmetic calculation will be using its internal functions rather than our conventional way of performing arithmetic calculations.
Creating the big number is quite straight-forward depending on how big the number is. Most of the time, you would want to translate from String (since you can't use any primitive number types).
For big integer, to create from string would be using SetString
function:
i := new(big.Int)
i.SetString("1180591620717411303424", 10)
For big float, to create from string would be using SetString
function:
i := new(big.Float)
i.SetString("1180591620717411303424")
For big rational numbers, to create from string would be using SetString
function:
i := new(big.Rat)
i.SetString("1180591620717411303424/43121243546437465891334573467")
The string is expressed in mathematical form where the divider is between the dividend and divisor.
All 3 of them has an internal function String()
. That is how you read the value from it.
var x := i.String()
fmt.Printf("%v\n", i)
There are various ways to do arithmetic with the big numbers, some are advanced mathematics calculations like squaring 2 big numbers, bit-wise operations, etc. You need to consult the documentation for those operations. Here, we only show the basic types of calculations:
Adding 2 big numbers uses the Add(x, y TYPE)
function. Different big numbers has different add function. A big number can only perform addition to its own big number types. Here are some examples:
i := new(big.Rat)
i.SetString("1/2")
i.Add(i, big.NewRat(1, 4)) // add 1/4
fmt.Printf("%v\n", i)
i := new(big.Float)
i.SetString("1e24")
i.Add(i, i)
fmt.Printf("%v\n", i)
i := new(big.Int)
i.SetString("1180591620717411303424", 10)
i.Add(i, i)
fmt.Printf("%v\n", i)
There is no function related to minus or subtraction. You need to use add and set the value to negative for addition. Example:
i := new(big.Int)
i.SetString("1180591620717411303424", 10)
i.Add(i, big.NewInt(-3000))
fmt.Printf("%v\n", i)
Multiplication is available via the mul(x, y TYPE)
function. It allows large values output. Here are some examples:
i := new(big.Rat)
i.SetString("1/2")
i.Mul(i, big.NewRat(1, 4)) // add 1/4
fmt.Printf("%v\n", i)
i := new(big.Float)
i.SetString("1e24")
i.Mul(i, i)
fmt.Printf("%v\n", i)
i := new(big.Int)
i.SetString("1180591620717411303424", 10)
i.Mul(i, i)
fmt.Printf("%v\n", i)
Not all Big Numbers supports this function. You need to read the documentation and workaround those that does not with multiplication. Example, for rational number, you need to swap the dividend and divisor position before sending in for multiplications. Float does not support division.
i := new(big.Int)
i.SetString("1180591620717411303424", 10)
i.Div(i, i)
fmt.Printf("%v\n", i)
There are some other functions available for each types of Big Numbers. Some of them are specialized to that Big Number. These are:
Not(...)
, Or(...)
, And(...)
, Xor(...)
- for bit-wise operations.Inv(...)
, Denom(...)
- for rational numbers specific calculations.Acc(...)
, - for float adjustment and specific calculations.That's all about the big numbers in Go.