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

QuantityUtil#isEquivalentRel does not consider zero values #295

Open
sebastian-peter opened this issue Aug 23, 2022 · 0 comments
Open

QuantityUtil#isEquivalentRel does not consider zero values #295

sebastian-peter opened this issue Aug 23, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@sebastian-peter
Copy link
Member

sebastian-peter commented Aug 23, 2022

In order to calculate the relative difference between a and b, the subtractive difference is divided by a. This means though that if a has a 0 value, the result is NaN or Infinity.

public static <Q extends Quantity<Q>> boolean isEquivalentRel(
Quantity<Q> a, Quantity<Q> b, double relQuantityTolerance) {
double aVal = a.getValue().doubleValue();
double bVal = b.to(a.getUnit()).getValue().doubleValue();
return (Math.abs(aVal - bVal) / Math.abs(aVal)) <= relQuantityTolerance;
}

@sebastian-peter sebastian-peter added the bug Something isn't working label Aug 23, 2022
@sebastian-peter sebastian-peter changed the title QuantityUtil#isEquivalentRel does not consider zero values QuantityUtil#isEquivalentRel does not consider zero values Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant