-1

This is the expression:

if( Math.abs(GH1[i]) < Double.valueOf("1E-100") ){
.
.
.
}  

All variables are of double type. It should be clear from the code about what I am trying to compare. I always assumed this would be correct in java , as the compiler never shows any errors either. But I recently read here that I should not use == for comparison of floating point numbers. Is it true of other relational operators? If I am not worried about NaN, can I just use the built in relational operators in stead of the Double or Float functions?

Can I use the expression shown below?

if(GH1[i] < 0.0f)           // if( Double.compare(GH1[i], 0.0f) < 0  )
    tempSign = -1.0f;
else if(GH1[i] == 0.0f)     // else if( Double.compare(GH1[i], 0.0f) == 0 )
    tempSign = 0.0f;
else if(GH1[i] > 0.0f)      // else if( Double.compare(GH1[i], 0.0f) > 0 )
    tempSign = 1.0f;

Or does it have to use Double and Float's functions?

Community
  • 1
  • 1
user13267
  • 6,871
  • 28
  • 80
  • 138

3 Answers3

0

Is it valid? Yes. It will compile and run. However, as you read in that question, when you start comparing very close numbers you could get into rounding error, and rounding error can be a bit unpredictable at times. This causes big issues with exact equality (as your read) but can also cause issues when you start comparing floats which are very close (though it is less likely).

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
0

As stated in the Q&A that you linked, due to edge cases with double NaN, etc., it's best to use double.Compare()

Mike P.
  • 1,920
  • 1
  • 18
  • 20
  • For many applications, it's actually better to use the comparison operators. One generally does not want `Double.NaN` to test as being equal to itself. Same goes for `Double.POSITIVE_INFINITY` and `Double.NEGATIVE_INFINITY`. – Ted Hopp Oct 08 '13 at 04:59
  • Comparison operators don't handle signed zeroes though. 0d == -0d = true. – Mike P. Oct 08 '13 at 05:24
  • I don't recommend using the equality operator `==` for floating point values. However, for many applications 0d and -0d _should_ be considered equal. Comparison operators (`<`, `<=`, etc.) do handle signed zeroes; they just do it differently than `Double.compare`. The time when you absolutely must use `Double.compare` is when you need consistency with `equals()` (e.g., when using them as keys to a `HashMap`). – Ted Hopp Oct 08 '13 at 05:35
  • Yes, by IEEE standards, they should be equal, agreed. – Mike P. Oct 08 '13 at 05:54
0

The relational operators are generally safe to use. The problem with == is that very small rounding errors will result in numbers that one might think are equal testing as not equal. When you do want an equality test, it's best to use something like:

if (Math.abs(a - b) < TOLERANCE) { . . . }

where TOLERANCE is the amount of round-off error you want to ignore. (You can also use a relative amount—TOLERANCE * a, for instance.

You could use Double.compare and test whether it is positive, negative, or 0, but that is generally unnecessary and, in many cases, may be wrong. For instance, Double.compare(Double.NaN, Double.NaN) will return 0 (signifying equal), when you generally want to treat NaN as not being equal to anything (including itself). Same goes for plus and minus infinity.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521