In which case could quot;a != aquot; return quot;truequot;?(在这种情况下,可以“a != a;返回“真?)
问题描述
java.lang.Math#min(double, double):
public static double min(double a, double b) {
if (a != a) return a; // a is NaN
if (a == 0.0d && b == 0.0d && Double.doubleToLongBits(b) == negativeZeroDoubleBits) return b;
return (a <= b) ? a : b;
}
在什么情况下 a != a 会返回 true?似乎是当 a 为 NaN 时,但我无法想象一个例子.可以提供一份吗?
In which case could a != a return true? It seems that it's when a is NaN, but I can't imagine an example. Could you please provide one?
推荐答案
一个简单的例子是
double d = Double.NaN; // or
double d = 0.0/0.0; // or
double d = Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY;
if (Double.isNaN(a)) { // tests if a != a
// do something
BTW Double.compare() 确实认为 NaN 相等
BTW Double.compare() does see NaN as equal
if (Double.compare(d, d) == 0) // always.
对于任何类型和值,多线程都是可能的.例如
With multiple threads this is possible for any type and value. e.g.
if (a != /* another thread changes 'a' */ a) {
// a thread changed a while you were looking at it.
这篇关于在这种情况下,可以“a != a";返回“真"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在这种情况下,可以“a != a";返回“真"?
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
