【发布时间】:2022-01-19 03:24:39
【问题描述】:
testA, testB, testC := big.NewFloat(0), big.NewFloat(0), big.NewFloat(0)
testA.SetPrec(500)
testB.SetPrec(500)
testA.SetString("0.081531021188798896")
testB.SetString("0.9975")
testC.Mul(testA, testB)
testD := testC.Text('f', 500)
fmt.Println("testC", testD)
go结果是
0.081327193635826897089025...
而实际结果是
0.08132719363582689876
由https://www.calculator.net/big-number-calculator.html生成
go结果很接近,但不正确
【问题讨论】:
-
您正在使用 floats 进行计算。如果您想要准确的结果,请使用有理数 (big.Rat) 进行计算。通过将精度提高到过高的值,浮点数的计算不会神奇地变得“正确”。您的问题是,不可能将 0.9975 精确地呈现为浮点数。见0.30000000000000004.com
-
你也打算
testC.SetPrec(500)吗?
标签: go floating-point precision floating-accuracy