[英]Infinity is equal to Infinity in Java
I wanted to test if infinity is equal to infinity in Java: 我想测试Java中的无穷是否等于无穷:
Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY
I was surprised by the result when it turned out to be true. 事实证明我对结果感到惊讶。 My question is how can two infinite values be equal?
我的问题是两个无限值如何相等?
Because Double.POSITIVE_INFINITY
represents a specific number, so comparing it to itself using ==
should return true. 因为
Double.POSITIVE_INFINITY
表示一个特定的数字,所以使用==
与它本身进行比较应该返回true。
This behaviour is specified explicitly in JLS Sec 15.21.1 : JLS Sec 15.21.1中明确指定了此行为:
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
浮点相等性测试是根据IEEE 754标准的规则执行的:
...
...
Otherwise, two distinct floating-point values are considered unequal by the equality operators.
否则,相等运算符会将两个不同的浮点值视为不相等。
In particular, there is one value representing positive infinity and one value representing negative infinity;
特别地,有一个值代表正无穷大,一个值代表负无穷大; each compares equal only to itself, and each compares unequal to all other values.
每个比较仅等于自身,并且比较不等于所有其他值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.