Vishrant
Vishrant

Reputation: 16628

Casting NaN value

I simply learned that NaN is unordered and I was just playing with Float and Double NaN and I tried following snippet:

System.out.println("Comparing with isNaN method: " + Float.isNaN((float)Double.NaN));

I got a question if NaN is Not-a-Number then why casting of NaN is allowed. I want to know what happen at Architectural Level when I cast NaN value.

Upvotes: 1

Views: 574

Answers (2)

user1399290
user1399290

Reputation:

In the javadoc you can see that Double.NaN is a Constant of the type Double, so you can cast! Double.Nan is a valid number. http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533472

When you cast, NaN of one type is mapped to NaN of the other type. This is done by the CPU so what you see in Java is what happens at the "Architectural Level"

Upvotes: 1

Related Questions