1

The assertEquals(double,double) method is justifiably deprecated. Is there an alternate and/or different approach baked into junit - or do I roll my own tiny extension

  static boolean assertEquals(double d1, double d2) {
    return Math.abs(d1 - d2) < 1e-8;
}
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • Read the javadoc: http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals%28double,%20double,%20double%29 – JB Nizet Aug 02 '15 at 20:13

2 Answers2

10

Yes, use assertEquals(double expected, double actual, double delta) instead.

vidit
  • 6,293
  • 3
  • 32
  • 50
1

In the case of JUnit, there is already made functions.

But in other type of Java code, other than JUnit Test cases, IBM has a recommendation for comparing two floats, using division rather than subtraction - this makes it easier to select an epsilon that works for all ranges of input.

if (abs(a/b - 1) < epsilon)

As for the value of epsilon, I would use 5.96e-08 as given in this Wikipedia table, or perhaps 2x that value.

(see comparing float/double values using == operator)

Community
  • 1
  • 1
Valentin Montmirail
  • 2,594
  • 1
  • 25
  • 53