🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 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&gt;&gt;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 &lt;&lt; i);如b为0,z = x & ^(1 &lt;&lt; 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&gt;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&lt;0时返回-1;x&gt;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&lt;y时返回-1;x&gt;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 &lt;&lt; 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 &gt;&gt; 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 &lt;= 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都&gt;0时设置z为最大公约数;如果任一个&lt;=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的分子,分子可能&lt;=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的分母,分母总是&gt;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 &lt; 0返回-1;如x &gt; 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 &lt; y返回-1;如x &gt; 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接口。