javadba javadba - 3 months ago 16
Java Question

How to test for equality of float/double in JUnit?

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

Answer

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