• Post Reply Bookmark Topic Watch Topic
  • New Topic

Float.NaN  RSS feed

 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is (Float.NaN == Float.NaN) returning false ??
as per documentation :
public static final float NaN
A constant holding a Not-a-Number (NaN) value of type float. It is equivalent to the value returned by Float.intBitsToFloat(0x7fc00000).
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short answer: Because the Java Language Specification says so, in paragraph 4.2.3:

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.

    In particular, (x<y) == !(x>=y) will be false if x or y is NaN.


  • 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.


  • NaN means "not a number", it's a special value that is used to indicate that the result of some computation is not a valid number.

    When you compare NaN with anything, the result is always false, even if that other thing is NaN.

    It doesn't really make things to compare "not a number" with anything, so the designers of the Java language chose to do what is in the specification.

    If you need to check if a float has the special "not a number" value, use Float.isNaN(...):
     
    Paweł Baczyński
    Bartender
    Posts: 2083
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Funny thing. This behaviour is used to implement isNan method in Float class ;)
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That implementation is perfectly in line with the specification:
    In particular, x!=x is true if and only if x is NaN.
     
    Shubham Semwal
    Ranch Hand
    Posts: 176
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Got it.. Thanks
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If NaN equals NaN, then you can prove that 3 equals 2. I challenge you to do it. It shouldn't take more than five minutes.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper de Jong wrote:Short answer: Because the Java Language Specification says so, in paragraph 4.2.3:



    And the reason the JLS says so, is because Java follows the IEEE 754 standard for floating points -- and the IEEE 754 specification says so.

    Henry
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!