Skip to content

QuantityUtil#isEquivalentRel does not consider zero values #295

@sebastian-peter

Description

@sebastian-peter

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;
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions