当前位置:网站首页>Golang实现Biginteger大数计算
Golang实现Biginteger大数计算
2022-06-24 14:29:00 【KunkkaWu】
Golang中的big.Int库支持大数计算,基于这个库封装了一层Bitinteger,支持字符串类型的大数,加减乘除等计算。 其他计算可以参考基于big.Int来实现。
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
}
边栏推荐
- postgresql 之 ilist
- 10 Ces autographes très stylisés.
- 10_那些格调很高的个性签名
- laravel8使用faker调用工厂填充数据
- Daily knowledge popularization
- Common sense knowledge points
- The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment
- Unit contour creation method
- R语言构建回归模型诊断(正态性无效)、进行变量变换、使用car包中的powerTransform函数对目标变量进行Box-Cox变换(Box–Cox transform to normality)
- Online text entity extraction capability helps applications analyze massive text data
猜你喜欢
常见的单例模式&简单工厂
Don't underestimate the integral mall. It can play a great role
Defeat the binary tree!
专精特新“小巨人”再启动,“企业上云”数字赋能
Convolution kernel and characteristic graph visualization
ES mapping之keyword;term查詢添加keyword查詢;更改mapping keyword類型
左手代码,右手开源,开源路上的一份子
港股上市公司公告 API 数据接口
leetcode:1504. 统计全 1 子矩形的个数
[deep learning] storage form of nchw, nhwc and chwn format data
随机推荐
六月集训(第23天) —— 字典树
Development of digital Tibetan product system NFT digital Tibetan product system exception handling source code sharing
MySQL log management, backup and recovery
Database considerations
阿里OSS对象存储服务
[pytoch] quantification
怎样评价国产报表工具和BI软件
Qunhui synchronizes with alicloud OSS
ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)
[deep learning] storage form of nchw, nhwc and chwn format data
Laravel 8 realizes auth login
【从零开始学zabbix】一丶Zabbix的介绍与部署Zabbix
API data interface for announcement of Hong Kong listed companies
3环杀掉360安全卫士进程
【ansible问题处理】远程执行用户环境变量加载问题
港股上市公司公告 API 数据接口
ES mapping之keyword;term查詢添加keyword查詢;更改mapping keyword類型
June training (day 24) - segment tree
数字臧品系统开发 NFT数字臧品系统异常处理源码分享
六石管理学:垃圾场效应:工作不管理,就会变成垃圾场