Maybe this comparison is completely wrong or unfair, but calculations with
Go untyped constants is much faster than
the github.com/ericlagergren/decimal package. In the end, they both produce
the same correct answer: 0 .
func BenchmarkUntypedConstants(b *testing.B) {
b.Log(0.3 - 0.1*3)
for i := 0; i < b.N; i++ {
_ = 0.3 - 0.1*3
}
}
func BenchmarkDecimalPackage(b *testing.B) {
x := decimal.New(1, 1)
y := decimal.New(3, 0)
z := decimal.New(3, 1)
for i := 0; i < b.N; i++ {
z.Sub(z, x.Mul(x, y))
}
}
BenchmarkUntypedConstants-8 2000000000 0.29 ns/op
BenchmarkDecimalPackage-8 20000000 117 ns/op
On Wednesday, August 29, 2018 at 10:33:16 PM UTC-4, José Colón wrote:
>
> I read that a common way to demonstrate that floating point numbers suffer
> from approximation problems is by calculating this:
>
> 0.3 - 0.1 * 3
>
> which should produce 0 but in Java, Python, and Javascript for example,
> they produce -5.551115123125783e-17 .
>
> Surprisingly (or not, ;) ), Go produces the correct 0 result! I wonder why
> is this so? Is it some higher precision being used versus these other
> languages? Or is it some extra correcting logic behind the scenes?
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.