shopspring / decimal

Arbitrary-precision fixed-point decimal numbers in Go
Other
6.41k stars 623 forks source link

result Not accurate #377

Open guotie opened 4 months ago

guotie commented 4 months ago

code:

func TestDecimalPow(t *testing.T) {
    d := decimal.NewFromInt(100)
    sqrt := d.Pow(decimal.NewFromFloat(0.5))
    t.Logf("sqrt: %v", sqrt) // 10

    d = decimal.NewFromBigInt(big.NewInt(100), 0)
    sqrt = d.Pow(decimal.NewFromFloat(0.5))
    t.Logf("sqrt: %v", sqrt) // 10

    d = decimal.NewFromBigInt(big.NewInt(10000), 0)
    d = d.Div(decimal.NewFromBigInt(big.NewInt(100), 0))
    sqrt = d.Pow(decimal.NewFromFloat(0.5))
    t.Logf("sqrt: %v", sqrt) // 9.99999999999999999999999999999999999999998
}

result:

sqrt: 10
sqrt: 10
sqrt: 9.99999999999999999999999999999999999999998

why?