How can “while (i == i) ;” be a non-infinite loop in a single threaded application?

前端 未结 12 1103
甜味超标
甜味超标 2020-12-04 09:41

I just got a question that I can\'t answer.

Suppose you have this loop definition in Java:

while (i == i) ;

What is the type of

相关标签:
12条回答
  • 2020-12-04 10:19
    float i = Float.NaN;
    while(i == i) ;
    System.out.println("Not infinite!");
    
    0 讨论(0)
  • 2020-12-04 10:19

    Since others said it's NaN I got curious about the official (JDK 6) implementation of Double.isNaN, and behold:

    /**
     * Returns <code>true</code> if the specified number is a
     * Not-a-Number (NaN) value, <code>false</code> otherwise.
     *
     * @param   v   the value to be tested.
     * @return  <code>true</code> if the value of the argument is NaN;
     *          <code>false</code> otherwise.
     */
    static public boolean isNaN(double v) {
        return (v != v);
    }
    
    0 讨论(0)
  • 2020-12-04 10:20

    I know this is a Java question, but considering the question for other languages is intriguing.

    In C, a simple type such as 'int' could exhibit 'terminate before the universe grows cold' behaviour if 'i' was declared as a volatile (so the compiler would be forced to do two reads of 'i' for each iteration) and if 'i' was actually in memory where something else could affect it. Then the loop would terminate when 'i' changed between the two reads of a single iteration. (Added: a possible place - in a micro-computer where 'i' is actually located at the address of an I/O port, perhaps connected to a position sensor. It would be more plausible if 'i' was a pointer variable (a pointer to volatile memory) and the statement was 'while (*i == *i);'.)

    As evidenced by other answers, in C++, the '==' operator can be supplied by the user if i is of a user-defined class, so anything might be possible.

    Rather like NaN, in an SQL-based language, the loop would not be infinite if the value of i was NULL; however, any non-NULL value would make the loop infinite. This is rather like Java, where any number (as opposed to NaN) makes the loop infinite.

    I do not see the construct having any practical use, but it is an interesting trivia question.

    0 讨论(0)
  • 2020-12-04 10:27

    I'm not sure, but I believe (i == i) is not atomic operation in multithreaded process, so if i value will be changed by other thread between pushes of it's value to stack on thread executing the loop, then that condition can be false.

    0 讨论(0)
  • 2020-12-04 10:28

    I would add

    float i = Float.NaN;
    

    as well as

    double i = Double.NaN;
    

    A common trick in these sort of questions it in the assumption you make that i is an int. Other common assumptions might be s is a String, x,y are a double, ch is a char, b is a byte etc. If you see a question like this you can bet that 'i' is not its expect type.

    A similar question is; This never loops, what is 'x'

    while(x == x && x != x + 0) { }
    

    Another question I quite like is; This loop is an infinite loop, what are the possible values of x. (: I count twelve of them :)

    while(x != 0 && x == -x) { }
    
    0 讨论(0)
  • 2020-12-04 10:29
    double i = Double.NaN;
    

    The API for Double.equals() spells out the answer: "Double.NaN==Double.NaN has the value false". This is elaborated in the Java Language Specification under "Floating-Point Types, Formats, and Values":

    NaN is unordered, so the numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN. The equality operator == returns false if either operand is NaN, and the inequality operator != returns true if either operand is NaN. In particular, x!=x is true if and only if x is NaN, and (x<y) == !(x>=y) will be false if x or y is NaN.

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