[英]Properties of Double.NaN and why does Integer wrapper not have an NaN data memeber?
i=Double.NaN
while(i==i)
{
//some code
}
what is the output? 什么是输出? Why don't we have a Integer.NaN
? 为什么我们没有Integer.NaN
?
IEEE floating points have a "Not a Number" representation by spec. IEEE浮点按规范具有“非数字”表示。 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. 由于根据IEEE 754标准NaN!= NaN,因此没有输出,因此将永远不会输入循环。
Why don't we have a Integer.NaN? 为什么我们没有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. 对于float
和double
NaN甚至不等于任何东西。
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. 对于int
和long
类型,我已经使用MIN_VALUE作为类似NaN的值,但是如果您希望它以这种方式工作,则必须自己编写代码。
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. 当这是一个无限循环时, x
有三种类型。
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. 有16种类型/值组合,远远超出您的预期。 ;) ;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.