/fltcmp

Go float comparison using ULPs

Primary LanguageGoMIT LicenseMIT

fltcmp

Go Report Card Travis CI GoDoc

Go float comparison using Units in the Last Place (ULPs). You can find a good write up of the method here.

When you test for float equality, you typically aren't looking for an exact match. What you might end up doing is testing for equality with some constant error threshold. The problem with that is the behaviour is way different when comparing very small floats and very large floats. ULP testing is an alternative.

Benchmark

It's slow.

BenchmarkAlmostEqualDifferent-8         300000000                4.11 ns/op
BenchmarkAlmostEqualSame-8              300000000                3.98 ns/op
BenchmarkEpsilonSame-8                  2000000000               0.30 ns/op
BenchmarkEpsilonDifferent-8             2000000000               0.30 ns/op