Reputation: 447
For Double/Float, there are some rounding errors and loss of precision when converting from decimal to binary representation. For example, setting float to "6.1" and then printing it out again, you may get a reported value of something like "6.099999904632568359375". Which is the better option for checking equality of 2 Double objects : Using BigDecimal or (Math.abs(double1 - double2) < epsilon)
Upvotes: 9
Views: 18694
Reputation: 982
here is an example for jdk >= 21 :
record MeasurementDetails(LocalDateTime eventDate, double temp, double hum) {}
record Measurement(MeasurementDetails measurementDetails, double difference) {}
var res = new ArrayList<Measurement>();
// add elements to the "res" list...
var m1 = res.stream().findFirst().orElseThrow(); // first element
var m2 = res.stream().skip(1).findFirst().orElseThrow(); // second
var m3 = res.stream().skip(2).findFirst().orElseThrow(); // third
// equality is tolerated on 10^-6
assertThat(m1.difference()).isCloseTo(-0.2, Offset.offset(0.000001));
assertThat(m2.difference()).isCloseTo(-0.4, Offset.offset(0.000001));
assertThat(m3.difference()).isCloseTo(0, Offset.offset(0.000001));
Upvotes: 0
Reputation: 104
If you can use a library, you can use Apache Commons Math, there is a Precision class. Works the same way as previous answers.
boolean isEqual = Precision.equals(double x, double y, double eps);
link to javadoc
Upvotes: 1
Reputation: 61
With Guava, you have:
DoubleMath.fuzzyEquals(double a, double b, double tolerance)
and
DoubleMath.fuzzyCompare(double a, double b, double tolerance)
They are easier to use and more correct than writing "Math.abs(a-b) <= EPSILON". They cover the cases of Double.NaN and the infinities.
Upvotes: 6
Reputation: 449
It would be different based on the level of accuracy needed. But may be take a look at Java.lang.Double.equals() method.
Upvotes: 0
Reputation: 3836
Floating point numbers have what is called an epsilon tolerance, the minimum representable value. To compare floats check their difference is within tolerance.
Math.abs(a-b) <= EPSILON
Of course if you happen to be working with a number that is too big to fit in a double, you should use the big number variant. EPSILON is some tolerance several orders greater than Double.MIN_VALUE, but sufficiently small that the variance is negligible.
Upvotes: 0
Reputation: 2930
If you want to test equality you can use Double.compare()
or BigDecimal.
If you need want approximate equality then Math.abs(d1-d2) < epsilon
Upvotes: 0
Reputation: 14738
It depends on your usecase. If you work with currency, go for BigDecimal.
Otherwise if you can live with approximations use Math.abs(double1 - double2) < epsilon
Upvotes: 5