• 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 class Double

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
v != v == Double.isNaN(v)
Could anyone of you tell the behaviour of the above statement when v
takes the following values
Double.NaN;
Double.POSITIVE_INFINITY;
Double.MAX_VALUE;
Thanks,
Lakshmi.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about you simply try it out and see what happens? If you're confused by what actually happens, let me know.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lakshmi Saradha:
v != v == Double.isNaN(v)
Could anyone of you tell the behaviour of the above statement when v
takes the following values
Double.NaN;
Double.POSITIVE_INFINITY;
Double.MAX_VALUE;
Thanks,
Lakshmi.


First of all, the expression is invalid because isNan() returns a boolean.
NaN constant is not equal to anything including itself or another NaN:


NaN is unordered, so the numerical comparison operators <, <=, >, and >=
return false if either or both operands are NaN (�15.20.1). The equality operator == returns false if either operand is NaN, and the inequality operator != returns true if either operand is NaN (�15.21.1). In particular, x!=x is true if and only if x is NaN, and (x<y) == !(x>=y) will be false if x or y is NaN.


MAX_VALUE, POSITIVE_INFINITY, and NEGATIVE_INFINITY do not have any peculiar behavior like NaN. Double.MAX_VALUE==Double.MAX_VALUE returns true. The same applies to the infinities.
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks vf.
The actual piece of code is
class D {
static boolean m(double v) {
return(v != v == Double.isNaN(v)); //line 1
}
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));
}}
'v' is of type double in line 1. I was a bit confused about the precedence of operation. Now I realise that == and != have the same level of precedence and when occuring in the same line , they are evaluated from left to right . Am I right?
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now I realise that == and != have the same level of precedence and when occuring in the same line , they are evaluated from left to right.


You're right, it turns out that the expression is valid. :roll:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic