- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
That's one for the fridge door.
Admin
Close, but breaks for even numbers from 10-18, calling them odd.
Admin
Close, but breaks for even numbers from 10-18, calling them odd.
Guess I meant to hit "quote". WTF can't comment right.
Admin
Admin
Admin
Admin
I'd say #1 and #3 should be kicked for giving an incorrect solution to a trivial problem (both fail for x < 0, the second fails for x >= 10).
Admin
I'd say the previous commenter should be kicked for failing to read the original requirement, which placed a strict limit on the input values.
Admin
Admin
Admin
I'm not sure if TRWTF is the number of people who don't see that Steve the Cynic is passing in an unsigned int, or if it's how many more comments we get without Discourse.
Admin
None of the functions checks the parameter for range. In the first two, the function will still work. In the version by the "most experienced" programmer, if a bad parameter is passed, there will be a subscript out of range error and garbage will be returned.
Sincerely,
Gene Wirchenko
Admin
We have an Oracle table with two columns, col1_number_id and col2_odd_even with a row for each integer between 0 and 4294967296. Then I just run a simple select SQL to determine if a number is odd or even. Couldn't be simpler. Loading the table took a while since I had to enter each row by hand.
Admin
Some people fail at math.
I had a statistics professor in college.
He drew a picture on the board of a set of circles, arranged in a two by three grid.
He asked a student to count the circles.
The student starting counting. 1, 2, 3...
The professor told him he failed at counting, then announced the answer was 2x3 = 6.
Then he said, now I'm going to teach you how to count probability.
Admin
There was no requirement to check for range. It was specifically stated that the input was limited to 0>n>10 (or >= if you assume 'inclusive'. That means that the function is relieved of the requirement to consider values of n outside of this limit. ... Defensive programming would suggest a limits check anyway, but the stated requirements do not. ... I hope Gene Wirchenko doesn't translate requirements into code for a living.
Captcha: gravis -> It is a gravis error to confuse your understanding of requirements, with what they actually say.
Admin
Addendum (2014-07-22 15:36): s/exercist/exercise/
Admin
Haven't used C in some years so please ignore any syntax errors.
[i]Addendum (2014-07-22 15:37): i = strlen(foo)-1;
and first "foo" comparison should be "foo[i]". Sheesh.
Admin
That's weird, Visual Basic's Str() always uses '.' as decimal separator. Perhaps he used CStr() which is indeed culture specific?
Source: http://msdn.microsoft.com/en-us/library/4y6a1sx7(v=vs.90).aspx
Admin
Yes it does -- we used to use YR2 = MOD(YR, 100) all the time to get a 2-digit year from a 4-digit year.
Admin
And while VB is available only on two's complement binary machines, C89 does not insist on two's complement, or even binary. BZZZT on all the bit twiddlers,
is the only reasonable maximally conformant solution suggested yet.Admin
No, inquiring minds DON'T want to know.
And WTF is getting political? Captcha: abbas
Admin
Admin
You too, with n being int instead of unsigned int, should check what you return for isOdd(-3). For most versions of C the answer depended on the machine. For most machines and I think now for all machines you'd be guaranteed to return the wrong answer.
Admin
Admin
Admin
Admin
Not sure if serious. No need for any calculations based on iBoucle.
isOdd = 1; for (iBoucle = 0; iBoucle < Whatever ; iBoucle++) { isOdd = ! isOdd; if (isOdd) { .... } }
Admin
Admin
Ok, I see some people don't grasp that modulo maths ISN'T sign dependent. Hence it would be best to express what you want as % 2 which is always right regardless of sign or anything else because of what modulo arithmetic is. If the compiler realises that it can optimise this to & 1 then all the better.
Go try determining the truth value of -1 % 2 != 1 % 2 in your favourite language otherwise if you disagree. I'll wait.
Admin
Unfortunately, % is not a modulus operator, it is a remainder after division operator.
Admin
Compilers are perfectly at liberty, to say that (-1 % 2) = -1 or (-1 % 2) = +1, as long as they specify which, and are consistent. So (x % n) is only guaranteed to be in [0,1...n-1] if x is positive.
So, for a compiler that chooses that says (-7 % 2) = -1 then they're really not the same, unless you convert explicitly convert them to bool.
C99 removes the ambiguity, and says x % n must have the same sign as n.
Admin
Not necesarily. Who knows what will happen if a bad parameter is passed. Address exception and process crash are just as likely. Which is fine, because the behaviour is undefined.
Admin
Your education is appreciated sir - I was not aware of the previous state of affairs with regards to the treatment of that operator. Still for the odd/even thing 2 % 2 == -2 % 2 which is zero so anything !(x % 2) is going to be safe here.
Admin
Why is no_laughing_matter still alive?
Admin
Admin
So you can have -1 % 2 = -1 iff -1 / 2 == 0, or you can have -1 % 2 = 1 iff -1 / 2 = -1. Either way is consistent with C89 and C++98/03.
Provided that you compare against zero and not one, as some people have in this thread. if (x % 2 == 0) { /* even */ } is OK; if (x % 2 == 1) { /* odd */ } is not.(And of course omitting the explicit comparison from the conditional is fine, because that's comparing against zero.
Admin
There was no requirement to quit thinking either.
Exactly. Why the limits anyway?
It is part of my living. I would flag such a requirement and go for a function that has no limits, unless those limits are important, in which case, we are back to it being a good idea to limit check.
Sincerely,
Gene Wirchenko
Admin
Sure, infinite recusion is fun :)
Admin
Why is Str not determistic? Who wants a localized decimal point, should use an appropriate function, not Str
Admin
Admin
Admin
Admin
Using nth-child is far less performant than adding a class, especially if it is the right most selector.
Admin
Here's a slightly more subtle (broken) Java odd/even example (from real code):
Hint: it's isOdd() that is broken. :-)
Admin
Oh, as already noted by others above. ;-)