1

within my code:

else if (returnShortestTime() == shortTime[1])

The method returnShortestTime returns the smallest double, and compares to the stored double, so because this doesn't simply with with double A == double B how can i compare two doubles in my code to see if they are equal?

kaan mote
  • 29
  • 4
  • 1
    Your question is hard to understand - can you show a complete example with the expected vs. actual output? – assylias Nov 05 '15 at 09:54
  • The actual output doesn't work... all my if statements compare two doubles and there cannot be an 'else' statement so only conditional if/else if statements and atleast one has to run but it doesn't – kaan mote Nov 05 '15 at 09:55
  • as in java i have heard comparing two doubles directly returns false – kaan mote Nov 05 '15 at 09:55
  • 1
    to check for double "equality" you should compare the absolute of their difference with a threshold value – Manos Nikolaidis Nov 05 '15 at 09:57
  • 2
    `==` check is always fine for primitive equality check – Rahman Nov 05 '15 at 09:57
  • @kaanmote where did you hear that comparing two doubles returns false? Also have you actually tried to compare them with == to see if that works or not? – LBes Nov 05 '15 at 10:02
  • 2
    In general, you should never compare two doubles (or any other floating point types) using `==`, because floating point numbers are approximations (unlike integer types which are exact values). Your two doubles might appear to you be the same value, but depending on how they were computed, they might differ in value by a tiny amount. Yes, `==` might work in some situations, but not others. As @ManosNikolaidis said, the correct way to compare floating point types is to compute the difference and check it is below a small threshold – Tom Nov 05 '15 at 10:17
  • `else if ((returnShortestTime()-shortTime[1]<.0001)` would be one idea using some basic algebra where the `.0001` could be changed to some other value depending on how close you want to say are equal. – JB King Nov 05 '15 at 17:11

1 Answers1

2

In general terms, you can in fact use == for primitive data types in Java. The reason why it is generally not a good idea to compare floating point numbers with equality is due to floating point precision error. There are two solutions for comparison of equality of two floating point numbers (doubles), you can either use Java's BigDecimal or have a check as to if the difference between the two numbers is less than a certain threshold (This is usually called an Epsilon value).

Using BigDecimal

BigDecimal foo = new BigDecimal(returnShortestTimeAsString() /*String Representation of your returnShortestTime() method*/);
BigDecimal bar = new BigDecimal(shortTimeAsString[1] /*String Representation of this array value*/);
if(foo.compareTo(bar) == 0 /*If they are equal*/) doStuff();

Using an Epsilon

if(Math.abs(returnShortestTime() - shortTime[1]) < Math.ulp(1.0) /*This is the Epsilon value*/) doStuff();
Community
  • 1
  • 1
MrPublic
  • 520
  • 5
  • 16