# package big
`import "math/big"`
big包實現了大數字的多精度計算。?支持如下數字類型:
```
- Int 有符號整數
- Rat 有理數
```
方法一般為如下格式:
```
func (z *Int) Op(x, y *Int) *Int (similar for *Rat)
```
該方法實現了操作z = x Op y:計算并將結果寫入z。如果結果是操作數之一,可能會重寫該參數(重用其內存);為了實現鏈式的計算,計算結果同時會作為返回值。方法返回一個結果而不是讓\*Int/\*Rat調用方法獲取另一個操作數。
## Index
* [Constants](#pkg-constants)
* [type Word](#Word)
* [type Int](#Int)
* [func NewInt(x int64) \*Int](#NewInt)
* [func (x \*Int) Int64() int64](#Int.Int64)
* [func (x \*Int) Uint64() uint64](#Int.Uint64)
* [func (x \*Int) Bytes() []byte](#Int.Bytes)
* [func (x \*Int) String() string](#Int.String)
* [func (x \*Int) BitLen() int](#Int.BitLen)
* [func (x \*Int) Bits() []Word](#Int.Bits)
* [func (x \*Int) Bit(i int) uint](#Int.Bit)
* [func (z \*Int) SetInt64(x int64) \*Int](#Int.SetInt64)
* [func (z \*Int) SetUint64(x uint64) \*Int](#Int.SetUint64)
* [func (z \*Int) SetBytes(buf []byte) \*Int](#Int.SetBytes)
* [func (z \*Int) SetString(s string, base int) (\*Int, bool)](#Int.SetString)
* [func (z \*Int) SetBits(abs []Word) \*Int](#Int.SetBits)
* [func (z \*Int) SetBit(x \*Int, i int, b uint) \*Int](#Int.SetBit)
* [func (z \*Int) MulRange(a, b int64) \*Int](#Int.MulRange)
* [func (z \*Int) Binomial(n, k int64) \*Int](#Int.Binomial)
* [func (z \*Int) Rand(rnd \*rand.Rand, n \*Int) \*Int](#Int.Rand)
* [func (x \*Int) ProbablyPrime(n int) bool](#Int.ProbablyPrime)
* [func (x \*Int) Sign() int](#Int.Sign)
* [func (x \*Int) Cmp(y \*Int) (r int)](#Int.Cmp)
* [func (z \*Int) Not(x \*Int) \*Int](#Int.Not)
* [func (z \*Int) And(x, y \*Int) \*Int](#Int.And)
* [func (z \*Int) Or(x, y \*Int) \*Int](#Int.Or)
* [func (z \*Int) Xor(x, y \*Int) \*Int](#Int.Xor)
* [func (z \*Int) AndNot(x, y \*Int) \*Int](#Int.AndNot)
* [func (z \*Int) Lsh(x \*Int, n uint) \*Int](#Int.Lsh)
* [func (z \*Int) Rsh(x \*Int, n uint) \*Int](#Int.Rsh)
* [func (z \*Int) Abs(x \*Int) \*Int](#Int.Abs)
* [func (z \*Int) Neg(x \*Int) \*Int](#Int.Neg)
* [func (z \*Int) Set(x \*Int) \*Int](#Int.Set)
* [func (z \*Int) Add(x, y \*Int) \*Int](#Int.Add)
* [func (z \*Int) Sub(x, y \*Int) \*Int](#Int.Sub)
* [func (z \*Int) Mul(x, y \*Int) \*Int](#Int.Mul)
* [func (z \*Int) Div(x, y \*Int) \*Int](#Int.Div)
* [func (z \*Int) Mod(x, y \*Int) \*Int](#Int.Mod)
* [func (z \*Int) DivMod(x, y, m \*Int) (\*Int, \*Int)](#Int.DivMod)
* [func (z \*Int) Quo(x, y \*Int) \*Int](#Int.Quo)
* [func (z \*Int) Rem(x, y \*Int) \*Int](#Int.Rem)
* [func (z \*Int) QuoRem(x, y, r \*Int) (\*Int, \*Int)](#Int.QuoRem)
* [func (z \*Int) ModInverse(g, p \*Int) \*Int](#Int.ModInverse)
* [func (z \*Int) Exp(x, y, m \*Int) \*Int](#Int.Exp)
* [func (z \*Int) GCD(x, y, a, b \*Int) \*Int](#Int.GCD)
* [func (x \*Int) Format(s fmt.State, ch rune)](#Int.Format)
* [func (z \*Int) Scan(s fmt.ScanState, ch rune) error](#Int.Scan)
* [func (x \*Int) GobEncode() ([]byte, error)](#Int.GobEncode)
* [func (z \*Int) GobDecode(buf []byte) error](#Int.GobDecode)
* [func (z \*Int) MarshalJSON() ([]byte, error)](#Int.MarshalJSON)
* [func (z \*Int) UnmarshalJSON(text []byte) error](#Int.UnmarshalJSON)
* [func (z \*Int) MarshalText() (text []byte, err error)](#Int.MarshalText)
* [func (z \*Int) UnmarshalText(text []byte) error](#Int.UnmarshalText)
* [type Rat](#Rat)
* [func NewRat(a, b int64) \*Rat](#NewRat)
* [func (x \*Rat) Num() \*Int](#Rat.Num)
* [func (x \*Rat) Denom() \*Int](#Rat.Denom)
* [func (x \*Rat) Float64() (f float64, exact bool)](#Rat.Float64)
* [func (x \*Rat) RatString() string](#Rat.RatString)
* [func (x \*Rat) FloatString(prec int) string](#Rat.FloatString)
* [func (x \*Rat) String() string](#Rat.String)
* [func (x \*Rat) IsInt() bool](#Rat.IsInt)
* [func (z \*Rat) SetInt64(x int64) \*Rat](#Rat.SetInt64)
* [func (z \*Rat) SetFrac64(a, b int64) \*Rat](#Rat.SetFrac64)
* [func (z \*Rat) SetFloat64(f float64) \*Rat](#Rat.SetFloat64)
* [func (z \*Rat) SetInt(x \*Int) \*Rat](#Rat.SetInt)
* [func (z \*Rat) SetFrac(a, b \*Int) \*Rat](#Rat.SetFrac)
* [func (z \*Rat) SetString(s string) (\*Rat, bool)](#Rat.SetString)
* [func (x \*Rat) Sign() int](#Rat.Sign)
* [func (x \*Rat) Cmp(y \*Rat) int](#Rat.Cmp)
* [func (z \*Rat) Abs(x \*Rat) \*Rat](#Rat.Abs)
* [func (z \*Rat) Neg(x \*Rat) \*Rat](#Rat.Neg)
* [func (z \*Rat) Inv(x \*Rat) \*Rat](#Rat.Inv)
* [func (z \*Rat) Set(x \*Rat) \*Rat](#Rat.Set)
* [func (z \*Rat) Add(x, y \*Rat) \*Rat](#Rat.Add)
* [func (z \*Rat) Sub(x, y \*Rat) \*Rat](#Rat.Sub)
* [func (z \*Rat) Mul(x, y \*Rat) \*Rat](#Rat.Mul)
* [func (z \*Rat) Quo(x, y \*Rat) \*Rat](#Rat.Quo)
* [func (z \*Rat) Scan(s fmt.ScanState, ch rune) error](#Rat.Scan)
* [func (x \*Rat) GobEncode() ([]byte, error)](#Rat.GobEncode)
* [func (z \*Rat) GobDecode(buf []byte) error](#Rat.GobDecode)
* [func (r \*Rat) MarshalText() (text []byte, err error)](#Rat.MarshalText)
* [func (r \*Rat) UnmarshalText(text []byte) error](#Rat.UnmarshalText)
### Examples
* [Int.Scan](#example-Int-Scan)
* [Int.SetString](#example-Int-SetString)
* [Rat.Scan](#example-Rat-Scan)
* [Rat.SetString](#example-Rat-SetString)
## Constants
```
const MaxBase = 'z' - 'a' + 10 + 1 // = hexValue('z') + 1
```
MaxBase是字符串轉換函數接受的最大進制。
## type [Word](https://github.com/golang/go/blob/master/src/math/big/arith.go#L12 "View Source")
```
type Word uintptr
```
Word代表一個多精度無符號整數的單個數字。
## type [Int](https://github.com/golang/go/blob/master/src/math/big/int.go#L19 "View Source")
```
type Int struct {
// 內含隱藏或非導出字段
}
```
Int類型代表多精度的整數,零值代表數字0。
### func [NewInt](https://github.com/golang/go/blob/master/src/math/big/int.go#L62 "View Source")
```
func NewInt(x int64) *Int
```
創建一個值為x的\*Int。
### func (\*Int) [Int64](https://github.com/golang/go/blob/master/src/math/big/int.go#L515 "View Source")
```
func (x *Int) Int64() int64
```
返回x的int64表示,如果不能用int64表示,結果是未定義的。
### func (\*Int) [Uint64](https://github.com/golang/go/blob/master/src/math/big/int.go#L525 "View Source")
```
func (x *Int) Uint64() uint64
```
返回x的uint64表示,如果不能用uint64表示,結果是未定義的。
### func (\*Int) [Bytes](https://github.com/golang/go/blob/master/src/math/big/int.go#L567 "View Source")
```
func (x *Int) Bytes() []byte
```
返回x的絕對值的大端在前的字節切片表示。
### func (\*Int) [String](https://github.com/golang/go/blob/master/src/math/big/int.go#L324 "View Source")
```
func (x *Int) String() string
```
### func (\*Int) [BitLen](https://github.com/golang/go/blob/master/src/math/big/int.go#L574 "View Source")
```
func (x *Int) BitLen() int
```
返回x的絕對值的字位數,0的字位數為0。
### func (\*Int) [Bits](https://github.com/golang/go/blob/master/src/math/big/int.go#L80 "View Source")
```
func (x *Int) Bits() []Word
```
提供了對x的數據不檢查而快速的訪問,返回構成x的絕對值的小端在前的word切片。該切片與x的底層是同一個數組,本函數用于支持在包外實現缺少的低水平功能,否則不應被使用。
### func (\*Int) [Bit](https://github.com/golang/go/blob/master/src/math/big/int.go#L774 "View Source")
```
func (x *Int) Bit(i int) uint
```
返回第i個字位的值,即返回(x>>i)&1。i必須不小于0。
### func (\*Int) [SetInt64](https://github.com/golang/go/blob/master/src/math/big/int.go#L43 "View Source")
```
func (z *Int) SetInt64(x int64) *Int
```
將z設為x并返回z。
### func (\*Int) [SetUint64](https://github.com/golang/go/blob/master/src/math/big/int.go#L55 "View Source")
```
func (z *Int) SetUint64(x uint64) *Int
```
將z設為x并返回z。
### func (\*Int) [SetBytes](https://github.com/golang/go/blob/master/src/math/big/int.go#L560 "View Source")
```
func (z *Int) SetBytes(buf []byte) *Int
```
將buf視為一個大端在前的無符號整數,將z設為該值,并返回z。
### func (\*Int) [SetString](https://github.com/golang/go/blob/master/src/math/big/int.go#L545 "View Source")
```
func (z *Int) SetString(s string, base int) (*Int, bool)
```
將z設為s代表的值(base為基數)。返回z并用一個bool來表明成功與否。如果失敗,z的值是不確定的,但返回值為nil。基數必須是0或者2到MaxBase之間的整數。如果基數為0,字符串的前綴決定實際的轉換基數:"0x"、"0X"表示十六進制;"0b"、"0B"表示二進制;"0"表示八進制;否則為十進制。
Example
```
i := new(big.Int)
i.SetString("644", 8) // octal
fmt.Println(i)
```
Output:
```
420
```
### func (\*Int) [SetBits](https://github.com/golang/go/blob/master/src/math/big/int.go#L89 "View Source")
```
func (z *Int) SetBits(abs []Word) *Int
```
提供了對z的數據不檢查而快速的操作,將abs視為小端在前的word切片并直接賦給z,返回z。會將z的底層設置為abs的同一底層數組,本函數用于支持在包外實現缺少的低水平功能,否則不應被使用。
### func (\*Int) [SetBit](https://github.com/golang/go/blob/master/src/math/big/int.go#L797 "View Source")
```
func (z *Int) SetBit(x *Int, i int, b uint) *Int
```
將z設為x并設置z的第i位為b,返回z。如b為1,z = x | (1 << i);如b為0,z = x & ^(1 << i);否則會panic。
### func (\*Int) [MulRange](https://github.com/golang/go/blob/master/src/math/big/int.go#L165 "View Source")
```
func (z *Int) MulRange(a, b int64) *Int
```
將z設置為區間[a, b]內所有整數的乘積A(a, b),并返回z。如果a>b會將z設為1并返回。
### func (\*Int) [Binomial](https://github.com/golang/go/blob/master/src/math/big/int.go#L186 "View Source")
```
func (z *Int) Binomial(n, k int64) *Int
```
將z設為k次二項式展開第n項的系數C(n, k),并返回z。
### func (\*Int) [Rand](https://github.com/golang/go/blob/master/src/math/big/int.go#L726 "View Source")
```
func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int
```
將z設為一個范圍在[0, n)的偽隨機值,并返回z。
### func (\*Int) [ProbablyPrime](https://github.com/golang/go/blob/master/src/math/big/int.go#L721 "View Source")
```
func (x *Int) ProbablyPrime(n int) bool
```
對x進行n次Miller-Rabin質數檢測。如果方法返回真則x是質數的幾率為1-(1/4)\*\*n;否則x不是質數。
### func (\*Int) [Sign](https://github.com/golang/go/blob/master/src/math/big/int.go#L32 "View Source")
```
func (x *Int) Sign() int
```
返回x的正負號。x<0時返回-1;x>0時返回+1;否則返回0。
### func (\*Int) [Cmp](https://github.com/golang/go/blob/master/src/math/big/int.go#L305 "View Source")
```
func (x *Int) Cmp(y *Int) (r int)
```
比較x和y的大小。x<y時返回-1;x>y時返回+1;否則返回0。
### func (\*Int) [Not](https://github.com/golang/go/blob/master/src/math/big/int.go#L937 "View Source")
```
func (z *Int) Not(x *Int) *Int
```
將z設為^x并返回z(按位取反)。
### func (\*Int) [And](https://github.com/golang/go/blob/master/src/math/big/int.go#L814 "View Source")
```
func (z *Int) And(x, y *Int) *Int
```
將z設為x & y并返回z(按位且)。
### func (\*Int) [Or](https://github.com/golang/go/blob/master/src/math/big/int.go#L877 "View Source")
```
func (z *Int) Or(x, y *Int) *Int
```
將z設為x | y并返回z(按位或)。
### func (\*Int) [Xor](https://github.com/golang/go/blob/master/src/math/big/int.go#L907 "View Source")
```
func (z *Int) Xor(x, y *Int) *Int
```
將z設為x ^ y并返回z(按位異或)。
### func (\*Int) [AndNot](https://github.com/golang/go/blob/master/src/math/big/int.go#L844 "View Source")
```
func (z *Int) AndNot(x, y *Int) *Int
```
將z設為x & (^y)并返回z(按位減)。
### func (\*Int) [Lsh](https://github.com/golang/go/blob/master/src/math/big/int.go#L750 "View Source")
```
func (z *Int) Lsh(x *Int, n uint) *Int
```
將z設為x << n并返回z(左位移運算)。
### func (\*Int) [Rsh](https://github.com/golang/go/blob/master/src/math/big/int.go#L757 "View Source")
```
func (z *Int) Rsh(x *Int, n uint) *Int
```
將z設為x >> n并返回z(右位移運算)。
### func (\*Int) [Abs](https://github.com/golang/go/blob/master/src/math/big/int.go#L96 "View Source")
```
func (z *Int) Abs(x *Int) *Int
```
將z設為|x|并返回z。
### func (\*Int) [Neg](https://github.com/golang/go/blob/master/src/math/big/int.go#L103 "View Source")
```
func (z *Int) Neg(x *Int) *Int
```
將z設為-x并返回z。
### func (\*Int) [Set](https://github.com/golang/go/blob/master/src/math/big/int.go#L67 "View Source")
```
func (z *Int) Set(x *Int) *Int
```
將z設為x(生成一個拷貝)并返回z
### func (\*Int) [Add](https://github.com/golang/go/blob/master/src/math/big/int.go#L110 "View Source")
```
func (z *Int) Add(x, y *Int) *Int
```
將z設為x + y并返回z。
### func (\*Int) [Sub](https://github.com/golang/go/blob/master/src/math/big/int.go#L131 "View Source")
```
func (z *Int) Sub(x, y *Int) *Int
```
將z設為x - y并返回z。
### func (\*Int) [Mul](https://github.com/golang/go/blob/master/src/math/big/int.go#L152 "View Source")
```
func (z *Int) Mul(x, y *Int) *Int
```
將z設為x * y并返回z。
### func (\*Int) [Div](https://github.com/golang/go/blob/master/src/math/big/int.go#L232 "View Source")
```
func (z *Int) Div(x, y *Int) *Int
```
如果y != 0會將z設為x/y并返回z;如果y==0會panic。函數采用歐幾里德除法(和Go不同),參見DivMod。
### func (\*Int) [Mod](https://github.com/golang/go/blob/master/src/math/big/int.go#L249 "View Source")
```
func (z *Int) Mod(x, y *Int) *Int
```
如果y != 0會將z設為x%y并返回z;如果y==0會panic。函數采用歐幾里德除法(和Go不同),參見DivMod。
### func (\*Int) [DivMod](https://github.com/golang/go/blob/master/src/math/big/int.go#L281 "View Source")
```
func (z *Int) DivMod(x, y, m *Int) (*Int, *Int)
```
如果y != 0將z設為x/y,將m設為x%y并返回(z, m);如果y == 0會panic。采用歐幾里德除法(和Go不同)
DivMod方法實現了歐幾里德帶余除法:
```
q = x div y 滿足
m = x - y*q 且 0 <= m < |q|
```
### func (\*Int) [Quo](https://github.com/golang/go/blob/master/src/math/big/int.go#L196 "View Source")
```
func (z *Int) Quo(x, y *Int) *Int
```
如果y != 0會將z設為x/y并返回z;如果y==0會panic。函數采用截斷除法(和Go相同),參見QuoRem。
### func (\*Int) [Rem](https://github.com/golang/go/blob/master/src/math/big/int.go#L205 "View Source")
```
func (z *Int) Rem(x, y *Int) *Int
```
如果y != 0會將z設為x%y并返回z;如果y==0會panic。函數采用截斷除法(和Go相同),參見QuoRem。
### func (\*Int) [QuoRem](https://github.com/golang/go/blob/master/src/math/big/int.go#L223 "View Source")
```
func (z *Int) QuoRem(x, y, r *Int) (*Int, *Int)
```
如果y != 0將z設為x/y,將r設為x%y并返回(z, r);如果y == 0會panic。函數采用截斷除法(和Go相同)
QuoRem方法實現了截斷帶余除法:
```
q = x/y 返回值向零的方向截斷
r = x - y*q
```
### func (\*Int) [ModInverse](https://github.com/golang/go/blob/master/src/math/big/int.go#L738 "View Source")
```
func (z *Int) ModInverse(g, p *Int) *Int
```
將z設為g相對p的模逆(即z、g滿足(z * g) % p == 1)。返回值z大于0小于p。
### func (\*Int) [Exp](https://github.com/golang/go/blob/master/src/math/big/int.go#L581 "View Source")
```
func (z *Int) Exp(x, y, m *Int) *Int
```
將z設為x\*\*y mod |m|并返回z;如果y <= 0,返回1;如果m == nil?或?m == 0,z設為x\*\*y。
### func (\*Int) [GCD](https://github.com/golang/go/blob/master/src/math/big/int.go#L602 "View Source")
```
func (z *Int) GCD(x, y, a, b *Int) *Int
```
將z設為a和b的最大公約數并返回z。a或b為nil時會panic;a和b都>0時設置z為最大公約數;如果任一個<=0方法就會設置z = x = y = 0。如果x和y都不是nil,會將x和y設置為滿足a\*x + b\*y==z。
### func (\*Int) [Format](https://github.com/golang/go/blob/master/src/math/big/int.go#L371 "View Source")
```
func (x *Int) Format(s fmt.State, ch rune)
```
Format方法實現了fmt.Formatter接口。本方法接受格式'b'(二進制)、'o'(八進制)、'd'(十進制)、'x'(小寫十六進制)、'X'(大寫十六進制)。
方法支持全套fmt包對整數類型的動作:包括用于符號控制的'+'、'-'、' ';用于十六進制和八進制前導0的'#';"%#x"和"%#X"會設置前導的"0x"或"0X";指定最小數字精度;輸出字段寬度;空格或'0'的補位;左右對齊。
### func (\*Int) [Scan](https://github.com/golang/go/blob/master/src/math/big/int.go#L492 "View Source")
```
func (z *Int) Scan(s fmt.ScanState, ch rune) error
```
Scan實現了fmt.Scanner接口,將z設為讀取的數字。方法可以接受接受格式'b'(二進制)、'o'(八進制)、'd'(十進制)、'x'(小寫十六進制)、'X'(大寫十六進制)。
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) [GobEncode](https://github.com/golang/go/blob/master/src/math/big/int.go#L955 "View Source")
```
func (x *Int) GobEncode() ([]byte, error)
```
本方法實現了gob.GobEncoder接口。
### func (\*Int) [GobDecode](https://github.com/golang/go/blob/master/src/math/big/int.go#L970 "View Source")
```
func (z *Int) GobDecode(buf []byte) error
```
本方法實現了gob.GobDecoder接口。
### func (\*Int) [MarshalJSON](https://github.com/golang/go/blob/master/src/math/big/int.go#L986 "View Source")
```
func (z *Int) MarshalJSON() ([]byte, error)
```
本方法實現了json.Marshaler接口。
### func (\*Int) [UnmarshalJSON](https://github.com/golang/go/blob/master/src/math/big/int.go#L992 "View Source")
```
func (z *Int) UnmarshalJSON(text []byte) error
```
本方法實現了json.Unmarshaler接口。
### func (\*Int) [MarshalText](https://github.com/golang/go/blob/master/src/math/big/int.go#L1001 "View Source")
```
func (z *Int) MarshalText() (text []byte, err error)
```
本方法實現了encoding.TextMarshaler接口。
### func (\*Int) [UnmarshalText](https://github.com/golang/go/blob/master/src/math/big/int.go#L1006 "View Source")
```
func (z *Int) UnmarshalText(text []byte) error
```
本方法實現了encoding.TextUnmarshaler接口。
## type [Rat](https://github.com/golang/go/blob/master/src/math/big/rat.go#L19 "View Source")
```
type Rat struct {
// 內含隱藏或非導出字段
}
```
Rat類型代表一個任意精度的有理數(底層采用分數表示),Rat的零值代表數字0。
### func [NewRat](https://github.com/golang/go/blob/master/src/math/big/rat.go#L27 "View Source")
```
func NewRat(a, b int64) *Rat
```
NewRat函數使用分子a和分母b創建一個Rat。
### func (\*Rat) [Num](https://github.com/golang/go/blob/master/src/math/big/rat.go#L285 "View Source")
```
func (x *Rat) Num() *Int
```
返回x的分子,分子可能<=0。返回的是x分子的指針,因此對返回值的操作可能改變x,反之亦然。x的符號與分子的符號是綁定的。
### func (\*Rat) [Denom](https://github.com/golang/go/blob/master/src/math/big/rat.go#L292 "View Source")
```
func (x *Rat) Denom() *Int
```
返回x的分母,分母總是>0。返回的是x分母的指針,因此對返回值的操作可能改變x,反之亦然。
### func (\*Rat) [Float64](https://github.com/golang/go/blob/master/src/math/big/rat.go#L170 "View Source")
```
func (x *Rat) Float64() (f float64, exact bool)
```
返回最接近x的值的float64值,exact用于說明f是否精確的表示了x。??如果x的量級太大或太小不能被float64類型表示,返回無窮和false;f的符號始終與x的符號一致,即使f==0。
### func (\*Rat) [RatString](https://github.com/golang/go/blob/master/src/math/big/rat.go#L491 "View Source")
```
func (x *Rat) RatString() string
```
返回z的字符串表示,如果分母不等于1,格式為"a/b";否則格式為"a"。
### func (\*Rat) [FloatString](https://github.com/golang/go/blob/master/src/math/big/rat.go#L500 "View Source")
```
func (x *Rat) FloatString(prec int) string
```
返回z的字符串表示為精度為prec的十進制浮點數,最后一位會進行四舍五入。
### func (\*Rat) [String](https://github.com/golang/go/blob/master/src/math/big/rat.go#L481 "View Source")
```
func (x *Rat) String() string
```
返回z的字符串表示,格式為"a/b"(即使分母等于1)。
### func (\*Rat) [IsInt](https://github.com/golang/go/blob/master/src/math/big/rat.go#L277 "View Source")
```
func (x *Rat) IsInt() bool
```
返回x的分母是否為1(即x為整數)。
### func (\*Rat) [SetInt64](https://github.com/golang/go/blob/master/src/math/big/rat.go#L219 "View Source")
```
func (z *Rat) SetInt64(x int64) *Rat
```
將z設為x,并返回z。
### func (\*Rat) [SetFrac64](https://github.com/golang/go/blob/master/src/math/big/rat.go#L198 "View Source")
```
func (z *Rat) SetFrac64(a, b int64) *Rat
```
將z設為a/b,并返回z。
### func (\*Rat) [SetFloat64](https://github.com/golang/go/blob/master/src/math/big/rat.go#L33 "View Source")
```
func (z *Rat) SetFloat64(f float64) *Rat
```
將z設為f的精確值并返回z。如果f不是有窮數(即f為+Inf、+Inf或NaN)時會返回nil。。
### func (\*Rat) [SetInt](https://github.com/golang/go/blob/master/src/math/big/rat.go#L212 "View Source")
```
func (z *Rat) SetInt(x *Int) *Rat
```
將z設為x(生成一個拷貝)并返回z。
### func (\*Rat) [SetFrac](https://github.com/golang/go/blob/master/src/math/big/rat.go#L183 "View Source")
```
func (z *Rat) SetFrac(a, b *Int) *Rat
```
將z設為a/b,并返回z。
### func (\*Rat) [SetString](https://github.com/golang/go/blob/master/src/math/big/rat.go#L426 "View Source")
```
func (z *Rat) SetString(s string) (*Rat, bool)
```
將z設為字符串代表的值,返回z并用一個bool表明是否成功。字符串s的格式可以是形如"a/b"的分數格式,也可以是浮點數后跟可選的指數的科學計數法格式。如果操作失敗,z的值是不確定的,但返回值為nil。
Example
```
r := new(big.Rat)
r.SetString("355/113")
fmt.Println(r.FloatString(3))
```
Output:
```
3.142
```
### func (\*Rat) [Sign](https://github.com/golang/go/blob/master/src/math/big/rat.go#L272 "View Source")
```
func (x *Rat) Sign() int
```
返回x的正負號。如x < 0返回-1;如x > 0返回+1;否則返回0。
### func (\*Rat) [Cmp](https://github.com/golang/go/blob/master/src/math/big/rat.go#L359 "View Source")
```
func (x *Rat) Cmp(y *Rat) int
```
比較x和y的大小。如x < y返回-1;如x > y返回+1;否則返回0。
### func (\*Rat) [Abs](https://github.com/golang/go/blob/master/src/math/big/rat.go#L235 "View Source")
```
func (z *Rat) Abs(x *Rat) *Rat
```
將z設為|x|并返回z。
### func (\*Rat) [Neg](https://github.com/golang/go/blob/master/src/math/big/rat.go#L242 "View Source")
```
func (z *Rat) Neg(x *Rat) *Rat
```
將z設為-x并返回z。
### func (\*Rat) [Inv](https://github.com/golang/go/blob/master/src/math/big/rat.go#L249 "View Source")
```
func (z *Rat) Inv(x *Rat) *Rat
```
將z設為1/x并返回z。
### func (\*Rat) [Set](https://github.com/golang/go/blob/master/src/math/big/rat.go#L226 "View Source")
```
func (z *Rat) Set(x *Rat) *Rat
```
將z設為x(生成一個拷貝)并返回z。
### func (\*Rat) [Add](https://github.com/golang/go/blob/master/src/math/big/rat.go#L364 "View Source")
```
func (z *Rat) Add(x, y *Rat) *Rat
```
將z設為x + y并返回z。
### func (\*Rat) [Sub](https://github.com/golang/go/blob/master/src/math/big/rat.go#L373 "View Source")
```
func (z *Rat) Sub(x, y *Rat) *Rat
```
將z設為x - y并返回z。
### func (\*Rat) [Mul](https://github.com/golang/go/blob/master/src/math/big/rat.go#L382 "View Source")
```
func (z *Rat) Mul(x, y *Rat) *Rat
```
將z設為x * y并返回z。
### func (\*Rat) [Quo](https://github.com/golang/go/blob/master/src/math/big/rat.go#L390 "View Source")
```
func (z *Rat) Quo(x, y *Rat) *Rat
```
如果y != 0會將z設為x/y并返回z;如果y==0會panic。
### func (\*Rat) [Scan](https://github.com/golang/go/blob/master/src/math/big/rat.go#L408 "View Source")
```
func (z *Rat) Scan(s fmt.ScanState, ch rune) error
```
本方法實現了fmt.Scanner接口,將z設為讀取到的數字。接受格式'e'、'E'、'f'、'F'、'g'、'G'、'v';它們都是等價的。
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) [GobEncode](https://github.com/golang/go/blob/master/src/math/big/rat.go#L548 "View Source")
```
func (x *Rat) GobEncode() ([]byte, error)
```
本方法實現了gob.GobEncoder接口。
### func (\*Rat) [GobDecode](https://github.com/golang/go/blob/master/src/math/big/rat.go#L571 "View Source")
```
func (z *Rat) GobDecode(buf []byte) error
```
本方法實現了gob.GobDecoder接口。
### func (\*Rat) [MarshalText](https://github.com/golang/go/blob/master/src/math/big/rat.go#L590 "View Source")
```
func (r *Rat) MarshalText() (text []byte, err error)
```
本方法實現了encoding.TextMarshaler接口。
### func (\*Rat) [UnmarshalText](https://github.com/golang/go/blob/master/src/math/big/rat.go#L595 "View Source")
```
func (r *Rat) UnmarshalText(text []byte) error
```
本方法實現了encoding.TextUnmarshaler接口。
- 庫
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe