GO/Golang:如何从Big.Float中提取最小的数字?

发布于 2025-01-18 09:08:18 字数 274 浏览 2 评论 0原文

在GO/Golang中,我有一个类型Big.Float的变量,(任意)精度为3,324,000,代表十进制数字的1,000,000位数字。这是计算PI的迭代的结果。 现在,我想打印出最小的100位数字,即数字999,900至1,000,000。

我尝试使用FMT.sprintf()和big.text()将变量转换为字符串。但是,两种功能都会在进一步提高精度时消耗大量的处理时间,这些处理时间变得不可接受(很多小时甚至几天)。

我正在寻找一些提取变量的最后100个(十进制)数字的功能。 事先感谢您的支持。

In Go/Golang I have a variable of type big.Float with an (arbitrary) precision of 3,324,000 to represent a decimal number of 1,000,000 digits. It's the result of an iteration to calculate pi.
Now I want to print out the least significant 100 digits, i.e. digits 999,900 to 1,000,000.

I tried to convert the variable to a string by using fmt.Sprintf() and big.Text(). However, both functions consume a lot of processing time which gets unacceptable (many hours and even days) when further raising the precision.

I'm searching for some functions which extract the last 100 (decimal) digits of the variable.
Thanks in advance for your kind support.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2025-01-25 09:08:18

标准库无法提供有效返回这些数字的函数,但是您可以计算它们。

隔离您感兴趣的数字并打印它们更有效。这避免了过多的计算,以确定每个数字。

下面的代码显示了可以完成的方法。您将需要确保有足够的精度来准确生成它们。

package main

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

func main() {
    // Replace with larger calculation.
    pi := big.NewFloat(math.Pi)

    const (
        // Pi: 3.1415926535897932...
        // Output: 5926535897
        digitOffset = 3
        digitLength = 10
    )

    // Move the desired digits to the right side of the decimal point.
    mult := pow(10, digitOffset)
    digits := new(big.Float).Mul(pi, mult)

    // Remove the integer component.
    digits.Sub(digits, trunc(digits))

    // Move the digits to the left of the decimal point, and truncate
    // to an integer representing the desired digits.
    // This avoids undesirable rounding if you simply print the N
    // digits after the decimal point.
    mult = pow(10, digitLength)
    digits.Mul(digits, mult)
    digits = trunc(digits)

    // Display the next 'digitLength' digits. Zero padded.
    fmt.Printf("%0*.0f\n", digitLength, digits)
}

// trunc returns the integer component.
func trunc(n *big.Float) *big.Float {
    intPart, accuracy := n.Int(nil)
    _ = accuracy
    return new(big.Float).SetInt(intPart)
}

// pow calculates n^idx.
func pow(n, idx int64) *big.Float {
    if idx < 0 {
        panic("invalid negative exponent")
    }
    result := new(big.Int).Exp(big.NewInt(n), big.NewInt(idx), nil)
    return new(big.Float).SetInt(result)
}

The standard library doesn't provide a function to return those digits efficiently, but you can calculate them.

It is more efficient to isolate the digits you are interested in and print them. This avoids excessive calculations of an extremely large number to determine each individual digit.

The code below shows a way it can be done. You will need to ensure you have enough precision to generate them accurately.

package main

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

func main() {
    // Replace with larger calculation.
    pi := big.NewFloat(math.Pi)

    const (
        // Pi: 3.1415926535897932...
        // Output: 5926535897
        digitOffset = 3
        digitLength = 10
    )

    // Move the desired digits to the right side of the decimal point.
    mult := pow(10, digitOffset)
    digits := new(big.Float).Mul(pi, mult)

    // Remove the integer component.
    digits.Sub(digits, trunc(digits))

    // Move the digits to the left of the decimal point, and truncate
    // to an integer representing the desired digits.
    // This avoids undesirable rounding if you simply print the N
    // digits after the decimal point.
    mult = pow(10, digitLength)
    digits.Mul(digits, mult)
    digits = trunc(digits)

    // Display the next 'digitLength' digits. Zero padded.
    fmt.Printf("%0*.0f\n", digitLength, digits)
}

// trunc returns the integer component.
func trunc(n *big.Float) *big.Float {
    intPart, accuracy := n.Int(nil)
    _ = accuracy
    return new(big.Float).SetInt(intPart)
}

// pow calculates n^idx.
func pow(n, idx int64) *big.Float {
    if idx < 0 {
        panic("invalid negative exponent")
    }
    result := new(big.Int).Exp(big.NewInt(n), big.NewInt(idx), nil)
    return new(big.Float).SetInt(result)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文