Golang计算完整精度浮点数。

huangapple go评论71阅读模式
英文:

golang calculate full precision float number

问题

使用小数点精度为200,我需要计算一个从atto到十进制数的数字,类似于屏幕截图。

英文:

Used a decimal point of 200 as the precision, I need to calculate a number from atto to decimal number similar screenshot.

Golang计算完整精度浮点数。

答案1

得分: 3

要获取纳米和阿托精度的值,您可以在fmt.Printf()中使用%.9f%.18f,我创建了一个小程序来获取您的值0.000000000000099707,如下所示:

package main

import (
	"fmt"
	"math"
)

func main() {

	powr := math.Pow(10, -18)
	numb := 99707 * powr
	fmt.Println("number", numb)
	fmt.Printf("\nthe value in atto %.18f\n", numb)
}

输出结果:

number 9.970700000000001e-14

the value in atto 0.000000000000099707
英文:

To get the values at precision of nano and atto you can use %.9f and %.18f in fmt.Printf() respectively,I created a small program to get your value of 0.000000000000099707 as follows:

package main

import (
	"fmt"
	"math"
)

func main() {

	powr := math.Pow(10, -18)
	numb := 99707 * powr
	fmt.Println("number", numb)
	fmt.Printf("\nthe value in atto %.18f\n", numb)
}

Output:

number 9.970700000000001e-14

the value in atto 0.000000000000099707

答案2

得分: 1

我对如何进行这个操作很感兴趣,所以我找到了Cockroach的apd包,它可以处理任意精度的计算。你可以像这样使用它:

import (
	"fmt"
	"github.com/cockroachdb/apd"
)

func main() {
	// 99707 * 10^(-18)
    n1 := apd.New(99707, 0)
    n2 := apd.New(10, 0)
    n3 := apd.New(-18, 0)
	c := apd.BaseContext.WithPrecision(200)
	res := apd.New(0,0)

	ctx, err := c.Pow(res, n2, n3)
	if err != nil {
		panic(err)
	}
	ctx, err = c.Mul(res, res, n1)
	if err != nil {
		panic(err)
	}
	
	fmt.Println(ctx.Inexact(), res.Text('f'))
}

它将输出:

false 0.000000000000099707

你需要注意可能发生的精度损失,并查看inexact字段。

英文:

I was interested in how to do this so I found the apd package from cockroach that handles arbitrary precision calculations. You can use it like this:

import (
	"fmt"
	"github.com/cockroachdb/apd"
)

func main() {
	// 99707 * 10^(-18)
    n1 := apd.New(99707, 0)
    n2 := apd.New(10, 0)
    n3 := apd.New(-18, 0)
	c := apd.BaseContext.WithPrecision(200)
	res := apd.New(0,0)

	ctx, err := c.Pow(res, n2, n3)
	if err != nil {
		panic(err)
	}
	ctx, err = c.Mul(res, res, n1)
	if err != nil {
		panic(err)
	}
	
	fmt.Println(ctx.Inexact(), res.Text('f'))
}

And it will output:

false 0.000000000000099707

You will have to be careful with the loss of precision that may happen and look at the inexact field.

答案3

得分: 1

你也可以使用github.com/shopspring/decimal包来实现这个功能。该库可以表示长达2^31(2147483648)位的数字。以下是一个简单的计算代码示例:

d := decimal.NewFromInt(99707)
d10 := decimal.NewFromInt(10)
dpow := decimal.NewFromInt(-18)
d10pow := d10.Pow(dpow)
dmul := d.Mul(d10pow)
fmt.Println(dmul)

这可以简化为:

d := decimal.NewFromInt(99707).Mul(decimal.NewFromInt(10).Pow(decimal.NewFromInt(-18)))
fmt.Println(d)

输出结果为:0.000000000000099707

请参考playground

英文:

You can use the github.com/shopspring/decimal package for this as well. This library can represents numbers up to 2^31 (2147483648) digits. Here is a simple code to do the calculation:

d := decimal.NewFromInt(99707)
d10 := decimal.NewFromInt(10)
dpow := decimal.NewFromInt(-18)
d10pow := d10.Pow(dpow)
dmul := d.Mul(d10pow)
fmt.Println(dmul)

This can simplified to:

d := decimal.NewFromInt(99707).Mul(decimal.NewFromInt(10).Pow(decimal.NewFromInt(-18)))
fmt.Println(d)

Output: 0.000000000000099707

See playground

huangapple
  • 本文由 发表于 2021年9月4日 16:40:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/69053718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定