I've met some interesting problem of Golang big.Float calculation.
The Problem is
10001000100010001000100010001000100010001000100015.5533 / 1000000000000000000
= 10001000100010001000100010001000.1000100010001000155533
However, big.Float gave "10001000100010001000100010001000.10001000100010001555329999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997"
The code:
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
", prec)
fmt.Printf("Quotient: %s
", q.Text('f', -1))
Result:
Percision: 1024
Quotient: 10001000100010001000100010001000.10001000100010001555329999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997
And the more confusing part is, if I set prec = 512, a smaller precision, it produced correct result instead.
Percision: 512
Quotient: 10001000100010001000100010001000.1000100010001000155533
Doses any one know what's wrong of my code or how to configure big.Float to get expected result?
Thanks to all!