最大值*big.Int和最大精度*big.Rat是多少?

推荐答案

以下是 struct 定义:

// A Word represents a single digit of a multi-precision unsigned integer.
type Word uintptr

type nat []Word

type Int struct {
    neg bool // sign
    abs nat  // absolute value of the integer
}

type Rat struct {
    // To make zero values for Rat work w/o initialization,
    // a zero value of b (len(b) == 0) acts like b == 1.
    // a.neg determines the sign of the Rat, b.neg is ignored.
    a, b Int
}

没有明确的限制.限制将是您的内存或理论上的最大数组大小(2^31或2^63,具体取决于您的平台).


如果您有实际的顾虑,您可能会对在http://golang.org/src/pkg/math/big/nat_test.go中进行的测试感兴趣,例如,10^http://golang.org/src/pkg/math/big/nat_test.go000是基准测试.

你可以很容易地运行这种程序:

package main

import (
    "fmt"
    "math/big"
)

func main() {
    verybig := big.NewInt(1)
    ten := big.NewInt(10)
    for i:=0; i<100000; i++ {
       verybig.Mul(verybig, ten)
    }
    fmt.Println(verybig)
}

(如果你想让它跑得足够快,go playground ,use a smaller exponent than 100000)

问题不在于最大大小,而在于使用的内存和此类计算所需的时间.

Go相关问答推荐

Go - os.userHomeDir()在WSL上返回C:\Users\

golang-jWT签名无效

Go汇编器命名为Constants

Go程序在并发Forking 循环中停留在syscall.Wait4

golang 的持久隐蔽服务

Golang中的泛型 struct /接口列表

JWT 如何解析声明有效性和错误?

从文件读取字节,将其保存到 struct 体并修改值

golang yaml 马歇尔网址

创建新对象后如何返回嵌套实体?

Apache Beam 在 Go 中从 PCollection 中 Select 前 N 行

无法使用 Golang 扫描文件路径

Golang - 客户 Unmarshaler/Marshaler 在指针上具有 nil/null 值

如何使用 math/big 对 bigInt 进行取模?

如何在gin中获取参数值数组

即使一个测试用例失败,如何运行所有测试用例

如何从 docker-compose 命令运行 2 个不同的命令:

为什么 Go 中的 maps.Keys() 将 map 类型指定为 M?

Go/Golang:如何从 big.Float 中提取最低有效数字?

如何从应用程序调用谷歌云身份 API