https://github.com/erinpentecost/fltcmp
Go float comparison using ULPs
https://github.com/erinpentecost/fltcmp
equality float floating-point ulp
Last synced: 3 months ago
JSON representation
Go float comparison using ULPs
- Host: GitHub
- URL: https://github.com/erinpentecost/fltcmp
- Owner: erinpentecost
- License: mit
- Created: 2018-12-09T17:49:52.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-18T05:15:24.000Z (over 7 years ago)
- Last Synced: 2024-06-20T05:26:04.280Z (almost 2 years ago)
- Topics: equality, float, floating-point, ulp
- Language: Go
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fltcmp
[](https://goreportcard.com/report/github.com/erinpentecost/fltcmp)
[](https://travis-ci.org/erinpentecost/fltcmp.svg?branch=master)
[](https://godoc.org/github.com/erinpentecost/fltcmp)
Go float comparison using Units in the Last Place (ULPs). You can find a good write up of the method [here](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
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.
```sh
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
```