Go语言的big包实现大整数运算

程序虽然写出来了,但是不知道如何用一个大数(例如100位的大数)去初始化一个大数变量,比较遗憾!


Go语言程序:

// bigint project main.go
package main

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
", ip)
	iq := big.NewInt(10000)
	ip.Mod(ip, iq)
	fmt.Printf("Big Int: %v
", ip)
}

程序运行结果:

Big Int: 43492122561469640008497075573153004
Big Int: 3004


程序说明:

1.math包中包含有各种功能函数,包括最大的整数math.MaxInt64

2.math/big包可以用于大整数计算

3.大整数可以使用"%v"格式输出


参考链接:

1.Ubuntu安装Go语言环境

2.Ubuntu构筑LiteIDE的Go语言开发环境





原文地址:https://www.cnblogs.com/tigerisland/p/7563559.html