The issue is not a[5]
vs. 5[a]
. It is that NaN is never equal to anything, including itself. This gnu.org page has more details on comparing NaN and Infinity.
why C compiler gets wrong when a[5]==5[a] [closed]
Question
Why does this happpens
double zero = 0.0;
double a[] = { 0,0,0,0,0, zero/zero}; // NaN
cout << (a[5] == 5[a] ? "true" : "false") << endl;
prints
false
Solution 2
OTHER TIPS
The compiler isn't getting it wrong, and it has nothing to do with a[5] == 5[a]
. zero/zero
is a nonsense expression and gets assigned NaN
as a result but NaN != NaN
because it can be produced in multiple non-equal ways.
Mind you, checking it out I think this is actually strictly undefined behaviour because the standard doesn't actually require the use of IEEE 754, or equivalent, floating point standards and can actually not support NaN. Where it does support NaN, section 7.2.14 (in the 2011 draft standard) requires that NaN comparisons return false.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow