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

前端 未结 2 1223
醉梦人生
醉梦人生 2020-12-21 02:57

java.lang.Math#min(double, double):

public static double min(double a, double b) {
    if (a != a) return a; // a is         


        
相关标签:
2条回答
  • 2020-12-21 03:18

    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.
    
    0 讨论(0)
  • 2020-12-21 03:28

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

    min(0.0f/0.0f, 0.0f)

    0 讨论(0)
提交回复
热议问题