• 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

== behaviour

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.danchisholm.net/oct1/mybook/chapter3/exam1.html


output:
a ::Infinity
b ::Infinity
c ::NaN
true,false,true

how (c==c) is false?
how (a==b) is true?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The rule for NaN (as defined in the language specification) is "The equality operation == returns false if either operand is NaN". So (NaN == anything) will return false.

When you compare two primitives of different type then (where possible) the smaller primitive type will be promoted to the l;arger type for the purpose of the comparison. In this example, a will be promoted to a double. Having established this, it is just a fact of the language that POSITIVE_INFINITY == POSITIVE_INFINITY.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The positive infinity of type float is promoted to the positive infinity of type double. NaN is not equal to anything including itself.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mat,
Can you locate the JLS for the same.
 
Mat Austin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS 15.21.1 states that "In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares unequal to all other values." Therefore if you set both values to the one-and-only POSITIVE INFITIY then an equality comparison will return true.

If the question about NaNs is also answered as part of that same sub-section. However, my earlier quote was from the last paragraph of 4.2.3 of the language specification.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nan is equal to nothing and you cannot compare nothing to nothing.
reply
    Bookmark Topic Watch Topic
  • New Topic