我遇到了一些有趣的 Golang big.Float 计算问题。
问题是
10001000100010001000100010001000100010001000100015.5533 / 1000000000000000000
= 10001000100010001000100010001000.1000100010001000155533
However, big.Float gave "10001000100010001000100010001000.10001000100010001555329999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997"
编码:
var prec uint = 1024 // 512
dec, _ := new(big.Float).SetPrec(prec).SetString("1000000000000000000")
f, _ := new(big.Float).SetPrec(prec).SetString("10001000100010001000100010001000100010001000100015.5533")
q := f.Quo(f, dec)
fmt.Printf("Percision: %d\n", prec)
fmt.Printf("Quotient: %s\n", q.Text('f', -1))
结果:
Percision: 1024
Quotient: 10001000100010001000100010001000.10001000100010001555329999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997
更令人困惑的是,如果我设置 prec = 512,更小的精度,它会产生正确的结果。
Percision: 512
Quotient: 10001000100010001000100010001000.1000100010001000155533
有人知道我的代码有什么问题或如何配置 big.Float 以获得预期结果吗?
谢谢大家!