2 ms·
But you can create a self-modifying function that, ‘for speed’, adds each case when it is called. The initial function should start with checking for special c
by Someone 3y ago
But you can create a self-modifying function that, ‘for speed’, adds each case when it is called.
The initial function should start with checking for special cases NaN, infinities, 0 and -0, then do (may not be valid JavaScript)
if((x++ === x) && (x—- === x)) printf("even\n");
to handle cases over 2^54 or below -2^54, then do
isOdd = true
neg = x
pos = x
while(true) {
if(neg++ === 0) break;
if(pos-- === 0) break;
isOdd = !isOdd
}
to determine whether x is odd or even in isOdd. Doing the self-modification is left as an exercise.
This is good defensive programming. It avoids doing a tricky division by 2.0 that might be buggy (I don’t think https://en.wikipedia.org/wiki/Pentium_FDIV_bug https://en.wikipedia.org/wiki/Pentium_FDIV_bug was affected, but who knows what other FPUs do?)