Overview

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

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:

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

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

  1. 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:

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

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

  1. func (z *T) SetV(v V) *T // z = v
  2. func (z *T) Unary(x *T) *T // z = unary x
  3. func (z *T) Binary(x, y *T) *T // z = x binary y
  4. 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).

  1. c.Add(a, b)

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

  1. sum.Add(sum, x)

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:

  1. 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:

  1. func (x *Int) Sign() int

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


Example:

  1. package big_test
  2. import (
  3. "fmt"
  4. "math/big"
  5. )
  6. // Use the classic continued fraction for e
  7. // e = [1; 0, 1, 1, 2, 1, 1, ... 2n, 1, 1, ...]
  8. // i.e., for the nth term, use
  9. // 1 if n mod 3 != 1
  10. // (n-1)/3 * 2 if n mod 3 == 1
  11. func recur(n, lim int64) *big.Rat {
  12. term := new(big.Rat)
  13. if n%3 != 1 {
  14. term.SetInt64(1)
  15. } else {
  16. term.SetInt64((n - 1) / 3 * 2)
  17. }
  18. if n > lim {
  19. return term
  20. }
  21. // Directly initialize frac as the fractional
  22. // inverse of the result of recur.
  23. frac := new(big.Rat).Inv(recur(n+1, lim))
  24. return term.Add(term, frac)
  25. }
  26. // This example demonstrates how to use big.Rat to compute the
  27. // the constant e (base of natural logarithm).
  28. func Example_eConvergents() {
  29. for i := 1; i <= 15; i++ {
  30. r := recur(0, int64(i))
  31. // Print r both as a fraction and as a floating-point number.
  32. // Since big.Rat implements fmt.Formatter, we can use %-13s to
  33. // get a left-aligned string representation of the fraction.
  34. fmt.Printf("%-13s = %s\n", r, r.FloatString(8))
  35. }
  36. // Output:
  37. // 2/1 = 2.00000000
  38. // 3/1 = 3.00000000
  39. // 8/3 = 2.66666667
  40. // 11/4 = 2.75000000
  41. // 19/7 = 2.71428571
  42. // 87/32 = 2.71875000
  43. // 106/39 = 2.71794872
  44. // 193/71 = 2.71830986
  45. // 1264/465 = 2.71827957
  46. // 1457/536 = 2.71828358
  47. // 2721/1001 = 2.71828172
  48. // 23225/8544 = 2.71828184
  49. // 25946/9545 = 2.71828182
  50. // 49171/18089 = 2.71828183
  51. // 517656/190435 = 2.71828183
  52. }


Example:

  1. // Initialize two big ints with the first two numbers in the sequence.
  2. a := big.NewInt(0)
  3. b := big.NewInt(1)
  4. // Initialize limit as 10^99, the smallest integer with 100 digits.
  5. var limit big.Int
  6. limit.Exp(big.NewInt(10), big.NewInt(99), nil)
  7. // Loop while a is smaller than 1e100.
  8. for a.Cmp(&limit) < 0 {
  9. // Compute the next Fibonacci number, storing it in a.
  10. a.Add(a, b)
  11. // Swap a and b so that b is the next number in the sequence.
  12. a, b = b, a
  13. }
  14. fmt.Println(a) // 100-digit Fibonacci number
  15. // Test a for primality.
  16. // (ProbablyPrimes' argument sets the number of Miller-Rabin
  17. // rounds to be performed. 20 is a good value.)
  18. // Output:
  19. // 1344719667586153181419716641724567886890850696275767987106294472017884974410332069524504824747437757
  20. // false


Example:

  1. // We'll do computations with 200 bits of precision in the mantissa.
  2. const prec = 200
  3. // Compute the square root of 2 using Newton's Method. We start with
  4. // an initial estimate for sqrt(2), and then iterate:
  5. // x_{n+1} = 1/2 * ( x_n + (2.0 / x_n) )
  6. // Since Newton's Method doubles the number of correct digits at each
  7. // iteration, we need at least log_2(prec) steps.
  8. steps := int(math.Log2(prec))
  9. // Initialize values we need for the computation.
  10. two := new(big.Float).SetPrec(prec).SetInt64(2)
  11. half := new(big.Float).SetPrec(prec).SetFloat64(0.5)
  12. // Use 1 as the initial estimate.
  13. x := new(big.Float).SetPrec(prec).SetInt64(1)
  14. // We use t as a temporary variable. There's no need to set its precision
  15. // since big.Float values with unset (== 0) precision automatically assume
  16. // the largest precision of the arguments when used as the result (receiver)
  17. // of a big.Float operation.
  18. t := new(big.Float)
  19. // Iterate.
  20. for i := 0; i <= steps; i++ {
  21. t.Quo(two, x) // t = 2.0 / x_n
  22. t.Add(x, t) // t = x_n + (2.0 / x_n)
  23. x.Mul(half, t) // x_{n+1} = 0.5 * t
  24. }
  25. // We can use the usual fmt.Printf verbs since big.Float implements fmt.Formatter
  26. fmt.Printf("sqrt(2) = %.50f\n", x)
  27. // Print the error between 2 and x*x.
  28. t.Mul(x, x) // t = x*x
  29. fmt.Printf("error = %e\n", t.Sub(two, t))
  30. // sqrt(2) = 1.41421356237309504880168872420969807856967187537695
  31. // error = 0.000000e+00

Index

Package files

arith.go decimal.go float.go floatmarsh.go int.go intmarsh.go natconv.go rat.go ratmarsh.go sqrt.go

Constants

  1. const (
  2. MaxExp = .MaxInt32 // largest supported exponent
  3. MinExp = .MinInt32 // smallest supported exponent
  4. MaxPrec = .MaxUint32 // largest (theoretically) supported precision; likely memory-limited
  5. )

Exponent and precision limits.

  1. const MaxBase = 10 + ('z' - 'a' + 1) + ('Z' - 'A' + 1)

MaxBase is the largest number base accepted for string conversions.


  1. func Jacobi(x, y ) int


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

type


  1. type Accuracy


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

  1. const (
    Below Accuracy = -1
    Exact = 0
    Above Accuracy = +1
    )


Constants describing the Accuracy of a Float.

func (Accuracy)


  1. func (i ) String() string



type


  1. type ErrNaN struct {
    // contains filtered or unexported fields
    }


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.

func (ErrNaN)


  1. func (err ) Error() string



type


  1. type Float struct {
    // contains filtered or unexported fields
    }


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.


Example:

// Implement Float “shift” by modifying the (binary) exponents directly.
for s := -5; s <= 5; s++ {
x := big.NewFloat(0.5)
x.SetMantExp(x, x.MantExp(nil)+s) // shift x by s
fmt.Println(x)
}
// Output:
// 0.015625
// 0.03125
// 0.0625
// 0.125
// 0.25
// 0.5
// 1
// 2
// 4
// 8
// 16

func NewFloat


  1. func NewFloat(x float64)


NewFloat allocates and returns a new Float set to x, with precision 53 and
rounding mode ToNearestEven. NewFloat panics with ErrNaN if x is a NaN.

func ParseFloat


  1. func ParseFloat(s string, base , prec uint, mode ) (f Float, b , err error)


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

func (Float)


  1. func (z ) Abs(x Float)


Abs sets z to the (possibly rounded) value |x| (the absolute value of x) and
returns z.

func (Float) Acc


  1. func (x Float) Acc()


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

func (Float) Add


  1. func (z Float) Add(x, y ) Float


Add sets z to the rounded sum x+y and returns z. If z’s precision is 0, it is
changed to the larger of x’s or y’s precision before the operation. Rounding is
performed according to z’s precision and rounding mode; and z’s accuracy reports
the result error relative to the exact (not rounded) result. Add panics with
ErrNaN if x and y are infinities with opposite signs. The value of z is
undefined in that case.

BUG(gri) When rounding ToNegativeInf, the sign of Float values rounded to 0 is
incorrect.


Example:

// Operate on numbers of different precision.
var x, y, z big.Float
x.SetInt64(1000) // x is automatically set to 64bit precision
y.SetFloat64(2.718281828) // y is automatically set to 53bit precision
z.SetPrec(32)
z.Add(&x, &y)
fmt.Printf(“x = %.10g (%s, prec = %d, acc = %s)\n”, &x, x.Text(‘p’, 0), x.Prec(), x.Acc())
fmt.Printf(“y = %.10g (%s, prec = %d, acc = %s)\n”, &y, y.Text(‘p’, 0), y.Prec(), y.Acc())
fmt.Printf(“z = %.10g (%s, prec = %d, acc = %s)\n”, &z, z.Text(‘p’, 0), z.Prec(), z.Acc())
// Output:
// x = 1000 (0x.fap+10, prec = 64, acc = Exact)
// y = 2.718281828 (0x.adf85458248cd8p+2, prec = 53, acc = Exact)
// z = 1002.718282 (0x.faadf854p+10, prec = 32, acc = Below)

func (Float) Append


  1. func (x Float) Append(buf [], fmt byte, prec ) []byte


Append appends to buf the string form of the floating-point number x, as
generated by x.Text, and returns the extended buffer.

func (Float)


  1. func (x ) Cmp(y Float)


Cmp compares x and y and returns:

-1 if x < y
0 if x == y (incl. -0 == 0, -Inf == -Inf, and +Inf == +Inf)
+1 if x > y


Example:

inf := math.Inf(1)
zero := 0.0

operands := []float64{-inf, -1.2, -zero, 0, +1.2, +inf}

fmt.Println(“ x y cmp”)
fmt.Println(“———————-“)
for , x64 := range operands {
x := big.NewFloat(x64)
for
, y64 := range operands {
y := big.NewFloat(y64)
fmt.Printf(“%4g %4g %3d\n”, x, y, x.Cmp(y))
}
fmt.Println()
}

// Output:
// x y cmp
// ———————-
// -Inf -Inf 0
// -Inf -1.2 -1
// -Inf -0 -1
// -Inf 0 -1
// -Inf 1.2 -1
// -Inf +Inf -1
//
// -1.2 -Inf 1
// -1.2 -1.2 0
// -1.2 -0 -1
// -1.2 0 -1
// -1.2 1.2 -1
// -1.2 +Inf -1
//
// -0 -Inf 1
// -0 -1.2 1
// -0 -0 0
// -0 0 0
// -0 1.2 -1
// -0 +Inf -1
//
// 0 -Inf 1
// 0 -1.2 1
// 0 -0 0
// 0 0 0
// 0 1.2 -1
// 0 +Inf -1
//
// 1.2 -Inf 1
// 1.2 -1.2 1
// 1.2 -0 1
// 1.2 0 1
// 1.2 1.2 0
// 1.2 +Inf -1
//
// +Inf -Inf 1
// +Inf -1.2 1
// +Inf -0 1
// +Inf 0 1
// +Inf 1.2 1
// +Inf +Inf 0

func (Float)


  1. func (z ) Copy(x Float)


Copy sets z to x, with the same precision, rounding mode, and accuracy as x, and
returns z. x is not changed even if z and x are the same.

func (Float) Float32


  1. func (x Float) Float32() (, Accuracy)


Float32 returns the float32 value nearest to x. If x is too small to be
represented by a float32 (|x| < math.SmallestNonzeroFloat32), the result is (0,
Below) or (-0, Above), respectively, depending on the sign of x. If x is too
large to be represented by a float32 (|x| > math.MaxFloat32), the result is
(+Inf, Above) or (-Inf, Below), depending on the sign of x.

func (Float)


  1. func (x ) Float64() (float64, )


Float64 returns the float64 value nearest to x. If x is too small to be
represented by a float64 (|x| < math.SmallestNonzeroFloat64), the result is (0,
Below) or (-0, Above), respectively, depending on the sign of x. If x is too
large to be represented by a float64 (|x| > math.MaxFloat64), the result is
(+Inf, Above) or (-Inf, Below), depending on the sign of x.

func (Float) Format


  1. func (x Float) Format(s .State, format )


Format implements fmt.Formatter. It accepts all the regular formats for
floating-point numbers (‘b’, ‘e’, ‘E’, ‘f’, ‘F’, ‘g’, ‘G’) as well as ‘p’ and
‘v’. See (
Float).Text for the interpretation of ‘p’. The ‘v’ format is handled
like ‘g’. Format also supports specification of the minimum precision in digits,
the output field width, as well as the format flags ‘+’ and ‘ ‘ for sign
control, ‘0’ for space or zero padding, and ‘-‘ for left or right justification.
See the fmt package for details.

func (Float) GobDecode


  1. func (z Float) GobDecode(buf []) error


GobDecode implements the gob.GobDecoder interface. The result is rounded per the
precision and rounding mode of z unless z’s precision is 0, in which case z is
set exactly to the decoded value.

func (Float)


  1. func (x ) GobEncode() ([]byte, )


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

func (Float) Int


  1. func (x Float) Int(z ) (Int, )


Int returns the result of truncating x towards zero; or nil if x is an infinity.
The result is Exact if x.IsInt(); otherwise it is Below for x > 0, and Above for
x < 0. If a non-nil Int argument z is provided, Int stores the result in z
instead of allocating a new Int.

func (Float) Int64


  1. func (x Float) Int64() (, Accuracy)


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.

func (Float)


  1. func (x ) IsInf() bool


IsInf reports whether x is +Inf or -Inf.

func (Float)


  1. func (x ) IsInt() bool


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

func (Float)


  1. func (x ) MantExp(mant Float) (exp )


MantExp breaks x into its mantissa and exponent components and returns the
exponent. If a non-nil mant argument is provided its value is set to the
mantissa of x, with the same precision and rounding mode as x. The components
satisfy x == mant × 2exp, with 0.5 <= |mant| < 1.0. Calling MantExp with a nil
argument is an efficient way to get the exponent of the receiver.

Special cases are:

( ±0).MantExp(mant) = 0, with mant set to ±0
(±Inf).MantExp(mant) = 0, with mant set to ±Inf

x and mant may be the same in which case x is set to its mantissa value.

func (Float) MarshalText


  1. func (x Float) MarshalText() (text [], err error)


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

func (Float)


  1. func (x ) MinPrec() uint


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.

func (Float)


  1. func (x ) Mode() RoundingMode


Mode returns the rounding mode of x.

func (Float)


  1. func (z ) Mul(x, y Float)


Mul sets z to the rounded product xy and returns z. Precision, rounding, and
accuracy reporting are as for Add. Mul panics with ErrNaN if one operand is zero
and the other operand an infinity. The value of z is undefined in that case.

func (Float) Neg


  1. func (z Float) Neg(x ) Float


Neg sets z to the (possibly rounded) value of x with its sign negated, and
returns z.

func (Float)


  1. func (z ) Parse(s string, base ) (f Float, b , err error)


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.

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 ] [ prefix ] mantissa [ exponent ] | infinity .
sign = “+” | “-“ .
prefix = “0” ( “x” | “X” | “b” | “B” ) .
mantissa = digits | digits “.” [ digits ] | “.” digits .
exponent = ( “E” | “e” | “p” ) [ sign ] digits .
digits = digit { digit } .
digit = “0” … “9” | “a” … “z” | “A” … “Z” .
infinity = [ sign ] ( “inf” | “Inf” ) .

The base argument must be 0, 2, 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 “0x” or
“0X” selects base 16, and a “0b” or “0B” prefix selects base 2; 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” exponent indicates a binary (rather then decimal) exponent; for instance
“0x1.fffffffffffffp1023” (using base 0) represents the maximum float64 value.
For hexadecimal mantissae, the exponent must be binary, 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.

func (Float)


  1. func (x ) Prec() uint


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

func (Float)




Quo sets z to the rounded quotient x/y and returns z. Precision, rounding, and
accuracy reporting are as for Add. Quo panics with ErrNaN if both operands are
zero or infinities. The value of z is undefined in that case.

func (Float)


  1. func (x ) Rat(z Rat) (, Accuracy)


Rat returns the rational number corresponding to x; or nil if x is an infinity.
The result is Exact if x is not an Inf. If a non-nil
Rat argument z is
provided, Rat stores the result in z instead of allocating a new Rat.

func (Float)


  1. func (z ) Scan(s fmt., ch rune)


Scan is a support routine for fmt.Scanner; it sets z to the value of the scanned
number. It accepts formats whose verbs are supported by fmt.Scan for floating
point values, which are: ‘b’ (binary), ‘e’, ‘E’, ‘f’, ‘F’, ‘g’ and ‘G’. Scan
doesn’t handle ±Inf.


Example:

// The Scan function is rarely used directly;
// the fmt package recognizes it as an implementation of fmt.Scanner.
f := new(big.Float)
_, err := fmt.Sscan(“1.19282e99”, f)
if err != nil {
log.Println(“error scanning value:”, err)
} else {
fmt.Println(f)
}
// Output: 1.19282e+99

func (Float)


  1. func (z ) Set(x Float)


Set sets z to the (possibly rounded) value of x and returns z. If z’s precision
is 0, it is changed to the precision of x before setting z (and rounding will
have no effect). Rounding is performed according to z’s precision and rounding
mode; and z’s accuracy reports the result error relative to the exact (not
rounded) result.

func (Float) SetFloat64


  1. func (z Float) SetFloat64(x ) Float


SetFloat64 sets z to the (possibly rounded) value of x and returns z. If z’s
precision is 0, it is changed to 53 (and rounding will have no effect).
SetFloat64 panics with ErrNaN if x is a NaN.

func (Float)


  1. func (z ) SetInf(signbit bool)


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.

func (Float) SetInt


  1. func (z Float) SetInt(x ) Float


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

func (Float)


  1. func (z ) SetInt64(x int64)


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).

func (Float) SetMantExp


  1. func (z Float) SetMantExp(mant , exp int) *


SetMantExp sets z to mant × 2
exp and and returns z. The result z has the same
precision and rounding mode as mant. SetMantExp is an inverse of MantExp but
does not require 0.5 <= |mant| < 1.0. Specifically:

mant := new(Float)
new(Float).SetMantExp(mant, x.MantExp(mant)).Cmp(x) == 0

Special cases are:

z.SetMantExp( ±0, exp) = ±0
z.SetMantExp(±Inf, exp) = ±Inf

z and mant may be the same in which case z’s exponent is set to exp.

func (Float) SetMode


  1. func (z Float) SetMode(mode ) Float


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.


  1. func (z ) SetPrec(prec uint)


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.

func (Float) SetRat


  1. func (z Float) SetRat(x ) Float


SetRat sets z to the (possibly rounded) value of x and returns z. If z’s
precision is 0, it is changed to the largest of a.BitLen(), b.BitLen(), or 64;
with x = a/b.

func (Float)


  1. func (z ) SetString(s string) (, bool)


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.

func (Float)


  1. func (z ) SetUint64(x uint64)


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).

func (Float) Sign


  1. func (x Float) Sign()


Sign returns:

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

func (Float) Signbit


  1. func (x Float) Signbit()


Signbit returns true if x is negative or negative zero.

func (Float) Sqrt


  1. func (z Float) Sqrt(x ) Float


Sqrt sets z to the rounded square root of x, and returns it.

If z’s precision is 0, it is changed to x’s precision before the operation.
Rounding is performed according to z’s precision and rounding mode.

The function panics if z < 0. The value of z is undefined in that case.

func (Float)


  1. func (x ) String() string


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

func (Float)


  1. func (z ) Sub(x, y Float)


Sub sets z to the rounded difference x-y and returns z. Precision, rounding, and
accuracy reporting are as for Add. Sub panics with ErrNaN if x and y are
infinities with equal signs. The value of z is undefined in that case.

func (Float) Text


  1. func (x Float) Text(format , prec int)


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
‘b’ -ddddddp±dd, binary exponent
‘p’ -0x.dddp±dd, binary exponent, hexadecimal mantissa

For the binary exponent formats, the mantissa is printed in normalized form:

‘b’ decimal integer mantissa using x.Prec() bits, or -0
‘p’ hexadecimal fraction with 0.5 <= 0.mantissa < 1.0, or -0

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’, and ‘G’ formats. For ‘e’, ‘E’, and ‘f’ 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’ or ‘p’ format.

func (Float) Uint64


  1. func (x Float) Uint64() (, Accuracy)


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.

func (Float)


  1. func (z ) UnmarshalText(text []byte)


UnmarshalText implements the encoding.TextUnmarshaler interface. The result is
rounded per the precision and rounding mode of z. If z’s precision is 0, it is
changed to 64 before rounding takes effect.


  1. type Int struct {
    // contains filtered or unexported fields
    }


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

func NewInt


  1. func NewInt(x int64)


NewInt allocates and returns a new Int set to x.

func (Int) Abs


  1. func (z Int) Abs(x ) Int


Abs sets z to |x| (the absolute value of x) and returns z.

func (Int)


  1. func (z ) Add(x, y Int)


Add sets z to the sum x+y and returns z.

func (Int) And


  1. func (z Int) And(x, y ) Int


And sets z = x & y and returns z.

func (Int)


  1. func (z ) AndNot(x, y Int)


AndNot sets z = x &^ y and returns z.

func (Int) Append


  1. func (x Int) Append(buf [], base int) []


Append appends the string representation of x, as generated by x.Text(base), to
buf and returns the extended buffer.

func (Int) Binomial


  1. func (z Int) Binomial(n, k ) Int


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

func (Int)


  1. func (x ) Bit(i int)


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.

func (Int) BitLen


  1. func (x Int) BitLen()


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

func (Int) Bits


  1. func (x Int) 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.

func (Int) Bytes


  1. func (x Int) Bytes() []


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

func (Int) Cmp


  1. func (x Int) Cmp(y ) (r int)


Cmp compares x and y and returns:

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

func (Int)


  1. func (x ) CmpAbs(y Int)


CmpAbs compares the absolute values of x and y and returns:

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

func (Int) Div


  1. func (z Int) Div(x, y ) Int


Div sets z to the quotient x/y for y != 0 and returns z. If y == 0, a
division-by-zero run-time panic occurs. Div implements Euclidean division
(unlike Go); see DivMod for more details.

func (Int)


  1. func (z ) DivMod(x, y, m Int) (, Int)


DivMod sets z to the quotient x div y and m to the modulus x mod y and returns
the pair (z, m) for y != 0. If y == 0, a division-by-zero run-time panic occurs.

DivMod implements Euclidean division and modulus (unlike Go):

q = x div y such that
m = x - y
q with 0 <= m < |y|

(See Raymond T. Boute, The Euclidean definition of the functions div and mod''. ACM Transactions on Programming Languages and Systems (TOPLAS), 14(2):127-144, New York, NY, USA, 4/1992. ACM press.) See QuoRem for T-division and modulus (like Go). <h3 id="Int.Exp">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L439">Exp</a> <a href="#Int.Exp">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) Exp(x, y, m *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z. If y <= 0, the result is 1 mod |m|; if m == nil or m == 0, z = x**y. Modular exponentation of inputs of a particular size is not a cryptographically constant-time operation. <h3 id="Int.Format">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/intconv.go#L53">Format</a> <a href="#Int.Format">¶</a></h3> <pre>func (x *<a href="#Int">Int</a>) Format(s <a href="/fmt/">fmt</a>.<a href="/fmt/#State">State</a>, ch <a href="/builtin/#rune">rune</a>)</pre> Format implements fmt.Formatter. It accepts the formats 'b' (binary), 'o' (octal), 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal). Also supported are the full suite of package fmt's format flags for integral types, including '+' and ' ' for sign control, '#' for leading zero in octal and for hexadecimal, a leading "0x" or "0X" for "%#x" and "%#X" respectively, specification of minimum digits precision, output field width, space or zero padding, and '-' for left or right justification. <h3 id="Int.GCD">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L467">GCD</a> <a href="#Int.GCD">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) GCD(x, y, a, b *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> GCD sets z to the greatest common divisor of a and b, which both must be > 0, and returns z. If x or y are not nil, GCD sets their value such that z = a*x + b*y. If either a or b is <= 0, GCD sets z = x = y = 0. <h3 id="Int.GobDecode">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/intmarsh.go#L23">GobDecode</a> <a href="#Int.GobDecode">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) GobDecode(buf []<a href="/builtin/#byte">byte</a>) <a href="/builtin/#error">error</a></pre> GobDecode implements the gob.GobDecoder interface. <h3 id="Int.GobEncode">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/intmarsh.go#L8">GobEncode</a> <a href="#Int.GobEncode">¶</a></h3> <pre>func (x *<a href="#Int">Int</a>) GobEncode() ([]<a href="/builtin/#byte">byte</a>, <a href="/builtin/#error">error</a>)</pre> GobEncode implements the gob.GobEncoder interface. <h3 id="Int.Int64">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L354">Int64</a> <a href="#Int.Int64">¶</a></h3> <pre>func (x *<a href="#Int">Int</a>) Int64() <a href="/builtin/#int64">int64</a></pre> Int64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined. <h3 id="Int.IsInt64">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L369">IsInt64</a> <a href="#Int.IsInt64">¶</a></h3> <pre>func (x *<a href="#Int">Int</a>) IsInt64() <a href="/builtin/#bool">bool</a></pre> IsInt64 reports whether x can be represented as an int64. <h3 id="Int.IsUint64">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L378">IsUint64</a> <a href="#Int.IsUint64">¶</a></h3> <pre>func (x *<a href="#Int">Int</a>) IsUint64() <a href="/builtin/#bool">bool</a></pre> IsUint64 reports whether x can be represented as a uint64. <h3 id="Int.Lsh">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L813">Lsh</a> <a href="#Int.Lsh">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) Lsh(x *<a href="#Int">Int</a>, n <a href="/builtin/#uint">uint</a>) *<a href="#Int">Int</a></pre> Lsh sets z = x << n and returns z. <h3 id="Int.MarshalJSON">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/intmarsh.go#L59">MarshalJSON</a> <a href="#Int.MarshalJSON">¶</a></h3> <pre>func (x *<a href="#Int">Int</a>) MarshalJSON() ([]<a href="/builtin/#byte">byte</a>, <a href="/builtin/#error">error</a>)</pre> MarshalJSON implements the json.Marshaler interface. <h3 id="Int.MarshalText">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/intmarsh.go#L39">MarshalText</a> <a href="#Int.MarshalText">¶</a></h3> <pre>func (x *<a href="#Int">Int</a>) MarshalText() (text []<a href="/builtin/#byte">byte</a>, err <a href="/builtin/#error">error</a>)</pre> MarshalText implements the encoding.TextMarshaler interface. <h3 id="Int.Mod">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L247">Mod</a> <a href="#Int.Mod">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) Mod(x, y *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> Mod sets z to the modulus x%y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Mod implements Euclidean modulus (unlike Go); see DivMod for more details. <h3 id="Int.ModInverse">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L653">ModInverse</a> <a href="#Int.ModInverse">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) ModInverse(g, n *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> ModInverse sets z to the multiplicative inverse of g in the ring ℤ/nℤ and returns z. If g and n are not relatively prime, the result is undefined. <h3 id="Int.ModSqrt">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L791">ModSqrt</a> <a href="#Int.ModSqrt">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) ModSqrt(x, p *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> ModSqrt sets z to a square root of x mod p if such a square root exists, and returns z. The modulus p must be an odd prime. If x is not a square mod p, ModSqrt leaves z unchanged and returns nil. This function panics if p is not an odd integer. <h3 id="Int.Mul">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L141">Mul</a> <a href="#Int.Mul">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) Mul(x, y *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> Mul sets z to the product x*y and returns z. <h3 id="Int.MulRange">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L159">MulRange</a> <a href="#Int.MulRange">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) MulRange(a, b <a href="/builtin/#int64">int64</a>) *<a href="#Int">Int</a></pre> 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. <h3 id="Int.Neg">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L92">Neg</a> <a href="#Int.Neg">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) Neg(x *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> Neg sets z to -x and returns z. <h3 id="Int.Not">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L1000">Not</a> <a href="#Int.Not">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) Not(x *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> Not sets z = ^x and returns z. <h3 id="Int.Or">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L940">Or</a> <a href="#Int.Or">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) Or(x, y *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> Or sets z = x | y and returns z. <h3 id="Int.ProbablyPrime">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/prime.go#L16">ProbablyPrime</a> <a href="#Int.ProbablyPrime">¶</a></h3> <pre>func (x *<a href="#Int">Int</a>) ProbablyPrime(n <a href="/builtin/#int">int</a>) <a href="/builtin/#bool">bool</a></pre> 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. <h3 id="Int.Quo">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L194">Quo</a> <a href="#Int.Quo">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) Quo(x, y *<a href="#Int">Int</a>) *<a href="#Int">Int</a></pre> Quo sets z to the quotient x/y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Quo implements truncated division (like Go); see QuoRem for more details. <h3 id="Int.QuoRem">func (*Int) <a href="//github.com/golang/go/blob/release-branch.go1.10/src/math/big/int.go#L221">QuoRem</a> <a href="#Int.QuoRem">¶</a></h3> <pre>func (z *<a href="#Int">Int</a>) QuoRem(x, y, r *<a href="#Int">Int</a>) (*<a href="#Int">Int</a>, *<a href="#Int">Int</a>)</pre> QuoRem sets z to the quotient x/y and r to the remainder x%y and returns the pair (z, r) for y != 0. If y == 0, a division-by-zero run-time panic occurs. QuoRem implements T-division and modulus (like Go): q = x/y with the result truncated to zero r = x - y*q (See Daan Leijen,Division and Modulus for Computer Scientists’’.) See DivMod
for Euclidean division and modulus (unlike Go).

func (Int)


  1. func (z ) Rand(rnd rand., n Int)


Rand sets z to a pseudo-random number in [0, n) and returns z.

As this uses the math/rand package, it must not be used for security-sensitive
work. Use crypto/rand.Int instead.

func (Int) Rem


  1. func (z Int) Rem(x, y ) Int


Rem sets z to the remainder x%y for y != 0 and returns z. If y == 0, a
division-by-zero run-time panic occurs. Rem implements truncated modulus (like
Go); see QuoRem for more details.

func (Int)


  1. func (z ) Rsh(x Int, n ) Int


Rsh sets z = x >> n and returns z.

func (Int)


  1. func (z ) Scan(s fmt., ch rune)


Scan is a support routine for fmt.Scanner; it sets z to the value of the scanned
number. It accepts the formats ‘b’ (binary), ‘o’ (octal), ‘d’ (decimal), ‘x’
(lowercase hexadecimal), and ‘X’ (uppercase hexadecimal).


Example:

// The Scan function is rarely used directly;
// the fmt package recognizes it as an implementation of fmt.Scanner.
i := new(big.Int)
_, err := fmt.Sscan(“18446744073709551617”, i)
if err != nil {
log.Println(“error scanning value:”, err)
} else {
fmt.Println(i)
}
// Output: 18446744073709551617

func (Int)


  1. func (z ) Set(x Int)


Set sets z to x and returns z.

func (Int) SetBit


  1. func (z Int) SetBit(x , i int, b ) Int


SetBit sets z to x, with x’s i’th bit set to b (0 or 1). That is, if b is 1
SetBit sets z = x | (1 << i); if b is 0 SetBit sets z = x &^ (1 << i). If b is
not 0 or 1, SetBit will panic.

func (Int)


  1. func (z ) SetBits(abs []Word)


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

func (Int) SetBytes


  1. func (z Int) SetBytes(buf []) Int


SetBytes interprets buf as the bytes of a big-endian unsigned integer, sets z to
that value, and returns z.

func (Int)




SetInt64 sets z to x and returns z.

func (Int)


  1. func (z ) SetString(s string, base ) (Int, )


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. If the base is 0,
the string prefix determines the actual conversion base. A prefix of 0x'' or0X’’ selects base 16; the 0'' prefix selects base 8, and a0b’’ or ``0B’’
prefix selects base 2. Otherwise the selected base is 10.

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.


Example:

i := new(big.Int)
i.SetString(“644”, 8) // octal
fmt.Println(i)
// Output: 420

func (Int)


  1. func (z ) SetUint64(x uint64)


SetUint64 sets z to x and returns z.

func (Int) Sign


  1. func (x Int) Sign()


Sign returns:

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


  1. func (z Int) Sqrt(x ) Int


Sqrt sets z to ⌊√x⌋, the largest integer such that z² ≤ x, and returns z. It
panics if x is negative.

func (Int)


  1. func (x ) String() string



func (Int)


  1. func (z ) Sub(x, y Int)


Sub sets z to the difference x-y and returns z.

func (Int) Text


  1. func (x Int) Text(base ) string


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.

func (Int)


  1. func (x ) Uint64() uint64


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

func (Int)


  1. func (z ) UnmarshalJSON(text []byte)


UnmarshalJSON implements the json.Unmarshaler interface.

func (Int) UnmarshalText


  1. func (z Int) UnmarshalText(text []) error


UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Int)


  1. func (z ) Xor(x, y Int)


Xor sets z = x ^ y and returns z.

type Rat


  1. type Rat struct {
    // contains filtered or unexported fields
    }


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

func NewRat


  1. func NewRat(a, b int64)


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

func (Rat) Abs


  1. func (z Rat) Abs(x ) Rat


Abs sets z to |x| (the absolute value of x) and returns z.

func (Rat)


  1. func (z ) Add(x, y Rat)


Add sets z to the sum x+y and returns z.

func (Rat) Cmp


  1. func (x Rat) Cmp(y ) int


Cmp compares x and y and returns:

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

func (Rat)


  1. func (x ) Denom() Int


Denom returns the denominator of x; it is always > 0. The result is a reference
to x’s denominator; it may change if a new value is assigned to x, and vice
versa.

func (Rat)


  1. func (x ) Float32() (f float32, exact )


Float32 returns the nearest float32 value for x and a bool indicating whether f
represents x exactly. If the magnitude of x is too large to be represented by a
float32, f is an infinity and exact is false. The sign of f always matches the
sign of x, even if f == 0.

func (Rat) Float64


  1. func (x Rat) Float64() (f , exact bool)


Float64 returns the nearest float64 value for x and a bool indicating whether f
represents x exactly. If the magnitude of x is too large to be represented by a
float64, f is an infinity and exact is false. The sign of f always matches the
sign of x, even if f == 0.

func (Rat)


  1. func (x ) FloatString(prec int)


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

func (Rat) GobDecode


  1. func (z Rat) GobDecode(buf []) error


GobDecode implements the gob.GobDecoder interface.

func (Rat)


  1. func (x ) GobEncode() ([]byte, )


GobEncode implements the gob.GobEncoder interface.

func (Rat) Inv


  1. func (z Rat) Inv(x ) Rat


Inv sets z to 1/x and returns z.

func (Rat)


  1. func (x ) IsInt() bool


IsInt reports whether the denominator of x is 1.

func (Rat)


  1. func (x ) MarshalText() (text []byte, err )


MarshalText implements the encoding.TextMarshaler interface.

func (Rat) Mul


  1. func (z Rat) Mul(x, y ) Rat


Mul sets z to the product x
y and returns z.

func (Rat)


  1. func (z ) Neg(x Rat)


Neg sets z to -x and returns z.

func (Rat) Num


  1. func (x Rat) 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.

func (Rat) Quo


  1. func (z Rat) Quo(x, y ) Rat


Quo sets z to the quotient x/y and returns z. If y == 0, a division-by-zero
run-time panic occurs.

func (Rat)


  1. func (x ) RatString() string


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

func (Rat)


  1. func (z ) Scan(s fmt., ch rune)


Scan is a support routine for fmt.Scanner. It accepts the formats ‘e’, ‘E’, ‘f’,
‘F’, ‘g’, ‘G’, and ‘v’. All formats are equivalent.


Example:

// The Scan function is rarely used directly;
// the fmt package recognizes it as an implementation of fmt.Scanner.
r := new(big.Rat)
_, err := fmt.Sscan(“1.5000”, r)
if err != nil {
log.Println(“error scanning value:”, err)
} else {
fmt.Println(r)
}
// Output: 3/2

func (Rat)


  1. func (z ) Set(x Rat)


Set sets z to x (by making a copy of x) and returns z.

func (Rat) SetFloat64


  1. func (z Rat) SetFloat64(f ) Rat


SetFloat64 sets z to exactly f and returns z. If f is not finite, SetFloat
returns nil.

func (Rat)


  1. func (z ) SetFrac(a, b Int)


SetFrac sets z to a/b and returns z.

func (Rat) SetFrac64


  1. func (z Rat) SetFrac64(a, b ) Rat


SetFrac64 sets z to a/b and returns z.

func (Rat)


  1. func (z ) SetInt(x Int)


SetInt sets z to x (by making a copy of x) and returns z.

func (Rat) SetInt64


  1. func (z Rat) SetInt64(x ) Rat


SetInt64 sets z to x and returns z.

func (Rat)


  1. func (z ) SetString(s string) (, bool)


SetString sets z to the value of s and returns z and a boolean indicating
success. s can be given as a fraction “a/b” or as a floating-point number
optionally followed by an exponent. 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.


Example:

r := new(big.Rat)
r.SetString(“355/113”)
fmt.Println(r.FloatString(3))
// Output: 3.142

func (Rat) Sign


  1. func (x Rat) Sign()


Sign returns:

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

func (Rat) String


  1. func (x Rat) String()


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

func (Rat) Sub


  1. func (z Rat) Sub(x, y ) Rat


Sub sets z to the difference x-y and returns z.

func (Rat)


  1. func (z ) UnmarshalText(text []byte)


UnmarshalText implements the encoding.TextUnmarshaler interface.

type RoundingMode


  1. type RoundingMode byte


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.

  1. const (
    ToNearestEven = iota // == IEEE 754-2008 roundTiesToEven
    ToNearestAway // == IEEE 754-2008 roundTiesToAway
    ToZero // == IEEE 754-2008 roundTowardZero
    AwayFromZero // no IEEE 754-2008 equivalent
    ToNegativeInf // == IEEE 754-2008 roundTowardNegative
    ToPositiveInf // == IEEE 754-2008 roundTowardPositive
    )


These constants define supported rounding modes.


Example:

operands := []float64{2.6, 2.5, 2.1, -2.1, -2.5, -2.6}

fmt.Print(“ x”)
for mode := big.ToNearestEven; mode <= big.ToPositiveInf; mode++ {
fmt.Printf(“ %s”, mode)
}
fmt.Println()

for _, f64 := range operands {
fmt.Printf(“%4g”, f64)
for mode := big.ToNearestEven; mode <= big.ToPositiveInf; mode++ {
// sample operands above require 2 bits to represent mantissa
// set binary precision to 2 to round them to integer values
f := new(big.Float).SetPrec(2).SetMode(mode).SetFloat64(f64)
fmt.Printf(“ %
g”, len(mode.String()), f)
}
fmt.Println()
}

// Output:
// x ToNearestEven ToNearestAway ToZero AwayFromZero ToNegativeInf ToPositiveInf
// 2.6 3 3 2 3 2 3
// 2.5 2 3 2 3 2 3
// 2.1 2 2 2 3 2 3
// -2.1 -2 -2 -2 -3 -3 -2
// -2.5 -2 -3 -2 -3 -3 -2
// -2.6 -3 -3 -2 -3 -3 -2

func (RoundingMode) String


  1. func (i RoundingMode) String()



type Word




A Word represents a single digit of a multi-precision unsigned integer.