Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Why a NaN variable is not equal to Double.NaN?  (Read 1115 times)
0 Members and 1 Guest are viewing this topic.
Offline tom_mai78101
« Posted 2014-04-02 03:59:07 »



In the picture shown, Eclipse is telling me that the double variable, "unprocessed", is NaN. However, when comparing it to Double.NaN, it tells me the Boolean value is "false".

Why? And how do I fix it?
Offline Stranger

Senior Duke


Medals: 7



« Reply #1 - Posted 2014-04-02 04:48:06 »

how do I fix it?
Do the following:
1  
2  
3  
4  
double x = ... 
if (Double.isNaN(x)) {
    ...
}

Anton
Offline tom_mai78101
« Reply #2 - Posted 2014-04-02 05:18:08 »

Thanks, stranger, totally fixed the NaN problem I keep having. Fitting name, by the way.  Pointing
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2014-04-02 05:24:36 »

Keep in mind that NaN values are not even equal to themselves:
1  
boolean isNaN =   unprocessed != unprocessed; // true

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline tom_mai78101
« Reply #4 - Posted 2014-04-02 06:43:34 »

Interesting. Thanks for the tip.
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2014-04-02 08:46:15 »

This is an admittedly braindead feature in the language which IMHO breaks one of those principles of least surprise.

Cas Smiley

Offline Roquen
« Reply #6 - Posted 2014-04-02 10:08:26 »

No: It's a desirable feature.  x != x
Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« Reply #7 - Posted 2014-04-02 10:54:25 »

autoboxing + NaN = potential for much pain

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #8 - Posted 2014-04-02 10:55:02 »

I mean if you really think about it, how can two "numbers" that aren't even numbers be equal to each other? If they aren't numbers in the first place... Its kind of hard to be equal to another not number.

Offline Regenuluz
« Reply #9 - Posted 2014-04-02 10:56:55 »

Personally I think it makes perfect sense that NaN != NaN. Smiley All we know about "NaN" is that is that it's not a number, nothing more, hence we don't know if they're equal. ^_^
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #10 - Posted 2014-04-02 11:09:00 »

The IEEE standard is pretty well thought out.  The way the special values: NaN, Infs and signed zeroes all have reasons.
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #11 - Posted 2014-04-02 11:24:19 »

The problem is that when we say == we are usually not exactly referring to mathematical equality but simply trying to say "is this not a number". Which is exactly how you read if x == NaN. It's just plain daft to need a method call to say this as it leads to very subtle errors, almost always in edge cases because NaN very rarely appears. It's rather like how the designers of SQL thought they were being clever by insisting we use IS NULL and IS NOT NULL rather than = NULL and <> NULL. Foolish.

Cas Smiley

Offline Roquen
« Reply #12 - Posted 2014-04-02 11:46:58 »

Not at all.  The reason is sound.  If you don't want to call a method then if (x != x) is only true if NaN and if (x==x) is true for any non-NaN.  Having NaN==NaN would drastically complicate a fair number of numeric routines.
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #13 - Posted 2014-04-02 12:18:40 »

I don't quite follow that... have you got some particular use-cases as example?

(I think the real reason NaN != NaN is because there are > 1 binary representations of NaN but there we go)

Cas Smiley

Offline Roquen
« Reply #14 - Posted 2014-04-02 12:25:45 »

if (a == b)
would have to become
if (a == b && !isNaN(a))
as an example
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #15 - Posted 2014-04-02 12:35:47 »

I must be being dense but I still don't follow that.

Cas Smiley

Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #16 - Posted 2014-04-02 13:55:51 »

I must be being dense but I still don't follow that.

Cas Smiley

While I lean towards your viewpoint Princec, I do understand Roquen's example after thinking about it for a moment.

If a == NAN, b == NAN, and NAN == NAN, then you would need the extra check in the example given. While a would equal b, neither a nor b would be guaranteed to be valid numbers within the scope of the if statement, thus the extra check for !isNAN.

I would think a more "Java-centric" approach would have been a "NAN exception", but I imagine the extra overhead of the implementation would have a larger impact on performance than one might initially think.

Just my $0.02. Huh

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #17 - Posted 2014-04-02 14:14:18 »

Still don't get it  Emo

Let's just for the sake of a straw man declare that NaN is represented in binary by some magic number eg. 0xFFFFFFFF.

I want to know if a == NaN. Is a == 0xFFFFFFFF? Yes? Good.
I want to know if b == NaN. Is b == 0xFFFFFFFF? Yes? Good.
I want to know if a == b. I'm entirely happy with a == b being true for any values of a and b. I'm also happy that if NaN == NaN then a == b here too.
I just want to know is a is NaN or b is NaN.
I'm happy that NaN == NaN in my world. That makes sense to me. Very much like null == null in Java (but irritatingly, not SQL).

What am I missing?

Cas Smiley

Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #18 - Posted 2014-04-02 14:47:59 »

You know that a == b, but what is your guarantee that a and b are usable values for a mathematical operation if NAN == NAN without the extra check mentioned earlier (!isNAN(a))? I think that's the point Roquen is trying to make.

Also consider that Null is indicative of a single state, so Null == Null makes sense. Without this becoming a discussion of Jean-Paul Sartre, nothing should always equal nothing. Nan indicates that there is a value being pointed to, but it's not the type of value that we can work with. If I have a box of items that are not apples, does that mean that everything in that box is the same item, or could I have varying items in the box whose only similarity is that they're not apples? Wink

Damn, philosophy and code before caffeine is going to make my head hurt. LOL

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #19 - Posted 2014-04-02 15:57:23 »

Indeed, but that usage of NaN is about as useful as a chocolate kettle to most of us.

Cas Smiley

Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #20 - Posted 2014-04-02 15:58:37 »

In SQL, NULL pretty much behaves like NaN in Java.

SELECT NULL = NULL
->
NULL

SELECT NULL != NULL
->
NULL

SELECT NULL <> NULL
->
NULL

SELECT NULL <=> NULL
->
1
(true)
SELECT NULL IS NULL
->
1
(true)
SELECT NULL IS NOT NULL
->
0
(false)
SELECT NULL = 13
->
NULL

SELECT NULL <=> 13
->
0
(false)

Also consider that Null is indicative of a single state, so Null == Null makes sense. [...] nothing should always equal nothing.
You can equally reason that
null
is nothing, unknown, and cannot be compared with another unknown.


The difference, however is, that in Java, NaN is a range of values, and null is basically the integer zero (for the VM). In theory the VM could have been implemented that a null value would be defined as any value below 1, but the language designers were sane enough to follow C's example, as opposed to SQL's.

One advantage of SQL's NULL is that you can have unlimited NULLs in a set (as used by UNIQUE INDEX).

One advantage of Java's NaN is that it's 'hardware accelerated' (x87)



Slightly more on topic: it would be nice to have (additional) floating point types in Java that raised exceptions when the result of an operation was NaN, as to avoid silent errors. Just like dividing by zero for integers.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #21 - Posted 2014-04-02 16:25:30 »

The reason that Cas dragged SQL into the discussion, is that in SQL, NULL pretty much behaves like NaN in Java.

Indeed, and the point is valid. I was just discussing the reasoning behind the Java devs choice to implement Nan as they did. When it comes to SQL implementation, that's something directed by a body external to Oracle so there isn't really a real choice of implementation in that instance.

I guess the best way I can describe my thinking on it is this: Null completely defines what something is whereas Nan partially defines what something isn't. It's an interesting discussion either way; I can't say I've ever given much thought to the reasoning behind the implementation until now. It reminds me of this video discussing the foibles in various language implementations. Smiley

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #22 - Posted 2014-04-02 16:43:09 »

It's beginning to look like it's a binary-compatibility thing with the IEEE standards for NaN representation in binary that's made it work this way. If that's how they've had to implement NaN in hardware, then so be it.

Cas Smiley

Offline Roquen
« Reply #23 - Posted 2014-04-02 18:25:29 »

Short answer:  If NaNs were to compare as equal in no cases would the number of comparisons decrease and many cases the number would increase.

consider two variable a & b, all the comparisons except "!=" which result in 'true' tell you a & b are ordered and therefore neither are NaNs.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #24 - Posted 2014-04-02 19:20:34 »

Please, use, commas, Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #25 - Posted 2014-04-02 21:32:32 »

I still think it would have been reasonable to use a single binary value for NaN and thus allow == to work "as expected". <, >, != could all be intrinsically designed to work with it too.

Cas Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (30 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!