0

I have a question if there is a value in Java that is equivalent to Double.NaN. It is common knowledge that Double.NaN is equivalent to Double.longBitsToDouble(0x7ff8000000000000L). I tried this in C++:

long d = (long)0x7ff8000000000000L;
cout << setprecision(308) << *((double*)&d) << endl;

and recieved this number: 1.7913854515396173439813246875759771148370355414969747463437939834712133067539348971091007957318376384187247712070435161526222960236442304023361887799416099781498803833913104211684781256626064276104912473088500848240020138295465213393610297503047186864315005421110542290050185011298999959710557864144337912608e-307. But I tried this in Java and it didn't work. Is there a double equivalent to Double.NaN in Java?

2 Answers 2

2

In Java we have the constant Double.NaN, which according to the documentation is the same as Double.longBitsToDouble(0x7ff8000000000000L). Why do you expect it to be equal to C++'s representation of NaN?

Your C++ conversion seems faulty, and also according to the docs of Double.longBitsToDouble there can be differences in the bit patterns returned, even if it's IEEE 754 compliant. Don't depend on such low-level implementation details for your code.

0

NaN is not a number, therefore you must not attempt to perform arithmetic on it, for example, comparing it to another number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.