4 points · by malkia · 15y ago · 11 comments
Q1: In what situations X != X? Please, explain it in your language of choosing. Also consider different implementations, or run-time/compile-time versions of said language, system.
Q2: What are the common pitfalls associated with if such behavior exists?
Their behaviour is defined by the standards and special checks are required to identify these values properly. In SQL, it's
X IS (NOT) NULL
, in Javascript it'd be isNaN(X)
.rand() (well, most of the time[1]). This you just have to know :)
Improperly expanding C macros:
#define isupper(c) ((c) >= 'A' && (c) <= 'Z')
#define X isupper(getchar())
printf("X == X = %d\n", X == X);
And like lurker14 said, anything in general that has side-effects.[1] It is possible rand() will return the same value twice in a row, just not very likely. Also the original author might have been twisted enough to memoize it. Thank you, "Higher order Perl", for that idea.
"test" != "test"
Edit: To be specific, it results in undefined behaviour. The compiler may in fact allocate only a single block of memory for two literals if they are the same, which would allow this to evaluate to true.You will have to use is.na(X) to find is X is NA.
So you don't just handle TRUE and FALSE cases but take NA into consideration as well.
x = .999... 10x = 9.999... 10x-x = 9.999... - .999... 9x = 9 x = 1 .999... != 1
You prove above that 0.9 recurring equals 1 (the final step does not follow from what you write before), not that 0.9 recurring does not equal itself.
Asking the right question provides half of the solution already.
For example a sorting routine might never finish, if it has to sort such numbers/values.