gpt4 book ai didi

go - 如何获取大整数的自然对数

转载 作者:行者123 更新时间:2023-12-03 02:23:23 53 4
gpt4 key购买 nike

我正在尝试将字符串转换为整数,然后计算其对数。我的第一个方法是使用 strconv 库转换字符串,但我收到了有关要转换的字符串长度的错误。

之后,我使用了 math/big 库,效果很好。现在我无法对结果大整数应用 math.Log()

代码:

package main

import (
"fmt"
"math"
"math/big"
)

func main() {
bb := "11948904162160164791281681976941230184120142151411311314211115130161285142991119211447"
bi := big.NewInt(0)
if _, ok := bi.SetString(bb, 10); ok {
fmt.Println(math.Log(bi))
} else {
fmt.Printf("error parsing line %#v\n", bb)
}

}

错误:

cannot use bi (type *big.Int) as type float64 in argument to math.Log

最佳答案

很少有情况需要比标准 float64 类型提供的精度更高的精度。

但只是为了满足任何人可能遇到的“午夜疯狂想法”(甚至一些非常深入的科学研究!),Rob Pike 的 implementations of some operations with big floats可能是您现在可以使用 Go 获得的最好的。 log 函数可以在 here 找到.

关于go - 如何获取大整数的自然对数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58720776/

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com