# Namespace: go.std.math.big

v1.0

## Summary

Provides a low-level interface to the math/big package.

Package big implements arbitrary-precision arithmetic (big numbers).
The following numeric types are supported:

Int signed integers
Rat rational numbers
Float floating-point numbers

The zero value for an Int, Rat, or Float correspond to 0. Thus, new
values can be declared in the usual ways and denote 0 without further
initialization:

var x Int // &x is an *Int of value 0
var r = &Rat{} // r is a *Rat of value 0
y := new(Float) // y is a *Float of value 0

Alternatively, new values can be allocated and initialized with factory
functions of the form:

func NewT(v V) *T

For instance, NewInt(x) returns an *Int set to the value of the int64
argument x, NewRat(a, b) returns a *Rat set to the fraction a/b where
a and b are int64 values, and NewFloat(f) returns a *Float initialized
to the float64 argument f. More flexibility is provided with explicit
setters, for instance:

var z1 Int
z1.SetUint64(123) // z1 := 123
z2 := new(Rat).SetFloat64(1.25) // z2 := 5/4
z3 := new(Float).SetInt(z1) // z3 := 123.0

Setters, numeric operations and predicates are represented as methods of
the form:

func (z *T) SetV(v V) *T // z = v
func (z *T) Unary(x *T) *T // z = unary x
func (z *T) Binary(x, y *T) *T // z = x binary y
func (x *T) Pred() P // p = pred(x)

with T one of Int, Rat, or Float. For unary and binary operations, the
result is the receiver (usually named z in that case; see below); if it
is one of the operands x or y it may be safely overwritten (and its memory
reused).

Arithmetic expressions are typically written as a sequence of individual
method calls, with each call corresponding to an operation. The receiver
denotes the result and the method arguments are the operation's operands.
For instance, given three *Int values a, b and c, the invocation

computes the sum a + b and stores the result in c, overwriting whatever
value was held in c before. Unless specified otherwise, operations permit
aliasing of parameters, so it is perfectly ok to write

to accumulate values x in a sum.

(By always passing in a result value via the receiver, memory use can be
much better controlled. Instead of having to allocate new memory for each
result, an operation can reuse the space allocated for the result value,
and overwrite that value with the new result in the process.)

Notational convention: Incoming method parameters (including the receiver)
are named consistently in the API to clarify their use. Incoming operands
are usually named x, y, a, b, and so on, but never z. A parameter specifying
the result is named z (typically the receiver).

For instance, the arguments for (*Int).Add are named x and y, and because
the receiver specifies the result destination, it is called z:

func (z *Int) Add(x, y *Int) *Int

Methods of this form typically return the incoming receiver as well, to
enable simple call chaining.

Methods which don't require a result value to be passed in (for instance,
Int.Sign), simply return the result. In this case, the receiver is typically
the first operand, named x:

func (x *Int) Sign() int

Various methods support conversions between strings and corresponding
numeric values, and vice versa: *Int, *Rat, and *Float values implement
the Stringer interface for a (default) string representation of the value,
but also provide SetString methods to initialize a value from a string in
a variety of supported formats (see the respective SetString documentation).

Finally, *Int, *Rat, and *Float satisfy the fmt package's Scanner interface
for scanning and (except for *Rat) the Formatter interface for formatted
printing.

## Index

### Legend

Constant Variable Function Macro Special form GoType GoVar Receiver/Method

## Constants

Constants are variables with :const true in their metadata. Joker currently does not recognize them as special; as such, it allows redefining them or their values.
• ### Above

Int v1.0

Constants describing the Accuracy of a Float.

• ### AwayFromZero

Int v1.0

no IEEE 754-2008 equivalent

• ### Below

Int v1.0

Constants describing the Accuracy of a Float.

• ### Exact

Int v1.0

Constants describing the Accuracy of a Float.

• ### MaxBase

Int v1.0

MaxBase is the largest number base accepted for string conversions.

• ### ToNearestAway

Int v1.0

== IEEE 754-2008 roundTiesToAway

• ### ToNearestEven

Int v1.0

== IEEE 754-2008 roundTiesToEven

• ### ToNegativeInf

Int v1.0

== IEEE 754-2008 roundTowardNegative

• ### ToPositiveInf

Int v1.0

== IEEE 754-2008 roundTowardPositive

• ### ToZero

Int v1.0

== IEEE 754-2008 roundTowardZero

(None.)

## Functions, Macros, and Special Forms

• ### Jacobi

Function v1.0
````(Jacobi x y)`
```

Jacobi returns the Jacobi symbol (x/y), either +1, -1, or 0.
The y argument must be an odd integer.

Go input arguments: (x *Int, y *Int)

Go return type: int

Joker input arguments: [^(ref-to go.std.math.big/Int) x, ^(ref-to go.std.math.big/Int) y]

Joker return type: Int

• ### NewInt

Function v1.0
````(NewInt x)`
```

NewInt allocates and returns a new Int set to x.

Go input arguments: (x int64)

Go return type: *Int

Joker input arguments: [^Number x]

Joker return type: (ref-to go.std.math.big/Int)

• ### NewRat

Function v1.0
````(NewRat a b)`
```

NewRat creates a new Rat with numerator a and denominator b.

Go input arguments: (a int64, b int64)

Go return type: *Rat

Joker input arguments: [^Number a, ^Number b]

Joker return type: (ref-to go.std.math.big/Rat)

• ### ParseFloat

Function v1.0
````(ParseFloat s base prec mode)`
```

ParseFloat is like f.Parse(s, base) with f set to the given precision
and rounding mode.

Go input arguments: (s string, base int, prec uint, mode RoundingMode)

Go return type: (f *Float, b int, err error)

Joker input arguments: [^String s, ^Int base, ^Number prec, ^go.std.math.big/RoundingMode mode]

Joker return type: [(ref-to go.std.math.big/Float) Int Error]

## Types

• ### *Accuracy

Concrete GoType v1.0

• ### *ErrNaN

Concrete GoType v1.0

• ### *Float

Concrete GoType v1.0

• #### Acc

````([])`
```

Acc returns the accuracy of x produced by the most recent operation.

• #### GobEncode

````([])`
```

GobEncode implements the gob.GobEncoder interface.
The Float value and all its attributes (precision,
rounding mode, accuracy) are marshaled.

• #### Int64

````([])`
```

Int64 returns the integer resulting from truncating x towards zero.
If math.MinInt64 <= x <= math.MaxInt64, the result is Exact if x is
an integer, and Above (x < 0) or Below (x > 0) otherwise.
The result is (math.MinInt64, Above) for x < math.MinInt64,
and (math.MaxInt64, Below) for x > math.MaxInt64.

• #### IsInf

````([])`
```

IsInf reports whether x is +Inf or -Inf.

• #### IsInt

````([])`
```

IsInt reports whether x is an integer.
±Inf values are not integers.

• #### MarshalText

````([])`
```

MarshalText implements the encoding.TextMarshaler interface.
Only the Float value is marshaled (in full precision), other
attributes such as precision or accuracy are ignored.

• #### MinPrec

````([])`
```

MinPrec returns the minimum precision required to represent x exactly
(i.e., the smallest prec before x.SetPrec(prec) would start rounding x).
The result is 0 for |x| == 0 and |x| == Inf.

• #### Mode

````([])`
```

Mode returns the rounding mode of x.

• #### Parse

````([s base])`
```

Parse parses s which must contain a text representation of a floating-
point number with a mantissa in the given conversion base (the exponent
is always a decimal number), or a string representing an infinite value.

For base 0, an underscore character ``_'' may appear between a base
prefix and an adjacent digit, and between successive digits; such
underscores do not change the value of the number, or the returned
digit count. Incorrect placement of underscores is reported as an
error if there are no other errors. If base != 0, underscores are
not recognized and thus terminate scanning like any other character
that is not a valid radix point or digit.

It sets z to the (possibly rounded) value of the corresponding floating-
point value, and returns z, the actual base b, and an error err, if any.
The entire string (not just a prefix) must be consumed for success.
If z's precision is 0, it is changed to 64 before rounding takes effect.
The number must be of the form:

number = [ sign ] ( float | "inf" | "Inf" ) .
sign = "+" | "-" .
float = ( mantissa | prefix pmantissa ) [ exponent ] .
prefix = "0" [ "b" | "B" | "o" | "O" | "x" | "X" ] .
mantissa = digits "." [ digits ] | digits | "." digits .
pmantissa = [ "_" ] digits "." [ digits ] | [ "_" ] digits | "." digits .
exponent = ( "e" | "E" | "p" | "P" ) [ sign ] digits .
digits = digit { [ "_" ] digit } .
digit = "0" ... "9" | "a" ... "z" | "A" ... "Z" .

The base argument must be 0, 2, 8, 10, or 16. Providing an invalid base
argument will lead to a run-time panic.

For base 0, the number prefix determines the actual base: A prefix of
``0b'' or ``0B'' selects base 2, ``0o'' or ``0O'' selects base 8, and
``0x'' or ``0X'' selects base 16. Otherwise, the actual base is 10 and
no prefix is accepted. The octal prefix "0" is not supported (a leading
"0" is simply considered a "0").

A "p" or "P" exponent indicates a base 2 (rather then base 10) exponent;
for instance, "0x1.fffffffffffffp1023" (using base 0) represents the
maximum float64 value. For hexadecimal mantissae, the exponent character
must be one of 'p' or 'P', if present (an "e" or "E" exponent indicator
cannot be distinguished from a mantissa digit).

The returned *Float f is nil and the value of z is valid but not
defined if an error is reported.

• #### Prec

````([])`
```

Prec returns the mantissa precision of x in bits.
The result may be 0 for |x| == 0 and |x| == Inf.

• #### SetInf

````([signbit])`
```

SetInf sets z to the infinite Float -Inf if signbit is
set, or +Inf if signbit is not set, and returns z. The
precision of z is unchanged and the result is always
Exact.

• #### SetInt64

````([x])`
```

SetInt64 sets z to the (possibly rounded) value of x and returns z.
If z's precision is 0, it is changed to 64 (and rounding will have
no effect).

• #### SetMode

````([mode])`
```

SetMode sets z's rounding mode to mode and returns an exact z.
z remains unchanged otherwise.
z.SetMode(z.Mode()) is a cheap way to set z's accuracy to Exact.

• #### SetPrec

````([prec])`
```

SetPrec sets z's precision to prec and returns the (possibly) rounded
value of z. Rounding occurs according to z's rounding mode if the mantissa
cannot be represented in prec bits without loss of precision.
SetPrec(0) maps all finite values to ±0; infinite values remain unchanged.
If prec > MaxPrec, it is set to MaxPrec.

• #### SetString

````([s])`
```

SetString sets z to the value of s and returns z and a boolean indicating
success. s must be a floating-point number of the same format as accepted
by Parse, with base argument 0. The entire string (not just a prefix) must
be valid for success. If the operation failed, the value of z is undefined
but the returned value is nil.

• #### SetUint64

````([x])`
```

SetUint64 sets z to the (possibly rounded) value of x and returns z.
If z's precision is 0, it is changed to 64 (and rounding will have
no effect).

• #### Sign

````([])`
```

Sign returns:

-1 if x < 0
0 if x is ±0
+1 if x > 0

• #### Signbit

````([])`
```

Signbit reports whether x is negative or negative zero.

• #### String

````([])`
```

String formats x like x.Text('g', 10).
(String must be called explicitly, Float.Format does not support %s verb.)

• #### Text

````([format prec])`
```

Text converts the floating-point number x to a string according
to the given format and precision prec. The format is one of:

'e' -d.dddde±dd, decimal exponent, at least two (possibly 0) exponent digits
'E' -d.ddddE±dd, decimal exponent, at least two (possibly 0) exponent digits
'f' -ddddd.dddd, no exponent
'g' like 'e' for large exponents, like 'f' otherwise
'G' like 'E' for large exponents, like 'f' otherwise
'x' -0xd.dddddp±dd, hexadecimal mantissa, decimal power of two exponent
'p' -0x.dddp±dd, hexadecimal mantissa, decimal power of two exponent (non-standard)
'b' -ddddddp±dd, decimal mantissa, decimal power of two exponent (non-standard)

For the power-of-two exponent formats, the mantissa is printed in normalized form:

'x' hexadecimal mantissa in [1, 2), or 0
'p' hexadecimal mantissa in [½, 1), or 0
'b' decimal integer mantissa using x.Prec() bits, or 0

Note that the 'x' form is the one used by most other languages and libraries.

If format is a different character, Text returns a "%" followed by the
unrecognized format character.

The precision prec controls the number of digits (excluding the exponent)
printed by the 'e', 'E', 'f', 'g', 'G', and 'x' formats.
For 'e', 'E', 'f', and 'x', it is the number of digits after the decimal point.
For 'g' and 'G' it is the total number of digits. A negative precision selects
the smallest number of decimal digits necessary to identify the value x uniquely
using x.Prec() mantissa bits.
The prec value is ignored for the 'b' and 'p' formats.

• #### Uint64

````([])`
```

Uint64 returns the unsigned integer resulting from truncating x
towards zero. If 0 <= x <= math.MaxUint64, the result is Exact
if x is an integer and Below otherwise.
The result is (0, Above) for x < 0, and (math.MaxUint64, Below)
for x > math.MaxUint64.

• ### *Int

Concrete GoType v1.0

• #### Binomial

````([n k])`
```

Binomial sets z to the binomial coefficient of (n, k) and returns z.

• #### Bit

````([i])`
```

Bit returns the value of the i'th bit of x. That is, it
returns (x>>i)&1. The bit index i must be >= 0.

• #### BitLen

````([])`
```

BitLen returns the length of the absolute value of x in bits.
The bit length of 0 is 0.

• #### Bits

````([])`
```

Bits provides raw (unchecked but fast) access to x by returning its
absolute value as a little-endian Word slice. The result and x share
the same underlying array.
Bits is intended to support implementation of missing low-level Int
functionality outside this package; it should be avoided otherwise.

• #### Bytes

````([])`
```

Bytes returns the absolute value of x as a big-endian byte slice.

• #### GobEncode

````([])`
```

GobEncode implements the gob.GobEncoder interface.

• #### Int64

````([])`
```

Int64 returns the int64 representation of x.
If x cannot be represented in an int64, the result is undefined.

• #### IsInt64

````([])`
```

IsInt64 reports whether x can be represented as an int64.

• #### IsUint64

````([])`
```

IsUint64 reports whether x can be represented as a uint64.

• #### MarshalJSON

````([])`
```

MarshalJSON implements the json.Marshaler interface.

• #### MarshalText

````([])`
```

MarshalText implements the encoding.TextMarshaler interface.

• #### MulRange

````([a b])`
```

MulRange sets z to the product of all integers
in the range [a, b] inclusively and returns z.
If a > b (empty range), the result is 1.

• #### ProbablyPrime

````([n])`
```

ProbablyPrime reports whether x is probably prime,
applying the Miller-Rabin test with n pseudorandomly chosen bases
as well as a Baillie-PSW test.

If x is prime, ProbablyPrime returns true.
If x is chosen randomly and not prime, ProbablyPrime probably returns false.
The probability of returning true for a randomly chosen non-prime is at most ¼ⁿ.

ProbablyPrime is 100% accurate for inputs less than 2⁶⁴.
See Menezes et al., Handbook of Applied Cryptography, 1997, pp. 145-149,
and FIPS 186-4 Appendix F for further discussion of the error probabilities.

ProbablyPrime is not suitable for judging primes that an adversary may
have crafted to fool the test.

As of Go 1.8, ProbablyPrime(0) is allowed and applies only a Baillie-PSW test.
Before Go 1.8, ProbablyPrime applied only the Miller-Rabin tests, and ProbablyPrime(0) panicked.

• #### SetInt64

````([x])`
```

SetInt64 sets z to x and returns z.

• #### SetString

````([s base])`
```

SetString sets z to the value of s, interpreted in the given base,
and returns z and a boolean indicating success. The entire string
(not just a prefix) must be valid for success. If SetString fails,
the value of z is undefined but the returned value is nil.

The base argument must be 0 or a value between 2 and MaxBase.
For base 0, the number prefix determines the actual base: A prefix of
``0b'' or ``0B'' selects base 2, ``0'', ``0o'' or ``0O'' selects base 8,
and ``0x'' or ``0X'' selects base 16. Otherwise, the selected base is 10
and no prefix is accepted.

For bases <= 36, lower and upper case letters are considered the same:
The letters 'a' to 'z' and 'A' to 'Z' represent digit values 10 to 35.
For bases > 36, the upper case letters 'A' to 'Z' represent the digit
values 36 to 61.

For base 0, an underscore character ``_'' may appear between a base
prefix and an adjacent digit, and between successive digits; such
underscores do not change the value of the number.
Incorrect placement of underscores is reported as an error if there
are no other errors. If base != 0, underscores are not recognized
and act like any other character that is not a valid digit.

• #### SetUint64

````([x])`
```

SetUint64 sets z to x and returns z.

• #### Sign

````([])`
```

Sign returns:

-1 if x < 0
0 if x == 0
+1 if x > 0

• #### String

````([])`
```

String returns the decimal representation of x as generated by
x.Text(10).

• #### Text

````([base])`
```

Text returns the string representation of x in the given base.
Base must be between 2 and 62, inclusive. The result uses the
lower-case letters 'a' to 'z' for digit values 10 to 35, and
the upper-case letters 'A' to 'Z' for digit values 36 to 61.
No prefix (such as "0x") is added to the string. If x is a nil
pointer it returns "<nil>".

• #### TrailingZeroBits

````([])`
```

TrailingZeroBits returns the number of consecutive least significant zero
bits of |x|.

• #### Uint64

````([])`
```

Uint64 returns the uint64 representation of x.
If x cannot be represented in a uint64, the result is undefined.

• ### *Rat

Concrete GoType v1.0

• #### Denom

````([])`
```

Denom returns the denominator of x; it is always > 0.
The result is a reference to x's denominator, unless
x is an uninitialized (zero value) Rat, in which case
the result is a new Int of value 1. (To initialize x,
any operation that sets x will do, including x.Set(x).)
If the result is a reference to x's denominator it
may change if a new value is assigned to x, and vice versa.

• #### FloatString

````([prec])`
```

FloatString returns a string representation of x in decimal form with prec
digits of precision after the radix point. The last digit is rounded to
nearest, with halves rounded away from zero.

• #### GobEncode

````([])`
```

GobEncode implements the gob.GobEncoder interface.

• #### IsInt

````([])`
```

IsInt reports whether the denominator of x is 1.

• #### MarshalText

````([])`
```

MarshalText implements the encoding.TextMarshaler interface.

• #### Num

````([])`
```

Num returns the numerator of x; it may be <= 0.
The result is a reference to x's numerator; it
may change if a new value is assigned to x, and vice versa.
The sign of the numerator corresponds to the sign of x.

• #### RatString

````([])`
```

RatString returns a string representation of x in the form "a/b" if b != 1,
and in the form "a" if b == 1.

• #### SetFrac64

````([a b])`
```

SetFrac64 sets z to a/b and returns z.
If b == 0, SetFrac64 panics.

• #### SetInt64

````([x])`
```

SetInt64 sets z to x and returns z.

• #### SetString

````([s])`
```

SetString sets z to the value of s and returns z and a boolean indicating
success. s can be given as a (possibly signed) fraction "a/b", or as a
floating-point number optionally followed by an exponent.
If a fraction is provided, both the dividend and the divisor may be a
decimal integer or independently use a prefix of ``0b'', ``0'' or ``0o'',
or ``0x'' (or their upper-case variants) to denote a binary, octal, or
hexadecimal integer, respectively. The divisor may not be signed.
If a floating-point number is provided, it may be in decimal form or
use any of the same prefixes as above but for ``0'' to denote a non-decimal
mantissa. A leading ``0'' is considered a decimal leading 0; it does not
indicate octal representation in this case.
An optional base-10 ``e'' or base-2 ``p'' (or their upper-case variants)
exponent may be provided as well, except for hexadecimal floats which
only accept an (optional) ``p'' exponent (because an ``e'' or ``E'' cannot
be distinguished from a mantissa digit).
The entire string, not just a prefix, must be valid for success. If the
operation failed, the value of z is undefined but the returned value is nil.

• #### SetUint64

````([x])`
```

SetUint64 sets z to x and returns z.

• #### Sign

````([])`
```

Sign returns:

-1 if x < 0
0 if x == 0
+1 if x > 0

• #### String

````([])`
```

String returns a string representation of x in the form "a/b" (even if b == 1).

• ### *RoundingMode

Concrete GoType v1.0

• ### *Word

Concrete GoType v1.0

• ### Accuracy

Concrete GoType v1.0

Accuracy describes the rounding error produced by the most recent
operation that generated a Float value, relative to the exact value.

• #### String

````([])`
```

• ### ErrNaN

Concrete GoType v1.0

An ErrNaN panic is raised by a Float operation that would lead to
a NaN under IEEE-754 rules. An ErrNaN implements the error interface.

• #### Error

````([])`
```

• ### Float

Concrete GoType v1.0

A nonzero finite Float represents a multi-precision floating point number

sign × mantissa × 2**exponent

with 0.5 <= mantissa < 1.0, and MinExp <= exponent <= MaxExp.
A Float may also be zero (+0, -0) or infinite (+Inf, -Inf).
All Floats are ordered, and the ordering of two Floats x and y
is defined by x.Cmp(y).

Each Float value also has a precision, rounding mode, and accuracy.
The precision is the maximum number of mantissa bits available to
represent the value. The rounding mode specifies how a result should
be rounded to fit into the mantissa bits, and accuracy describes the
rounding error with respect to the exact result.

Unless specified otherwise, all operations (including setters) that
specify a *Float variable for the result (usually via the receiver
with the exception of MantExp), round the numeric result according
to the precision and rounding mode of the result variable.

If the provided result precision is 0 (see below), it is set to the
precision of the argument with the largest precision value before any
rounding takes place, and the rounding mode remains unchanged. Thus,
uninitialized Floats provided as result arguments will have their
precision set to a reasonable value determined by the operands, and
their mode is the zero value for RoundingMode (ToNearestEven).

By setting the desired precision to 24 or 53 and using matching rounding
mode (typically ToNearestEven), Float operations produce the same results
as the corresponding float32 or float64 IEEE-754 arithmetic for operands
that correspond to normal (i.e., not denormal) float32 or float64 numbers.
Exponent underflow and overflow lead to a 0 or an Infinity for different
values than IEEE-754 because Float exponents have a much larger range.

The zero (uninitialized) value for a Float is ready to use and represents
the number +0.0 exactly, with precision 0 and rounding mode ToNearestEven.

Operations always take pointer arguments (*Float) rather
than Float values, and each unique Float value requires
its own unique *Float pointer. To "copy" a Float value,
an existing (or newly allocated) Float must be set to
a new value using the Float.Set method; shallow copies
of Floats are not supported and may lead to errors.

• ### Int

Concrete GoType v1.0

An Int represents a signed multi-precision integer.
The zero value for an Int represents the value 0.

Operations always take pointer arguments (*Int) rather
than Int values, and each unique Int value requires
its own unique *Int pointer. To "copy" an Int value,
an existing (or newly allocated) Int must be set to
a new value using the Int.Set method; shallow copies
of Ints are not supported and may lead to errors.

• ### Rat

Concrete GoType v1.0

A Rat represents a quotient a/b of arbitrary precision.
The zero value for a Rat represents the value 0.

Operations always take pointer arguments (*Rat) rather
than Rat values, and each unique Rat value requires
its own unique *Rat pointer. To "copy" a Rat value,
an existing (or newly allocated) Rat must be set to
a new value using the Rat.Set method; shallow copies
of Rats are not supported and may lead to errors.

• ### RoundingMode

Concrete GoType v1.0

RoundingMode determines how a Float value is rounded to the
desired precision. Rounding may change the Float value; the
rounding error is described by the Float's Accuracy.

• #### String

````([])`