網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Golang實(shí)現(xiàn)Biginteger大數(shù)計(jì)算實(shí)例詳解_Golang
作者:KunkkaWu ? 更新時(shí)間: 2022-09-16 編程語(yǔ)言正文
Golang中的big.Int庫(kù)支持大數(shù)計(jì)算,基于這個(gè)庫(kù)封裝了一層Bitinteger,支持字符串類型的大數(shù),加減乘除等計(jì)算。
其他計(jì)算可以參考基于big.Int來(lái)實(shí)現(xiàn)。
package BigIntege import ( "fmt" "math/big" ) const DecBase = 10 // BigInteger wrapper for big.Int type BigInteger struct { Value *big.Int } func NewBigInteger(value string) \*BigInteger { var val big.Int newVal, ok := val.SetString(value, DecBase) if ok { return &BigInteger{ Value: newVal, } } return NewZeroBigInteger() } func NewZeroBigInteger() *BigInteger { return &BigInteger{ Value: big.NewInt(0), } } func (x *BigInteger) Add(y *BigInteger) { x.Value = x.Value.Add(x.Value, y.Value) } func (x *BigInteger) Sub(y *BigInteger) { x.Value = x.Value.Sub(x.Value, y.Value) } // Cmp compares x and y and returns: // // -1 if x < y // 0 if x == y // +1 if x > y func (x *BigInteger) Cmp(y *BigInteger) int { return x.Value.Cmp(y.Value) } func (x *BigInteger) String() string { return x.Value.String() } // Sum 加法 func Sum(x, y *BigInteger) *BigInteger { z := NewZeroBigInteger() z.Add(x) z.Add(y) return z } // Sub 減法 func Sub(x, y *BigInteger) *BigInteger { z := NewBigInteger(x.String()) z.Sub(y) return z } // Mul 懲罰 func Mul(x, y \*BigInteger) \*BigInteger { t := NewZeroBigInteger() z := t.Value.Mul(x.Value, y.Value) return &BigInteger{Value: z} } // Div 除法 func Div(x, y *BigInteger) *BigInteger { t := NewZeroBigInteger() z := t.Value.Div(x.Value, y.Value) return &BigInteger{Value: z} } func isValidBigInt(val string) error { _, ok := big.NewInt(0).SetString(val, 10) if !ok { return fmt.Errorf("parse string to big.Int failed, actual: %s", val) } return nil }
原文鏈接:https://cloud.tencent.com/developer/article/2029133
相關(guān)推薦
- 2022-07-08 一文詳解C++中運(yùn)算符的使用_C 語(yǔ)言
- 2022-09-25 Clion配置STM32開(kāi)發(fā)環(huán)境printf函數(shù)打印浮點(diǎn)數(shù)快速設(shè)置方法
- 2022-06-08 Spring Cloud Nacos NacosWatch
- 2022-04-10 Python?十個(gè)字典用法使用技巧歸納_python
- 2022-12-14 React?Native設(shè)備信息查看調(diào)試詳解_React
- 2022-08-25 .NET6環(huán)境下實(shí)現(xiàn)MQTT通信及詳細(xì)代碼演示_實(shí)用技巧
- 2022-03-16 Oracle數(shù)據(jù)庫(kù)分析函數(shù)用法_oracle
- 2022-08-30 Python?selenium下拉選擇框?qū)崙?zhàn)應(yīng)用例子_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支