-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Impossible to test equality of floating point numbers #83
Comments
Generally speaking I think if you are going to be using floating point numbers in examples, then they should be very close matches indeed. As such, if we knew what was a floating point test, we could pick some fairly reasonable values for the However, the main issue is working out when to use floating point comparison. I think there are a few options, but they all have downsides:
|
For what it's worth I don't think option 1 is so bad...are there actually many cases where you might want to write a fraction? One nice thing about option 1 is that then you don't have to worry about having an arbitrary tolerance, you can instead check whether the actual numeric result formatted to the same number of decimal places used in the example result gives the same string as the example result. Literally test that if you ran the example code and printed out the result to the same number of decimal places, would you get the same output... I guess you could argue that that that could be affected by rounding settings, but you could accomplish pretty much the same thing by using a tolerance set to 10^-n where n is the number of decimal digits shown in the example result. (e.g. if an example shows an output of 2.34, then that should definitely be accurate to within 0.01...) |
I think 1. is bad mostly for the unintuitive behaviour:
Deriving the precision from the number of decimal digits is a good idea though! My preferred option would be 3, but failing that 1 is probably the best way forward. |
Option 3 is pretty annoying if you have examples where the result is (unavoidably) irrational though...if you wanted to write out an example for a circle area or sphere volume function you'd need to include all 17 decimal places or whatever if you wanted the parsed number to be exactly equal to the actual result. Even something like (famously) 0.1 + 0.2 --> 0.3 doesn't work, you'd have to write 0.1 + 0.2 --> 0.30000000000000004 which seems...not ideal. |
The |
We could also specify the precision per example, something like -- Precision: 1
0.1 + 0.2 --> 0.3 |
The problem isn't (only) specifying the tolerance there, but even knowing that you should be using floating point equality.
I personally think that's fine, since at least its accurate. Like if someone copies |
Work around current limitations in elm-verify-examples (see stoeffel/elm-verify-examples#83)
Hey!
Thanks for the tool.
I want to use elm-verify-examples to test a function that returns a float. It seems impossible to test equality of floating point numbers, even if they are strictly equal. Consider the following example:
With this I am getting the following message:
Do not use Expect.equal with floats. Use Float.within instead.
The text was updated successfully, but these errors were encountered: