当前位置:网站首页>Golang implements BigInteger large number calculation
Golang implements BigInteger large number calculation
2022-06-24 14:33:00 【KunkkaWu】
Golang Medium big.Int The library supports large number calculation , Based on this library, a layer of Bitinteger, Support large numbers of string types , Add, subtract, multiply, divide, etc . Other calculations can refer to the calculation based on big.Int To achieve .
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 Add
func Sum(x, y *BigInteger) *BigInteger {
z := NewZeroBigInteger()
z.Add(x)
z.Add(y)
return z
}
// Sub Subtraction
func Sub(x, y *BigInteger) *BigInteger {
z := NewBigInteger(x.String())
z.Sub(y)
return z
}
// Mul punishment
func Mul(x, y \*BigInteger) \*BigInteger {
t := NewZeroBigInteger()
z := t.Value.Mul(x.Value, y.Value)
return &BigInteger{Value: z}
}
// Div division
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
- R语言plotly可视化:可视化模型在整个数据空间的分类轮廓线(等高线)、meshgrid创建一个网格,其中每个点之间的距离由mesh_size变量表示、使用不同的形状标签表征、训练、测试及分类标签
- List of PostgreSQL
- [pytoch] quantification
- 【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维
- From pair to unordered_ Map, theory +leetcode topic practice
- 专精特新“小巨人”再启动,“企业上云”数字赋能
- PgSQL queries the largest or smallest data of a field in a group
- R语言实战应用精讲50篇(二十三)-贝叶斯理论重要概念: 可信度Credibility, 模型Models, 和参数Parameters
- Linux 安装 CenOS7 MySQL - 8.0.26
猜你喜欢

左手代码,右手开源,开源路上的一份子

laravel8使用faker调用工厂填充数据

业务与技术双向结合构建银行数据安全管理体系

leetcode.12 --- 整数转罗马数字

Virtual machines on the same distributed port group but different hosts cannot communicate with each other

【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维

leetcode. 12 --- integer to Roman numeral

Common singleton mode & simple factory

Method after charging the idea plug-in material theme UI

Don't underestimate the integral mall. It can play a great role
随机推荐
打败 二叉树!
在宇宙的眼眸下,如何正确地关心东数西算?
不要小看了积分商城,它的作用可以很大
09_一种比较高效的记忆方法
How to evaluate domestic reporting tools and Bi software
时间同步业务的闭环管理——时间监测
box-sizing
As a developer, what is the most influential book for you?
laravel 8 实现Auth登录
STM32F1与STM32CubeIDE编程实例-WS2812B全彩LED驱动(基于SPI+DMA)
【LeetCode】10、正则表达式匹配
Development of B2B transaction collaborative management platform for kitchen and bathroom electrical appliance industry and optimization of enterprise inventory structure
c语言---18 函数(自定义函数)
日常知识科普
ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)
box-sizing
laravel8使用faker调用工厂填充数据
Laravel 8 realizes auth login
[learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
在CVS中恢复到早期版本