日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

golang中的big.Int

發(fā)布時間:2025/6/15 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 golang中的big.Int 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在官方的 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 // signabs 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() 函數(shù)只對 int64 有效,其他類型必須先轉(zhuǎn)成 int64 才行。
但是,官方還提供了許多 Set 函數(shù),可以方便的把其他類型的整形存入 Int ,因此,我們可以先 new(int) 然后再調(diào)用 Set 函數(shù)。

// SetInt64 sets z to x and returns z. func (z *Int) SetInt64(x int64) *Int {neg := falseif x < 0 {neg = truex = -x}z.abs = z.abs.setUint64(uint64(x))z.neg = negreturn z } ? // SetUint64 sets z to x and returns z. func (z *Int) SetUint64(x uint64) *Int {z.abs = z.abs.setUint64(x)z.neg = falsereturn 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 函數(shù),big 庫還提供了一個 SetString() 函數(shù),可以指定進(jìn)制數(shù),比如二進(jìn)制、十進(jìn)制或者十六進(jìn)制等!

// 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 consumedif _, 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 類型的轉(zhuǎn)換
如上,直接調(diào)用 big 庫提供的 Int64(), Uint64() 等函數(shù)就可以進(jìn)行轉(zhuǎn)換。
Int 對象上的運(yùn)算函數(shù)

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 := imio := 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 */

復(fù)制

輸出結(jié)果:

Big Int: 43492122561469640008497075573153004 Big Rat: -37/112 《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的golang中的big.Int的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。