In which case could “a != a” return “true”?

落爺英雄遲暮 提交于 2019-11-29 14:40:30

A simple example is

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() 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.

If a is NAN, and NAN is for example divide by zero

min(0.0f/0.0f, 0.0f)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!