3 ms·
A trickier variant of #11 is to show people bool is_zero(int x) { return x == -x; } bool is_zero(float x) { return x == -x; }
by Veedrac 12y ago
A trickier variant of #11 is to show people
bool is_zero(int x) {
return x == -x;
}
bool is_zero(float x) {
return x == -x;
}
and ask them which is wrong and for what value. Most of the time the instinctive response is that it must be the float code (because floats are evil, duh).
This works even in languages with defined overflow for integers.
- rav 12y agoBut you should give the functions different names, since it is not valid to define the same symbol more than once (and there's no name mangling in C that enables function overloading).