i=Double.NaN
while(i==i)
{
//some code
}
what is the output? Why don't we have a Integer.NaN
?
IEEE floating points have a "Not a Number" representation by spec. Integral types do not have such a state. Every possible binary representation of an integer is a real number.
what is the output?
There is no output because NaN != NaN as per the IEEE 754 standard, so the loop will never be entered.
Why don't we have a Integer.NaN?
Because Integers are based on a two's complement binary representation where every bit pattern is a valid integer, and none have any special meaning.
Double.NaN == x
始终为false
,无论x
是什么。
I cannot tell you why but you can work around it with this:
(int)Double.NaN;
so my guess is that there is not a good reason.
For float
and double
NaN is not equal to anything even itself.
For int
and long
types I have used MIN_VALUE as a value like NaN, but you have to code this yourself, if you want it to work this way.
BTW: There is a puzzler, when is the following an infinite loop.
while(x != x + 0);
There are three types for x
when this is an infinite loop.
Another is when is this an infinite loop.
while(x == -x);
There is 16 type/value combinations for this, much more than you might expect. ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.