Skip to content
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

Quantity.Equals(Quantity other, Quantity tolerance) gives false negative due to floating-point arithmetic #1368

Open
AndreasLeeb opened this issue Feb 22, 2024 · 1 comment
Labels

Comments

@AndreasLeeb
Copy link
Contributor

AndreasLeeb commented Feb 22, 2024

Describe the bug
The current implementation of e.g. Length.Equals(Length other, Length tolerance) can lead to false negatives at the boundaries of the tolerance (for instance when comparing a with a - tolerance).

To Reproduce

var tolerance = Length.FromMillimeters(0.5);
var length = Length.FromMeters(0.1);
Assert.True((length - tolerance).Equals(length, tolerance));

The assert fails, which makes sense if you rephrase it as
Assert.True(((length - tolerance) - length).Abs() <= tolerance);
which also fails as
((length - tolerance) - length).Abs().Millimeters == 0.50000000000000044
due to floating-point arithmetic.

Expected behavior
That the above Assert with Equals returns true.

Additional context
Tried it with UnitsNet 4.132.0, 5.43.0 and on the current master branch (1b3647f).

@angularsen
Copy link
Owner

This seems similar to #1367 , let's discuss there for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants