Programming Question: When X != X?
Just curious about it:
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?
12 comments
[ 5.1 ms ] story [ 43.1 ms ] threadAsking 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.
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.
Their behaviour is defined by the standards and special checks are required to identify these values properly. In SQL, it's
, in Javascript it'd be .rand() (well, most of the time[1]). This you just have to know :)
Improperly expanding C macros:
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.
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.