如何强制将bigFloat的所有小数位保存在文件中,而不是四舍五入?

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

how to force to save all decimal points of bigFloat in file, instead of rounding

问题

假设我有一个非常精确的输入数字(字符串格式),经过math/big操作后,我想再次转换为字符串格式。

package main

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

func check(e error) {
	if e != nil {
		panic(e)
	}
}

func main() {	
    // 编辑
    s := "3.1415926123456789123456789123456789123456789"
	var n, _ = new(big.Float).SetString(s)
	// var n = big.NewFloat(3.1415926123456789123456789123456789123456789)
	fmt.Println(n) // 3.1415926123456788 

	N := n.String()
	fmt.Println(N)  // 3.141592612

	d1 := []byte(N)
	err := os.WriteFile("./dat1.txt", d1, 0644) // 3.141592612
	check(err)
}

如何将类似于3.1415926123456789123456789123456789123456789的big.Float保存到文件中?我想保留所有的小数位数,或者至少尽可能多地保留。

英文:

Let's assume I have a very accurate input number(string format), and after math/big manipulation, I want to convert to string format again.

package main

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

func check(e error) {
	if e != nil {
		panic(e)
	}
}


func main() {	
    // edit
    s := "3.1415926123456789123456789123456789123456789"
	var n, _ = new(big.Float).SetString(s)
	// var n = big.NewFloat(3.1415926123456789123456789123456789123456789)
	fmt.Println(n) // 3.1415926123456788 

	N := n.String()
	fmt.Println(N)  // 3.141592612

	d1 := []byte(N)
	err := os.WriteFile("./dat1.txt", d1, 0644) // 3.141592612
	check(err)
}

How to save a big Float like 3.1415926123456789123456789123456789123456789 into a file? I want to keep all the decimal points, or at least as much as possible

答案1

得分: 3

你可以解析和存储你的输入,但是你必须增加精度(默认精度不够)。可以使用Float.SetPrec()来增加精度(需要指定位数)。

在生成文本表示时,使用Float.Text(),同样要使用足够大的精度(需要指定小数位数)。如果你不知道所需的小数精度,根据文档,你可以使用负值,以获得Float尾数所需的最小十进制位数。

例如:

s := "3.1415926123456789123456789123456789123456789"
fmt.Println(s)

n := big.NewFloat(0)
n.SetPrec(200)
n.SetString(s)

N := n.Text('f', 50)
fmt.Println(N)

N = n.Text('f', -1)
fmt.Println(N)

这将输出(在Go Playground上尝试):

3.1415926123456789123456789123456789123456789
3.14159261234567891234567891234567891234567890000000
3.1415926123456789123456789123456789123456789
英文:

You can parse and store your input "precisely", but you must increase the precision (the default precision doesn't cover that). Use Float.SetPrec() for that (requires a bit-count).

When generating text representation, use Float.Text(), again, with sufficiently large precision (requires a decimal digit-count). If you don't know the required digit-precision, as per the doc, you may use a negative value to have the smallest number of decimal digits that is needed for the Float's mantissa bits.

For example:

s := "3.1415926123456789123456789123456789123456789"
fmt.Println(s)

n := big.NewFloat(0)
n.SetPrec(200)
n.SetString(s)

N := n.Text('f', 50)
fmt.Println(N)

N = n.Text('f', -1)
fmt.Println(N)

This will output (try it on the Go Playground):

3.1415926123456789123456789123456789123456789
3.14159261234567891234567891234567891234567890000000
3.1415926123456789123456789123456789123456789

答案2

得分: 0

我刚刚查看了String方法及其文档(https://pkg.go.dev/math/big#Float.String)。

String方法将x格式化为x.Text('g', 10)的形式...

那么我们来看一下该方法的文档。

func (x *Float) Text(format byte, prec int) string

Text方法根据给定的格式和精度将浮点数x转换为字符串。

精度prec控制数字的位数... 负精度选择了最小的十进制位数,足以唯一标识值x。

你只需要阅读文档即可。

英文:

I've just looked what String method does and its documentation (https://pkg.go.dev/math/big#Float.String).

> String formats x like x.Text('g', 10)...

So let's go to that method doc.

> func (x *Float) Text(format byte, prec int) string

> Text converts the floating-point number x to a string according to the given format and precision prec.

> The precision prec controls the number of digits ... A negative precision selects the smallest number of decimal digits necessary to identify the value x uniquely

All you need is just reading the doc.

huangapple
  • 本文由 发表于 2021年10月21日 17:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/69659183.html
匿名

发表评论

匿名网友

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

确定