go-test / deep

Golang deep variable equality test that returns human-readable differences
MIT License
743 stars 54 forks source link

"Proper" floating point numbers comparison #30

Closed zerkms closed 4 years ago

zerkms commented 4 years ago

Instead of // 6 decimal places is close enough which looks like a bad joke, floating point numbers should be* compared using epsilon, using

|a - b| < epsilon

or

|(b - a)/b| < epsilon

References:

daniel-nichter commented 4 years ago

https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ is an interesting read. My guess is that most applications stay within the millisecond to nanosecond range of float values, so " 6 decimal places is close enough" has worked well for the last few years. But if an epsilon comparison will help at very high precisions, then sounds good to me. Want to submit a PR and test case?

daniel-nichter commented 4 years ago

Closing because there's no bug, or lots of upvotes, or a PR.

I considered this, but functionally it makes almost no difference for most testing, and I think rounding is the better choice for users in this context. Most programmers know, I hope, that floats will come back with "imprecision", 0.1 -> 0.100000001490116119384765625. The programmer knows, for example, that they're only interested in the first decimal place, so FloatPrecision = 1 is easy and works. (They probably also use %.1f in their print statements.) The same result can be obtained with epsilon = 0.1, but as a pkg feature that's the wrong focus: the programmer doesn't care about relative error and epsilon, they're just trying to round off the float "imprecision". (Or maybe they're rounding off values with a little jitter, like response times.) In a more strictly mathematical/scientific context, epsilon comparison would be the better choice.

I'll update the code to remove the comment about 6 decimal places. It was leftover from previous revs; it hasn't been accurate for a long time.

More references: https://floating-point-gui.de/errors/comparison/ https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/