• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
can somebody explain the return statement to me ?

class D {
static boolean m(double v) {
return(v != v == Double.isNaN(v));
}
public static void main (String args[]) {
double d1 = Double.NaN;
double d2 = Double.POSITIVE_INFINITY;
double d3 = Double.MAX_VALUE;
System.out.print(m(d1) + "," + m(d2) + "," + m(d3));
}
}
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its ok, I understand,
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
!= and == have same precedence so they are evaluated from left to right if both occure in same expression.
here in this expression: v != v == Double.isNaN(v)
first v!=v is evaluated which results in boolean.
after this == is eveluated.
for readability consider this as
(v != v) == (Double.isNaN(v))
hope this is helpful
Pinky
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Pinky
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Pinky.
This question demonstrates that NaN is never equal to itself. For example, you can not check to see if a variable, x, is equal to NaN using the expression (x == NaN) because the result will always be false. Instead, you should check to see if a variable is equal to NaN by passing it as an argument to the isNaN method.
The isNaN method just returns the result of the expression (v != v). Therefore, the expression
((v != v) == Double.isNaN(v)) will always be true.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then, i still don't get it, why the second part of the expression is not evaluated. that is :
Double.isNaN(v)? if both v!=v and Double.isNaN(v) are evaluated, and we put NaN into "v", the result
should be something like true == true , and the result will definitely be true.
By the way Double.MAX_VALUE is also not a number?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Rouosong.
Dan's explanation has already agreed with your post.
Double.MAX_VALUE is not NaN.
reply
    Bookmark Topic Watch Topic
  • New Topic