多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # big.Int 在官方的 math/big 包中,Int 类型定义如下: ~~~ // An Int represents a signed multi-precision integer. // The zero value for an Int represents the value 0. type Int struct { neg bool // sign abs nat // absolute value of the integer } ~~~ 生成 Int 类型的方法为 NewInt(),如下: ~~~ // NewInt allocates and returns a new Int set to x. func NewInt(x int64) *Int { return new(Int).SetInt64(x) } ~~~ 可见,NewInt() 函数只对 int64 有效,其他类型必须先转成 int64 才行。 但是,官方还提供了许多 Set 函数,可以方便的把其他类型的整形存入 Int ,因此,我们可以先 new(int) 然后再调用 Set 函数。 ~~~ // SetInt64 sets z to x and returns z. func (z *Int) SetInt64(x int64) *Int { neg := false if x < 0 { neg = true x = -x } z.abs = z.abs.setUint64(uint64(x)) z.neg = neg return z } ​ // SetUint64 sets z to x and returns z. func (z *Int) SetUint64(x uint64) *Int { z.abs = z.abs.setUint64(x) z.neg = false return z } ​ // Set sets z to x and returns z. func (z *Int) Set(x *Int) *Int { if z != x { z.abs = z.abs.set(x.abs) z.neg = x.neg } return z } ~~~ 用法示例 ~~~ func main() { big1 := new(big.Int).SetUint64(uint64(1000)) fmt.Println("big1 is: ", big1) big2 := big1.Uint64() fmt.Println("big2 is: ", big2) } ~~~ 除了上述的 Set 函数,big 库还提供了一个 SetString() 函数,可以指定进制数,比如二进制、十进制或者十六进制等! ~~~ // 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'' or ``0X'' selects base 16; the ``0'' prefix selects base 8, and a // ``0b'' or ``0B'' prefix selects base 2. Otherwise the selected base is 10. // func (z *Int) SetString(s string, base int) (*Int, bool) { r := strings.NewReader(s) if _, _, err := z.scan(r, base); err != nil { return nil, false } // entire string must have been consumed if _, err := r.ReadByte(); err != io.EOF { return nil, false } return z, true // err == io.EOF => scan consumed all of s } ~~~ 用法示例 ~~~ func main() { big1 := new(big.Int).SetString("1000", 10) fmt.Println("big1 is: ", big1) big2 := big1.Uint64() fmt.Println("big2 is: ", big2) } ~~~ uint64, int64 等普通类型与 big.Int 类型的转换 如上,直接调用 big 库提供的 Int64(), Uint64() 等函数就可以进行转换。 Int 对象上的运算函数 ~~~ Mul(im, in) Add(ip, im) Div(ip, io) ~~~ ~~~ import ( "fmt" "math" "math/big" ) func main() { // Here are some calculations with bigInts: im := big.NewInt(math.MaxInt64) in := im io := big.NewInt(1956) ip := big.NewInt(1) ip.Mul(im, in).Add(ip, im).Div(ip, io) fmt.Printf("Big Int: %v\n", ip) // Here are some calculations with bigInts: rm := big.NewRat(math.MaxInt64, 1956) rn := big.NewRat(-1956, math.MaxInt64) ro := big.NewRat(19, 56) rp := big.NewRat(1111, 2222) rq := big.NewRat(1, 1) rq.Mul(rm, rn).Add(rq, ro).Mul(rq, rp) fmt.Printf("Big Rat: %v\n", rq) } /* Output: Big Int: 43492122561469640008497075573153004 Big Rat: -37/112 */ ~~~ 输出结果: ~~~ Big Int: 43492122561469640008497075573153004 Big Rat: -37/112 ~~~