NaN and double/float

I took a flick through java.lang.math and found the following lines
public static double max(double a, double b) {
if (a != a) return a; // a is NaN
...

Idea highlighted the condition (a != a) and told me that it was always
false. Of course, this is almost correct, but not quite (as the code
comment says). Shouldn't Idea know about this possibility?

Robbie

PS I haven't filed a bug because I'm not sure about the usage, although
I think on balance I would prefer it not to signal an error here.


5 comments

How's about Double.isNaN(a)? At least you'll get rid of comment this way.

--

Best regards,
Maxim Shafirov
JetBrains, Inc / IntelliJ Software
http://www.intellij.com
"Develop with pleasure!"


"Robert Gibson" <robbie_usenet@yahoo.co.uk> wrote in message
news:bhtcjn$2hj$1@is.intellij.net...

I took a flick through java.lang.math and found the following lines
public static double max(double a, double b) {
if (a != a) return a; // a is NaN
...

>

Idea highlighted the condition (a != a) and told me that it was always
false. Of course, this is almost correct, but not quite (as the code
comment says). Shouldn't Idea know about this possibility?

>

Robbie

>

PS I haven't filed a bug because I'm not sure about the usage, although
I think on balance I would prefer it not to signal an error here.

>


0

How's about Double.isNaN(a)? At least you'll get rid of comment this way.


And it's much clearer.

But notice that this was a theoric question. In the case of doubles, a != a
may be true (let's not enter on who was smoking what for whomever thought (a
!= a) == true is a good idea).

So the idea parser/inspector in the case of doubles should not consider the
if always false.



0

Not having write access to the JDK source code I can't really change it
;) Anyway, have you seen the definition of that method?
static public boolean isNaN(double v) {
return (v != v);
}

Actually, the Double class contains another two lines that have false
problems:
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
In both these cases Java has a well-defined strategy for dealing with
the results, which is not the same as if they were ints.

So the more I think about it, the more I think that Idea should follow
the JLS rules for floats and doubles and not highlight these kinds of
cases as problematic.

Robbie

PS Idea doesn't give any warning for this line
public static final double NaN = 0.0d / 0.0;

0

Is it really worth while fixing, i don't find it very readable (v!=v) ... yeach ...

0

Please sign in to leave a comment.