Inspection Gadgets - floating point values compared... - question

Hello

I am not sure about this code, I might be wrong of course :-):

private Double denominator = new Double(0);
....
if (denominator.doubleValue() == 0) {
...
}

the comparison "denominator.doubleValue() == 0"
triggers warning "floating point values compared for exact equality"

I thought that 0 should is considered to be exact? And than the warning should be disabled for 0?

1 comment
Comment actions Permalink

Adam wrote:

the comparison "denominator.doubleValue() == 0"
triggers warning "floating point values compared for exact equality"

I thought that 0 should is considered to be exact? And than the warning should be disabled for 0?


The problem is not the zero, but the value you are comparing it with.
Depending on how it was computed there may be a small amount of floating
point error in it, so in general it is not safe to compare it with zero and
the inspection is entirely correct to warn you about this.

Hope that helps,
Vil.
--
Vilya Harvey
vilya.harvey@digitalsteps.com / digital steps /
(W) +44 (0)1483 469 480
(M) +44 (0)7816 678 457 http://www.digitalsteps.com/

0

Please sign in to leave a comment.