• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Wrappers

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));
}
}
the ans is true, true, true how?
how the exp are evualeted? the operators are same precedence.
thx
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you put parens around it to force the precedence like:

??
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are mixing apples and oranges here. this syntax (a == b == c) is NOT on the exam. separate your explorations of this weird syntax from your explorations of NaN , MAX_VALUE, etc.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anushree ari:
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));
}
}
the ans is true, true, true how?


It will always get 'true' as the result, because when v != v == Double.isNaN(v) is evaluated, v!=v is evaluated first and returns 'false', false==Double.isNaN(v) of course returns 'true'.
Hope that helps.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
scjp 1.4, that's u
but, i think, "v!=v is evaluated first and returns 'false'" ,error.
when v is a NaN,or infinite, v !=v always be true
 
anushree ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everybody,
but iam little bit confused about false==Double.isNan(v); how the result is true,
thx
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anushree ari:

but iam little bit confused about false==Double.isNan(v); how the result is true,


assume Double.isNan(v) returns false
false == false --> is true
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Let break it down:
When v is NaN
1. v != v is True because NaN when compare to any thing is alway false so !(false) = true
2. The only way for NaN compare to NaN is using the isNaN function so NaN(v) = true.
3. True == True so the m(v) = true
When v is infinity
v != v is Infinity == infinity so !infinity = false.
of course isNaN return false for any number not Nan so it is false.
false == false is true
When v is Double.MAX_VALUE it is the largest double value.
v != v again is false
isNaN(v) is also false
false == false is true
 
anushree ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thx jessica, and peter,
now i got the result,
thx to everyone to replied my question
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic