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
float i = Float.NaN;
while(i == i) ;
System.out.println("Not infinite!");
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);
}
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.
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.
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) { }
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>=
returnfalse
if either or both operands areNaN
. The equality operator==
returnsfalse
if either operand isNaN
, and the inequality operator!=
returnstrue
if either operand isNaN
. In particular,x!=x
istrue
if and only ifx
isNaN
, and(x<y) == !(x>=y)
will befalse
ifx
ory
isNaN
.