3

One of the methods i have returns a double. When testing this method as part of my jUnit, i noticed the following oddity:

    String a = "someString";
    String b = "someDifferentString";

    double result = c.getScore(a, b, true);
    System.out.println(result); // prints 0.0

    assert (result > 0.0); // Test passes

So .. I ask you, how can 0.0 be more then 0.0? Why does result > 0.0 evaluates to true?

James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • I think you can find the answer in this topic: http://stackoverflow.com/questions/285680/representing-monetary-values-in-java – Jako Jan 17 '12 at 15:54

3 Answers3

9

assert is a Java keyword. You need assertTrue(result > 0.0)

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html as per this documentation, assert Expression is valid, which is want done by OP right? Am I missing something? – kosa Jan 17 '12 at 15:55
  • Good catch. I was thinking it might be some floating-point precision problem. +1. – Bhesh Gurung Jan 17 '12 at 15:58
  • @thinksteep: from the same page: "By default, assertions are disabled at runtime". – JB Nizet Jan 17 '12 at 15:58
  • @JBNizet, ahhh! I should have refreshed my memory before posting the comment, yes, you are correct they are disabled by default. Shouldn't that be answer, enable assert? – kosa Jan 17 '12 at 16:02
  • No. This is much too fragile, and what the OP wants is to actually use JUnit to check than the condition is true. An assert is used to verify a postcondition. – JB Nizet Jan 17 '12 at 16:06
7

Comparing doubles in general is dangerous, because floating-point representations are, by definition, inexact. Furthermore, you have to be careful when printing values out, as the printed representation is often rounded compared to the actual stored representation.

That said, @JBNizet nailed it -- you're writing a Java assertion, not a JUnit test!

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
7

It's floating point math that is your worry. 0.0 is probably not absolute 0.0, but 0.00000000000000009 or something really really small. Why is this? Well floating point math is discrete in computers. But in reality floating point math is continuous, and hence we have a mismatch between discrete (digital) and continuous (analog). Errors begin to creep in causing things to drift a little. If you want to know more read this:

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

There are methods in junit specifically designed for comparing floats/doubles that allow for a tolerance (ie assertEquals(double expected,double actual,double epsilon)). Use those and you should stabilize your test in the face of the tiny errors you're seeing. Be aware not to set them too high because your overall error should be very small.

See this question about it as well:

JUnit assertEquals(double expected, double actual, double epsilon)

Community
  • 1
  • 1
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138