- 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
LOL wtf ...
Admin
I would rather say that this means a lack of coffee, not a real wtf.
Even if I didn't ask someone such a question yet, I guess everybody has those weird thoughts from time to time :D
Admin
This WTF originally racked up 5 pages of comments and alternate solutions!
What more can be said than...
int i;
if(i < 11){
i++;
if(i < 11){
i++;
if(i < 11){
i++;
if(i < 11){
i++;
if(i < 11){
i++;
if(i < 11){
i++;
if(i < 11){
i++;
if(i < 11){
i++;
if(i < 11){
i++;
if(i < 11){
i++;
if(i < 11){
i++;
}
}
}
}
}
}
}
}
}
}
}
*crossing fingers as I click 'Post...'*
Admin
the real wtf here is that the original code doesn't check for integer overflow
Admin
<FONT color=#000000>Wouldn't it have been obvious...</FONT>
variable(++)(++)(++)(++)(++)(++)(++)(++)(++)(++)(++)
or to mix it up a little
(++)(++)(++)(++)(++)variable(++)(++)(++)(++)(++)(++)
Admin
I would have told him the following:
There is no way to increment by 11. The only way to do this is to use ++ 11 times.
So if x = 5 and you need to increment by 11, you have to do the following:
x++;
x++;
x++;
x++;
x++;
x++;
x++;
x++;
x++;
x++;
x++;
Admin
You use +=.
This is not a WTF. It might even be the opposite. Nathan may never have seen += for whatever reason, or it could be a brief zone-out.
Sincerely,
Gene Wirchenko
Admin
what's wrong with x + 11;
Admin
int j = 0;
while( j != 11 ) { j = rand() % RAND_MAX; }
i = i + j + ( 0 + 1 + 0 - 1 + 0 + 1 + 0 - 1 + 0 ) * cos( 0 ) * (cos(i)*cos(i) + sin(i)*sin(i)) + (number_of_correct_proofs_for_P_equals_NP);
And there you have it.
Admin
Actionscript Version
//Frame 1
var i=0;
gotoAndPlay(2);
//frame 2
i++;
nextFrame();
//frame 3
if(i <11){
prevFrame();
}
Admin
for (i = 0; i < 11; i++) {
i++;
}
Admin
I think I saw something about recursion on the other thread...
int i = 3;
inc(ref i, 11);
...
private void inc(ref int num, int amt)
{
if (amt == 0)
return;
amt--;
num++;
inc(ref num, amt);
}
Admin
Heh... guess that should be "if (amt <= 0)" just to make sure that, while stupid, it won't dig its way to China on negative numbers.
Admin
Too bad none of the suggested solutions are thread-safe.
Admin
All above solutions are error prone, anyone knows that the following method that will be included in the Java SDK come next major update looks like this:
int addNumber(int variable, int numberToAdd) {
int variable2 = variable;
for(int i = 0; i < numberToAdd; i++) {
variable2 += (numberToAdd- (numberToAdd--);
}
if (variable + numberToAdd == variable2) {
return variable += numberToAdd;
} else {
return (variable < variable2) ? true : FileNotFound;
}
}
Admin
const int FORTYTWO = 11;
i += FORTYTWO;
Admin
A Modern C++ version:
// Abstract class for Incrementor interface
template<class T>
class IncrementorBase
{
public:
IncrementorBase() {}
virtual ~IncrementorBase() {}
virtual T operator+(const T& rhs) const = 0;
};
// General Purpose Integer Incrementor
// Assumes T has an over-ride for operator+(int)
template<class T, int Amount>
class Incrementor : public IncrementorBase<T>
{
public:
Incrementor() {}
virtual ~Incrementor() {}
virtual T operator+(const T& rhs) const { return T(rhs
};
typedef Incrementor<int, 11> IntElevenIncrementor;
int IncrementByEleven(int i)
{
IntElevenIncrementor i11;
return incr+i;
}
Admin
Here's a nice convoluted Perl version, which could be made more convoluted if it was all in-line.
${(++$i)}++;
${(++$i)}++;
${(++$i)}++;
${(++$i)}++;
${(++$i)}++;
$i++;
The taking a reference ( ( ... ) ) and then dereferencing it ( ${ ... } ) gets around the fact that, in perl, the pre- and post- decrement operators don't return lvalues, thus causing ++$i++ to generate a parse error, and (++$i)++ to bomb with a seemingly custom error message related to exactly this construct.
Can't modify postincrement (++) in preincrement (++)
Admin
x++ increments x. x + 11 does not affect the value of x.
Admin
I love it!
Admin
i = i + ((i << 3) + (i << 2) - i) / i;
Admin
That's right... can't have any of those pesky "magic numbers" floating around in the code.
Admin
...it does if you make it this:
x = x+11;
So really, even not knowing about a += isn't a valid excuse.
Admin
if(x==5) {
x *= 2;
x++;
}
else {
raise IllegalValueException("aaaarrrgh!\n")
}
Admin
Much simpler in Python.
i += int(str(ord("\x01"))*2)
Admin
Just because this comment deserves it....
Admin
Admin
How about:
const long Eleven1th = 11/1
inline long incrementByEleven(long val) return val + Eleven1th
Admin
Why not use tools that you have for free.
Admin
And this is exactly why I refuse to learn C++.
</int,></t></class></class>
Admin
Unfortunately for Nathan, adding eleven is one of the most laborious tasks in programming:
if (x == 1) { x = 12; }
else if (x == 2) { x = 13; }
else if (x == 3) { x = 14; }
...
Admin
Tom Lehrer says that the important thing [in new math] is to understand what you're doing, rather than to get the right answer:
#!/usr/bin/perlmy $val = shift;
my @digits = split(//,$val);
$digits[$#digits]++;
$digits[$#digits -1]++ if $val > 9;
$pow10 = 0;
$x = 0;
for ($j = $#digits; $j >= 0; $j--) {
$x += (10 ** $pow10) * $digits[$j];
$pow10++;
}
print "$x\n";
Admin
Since we are being silly.
i += 011 + 1 + 1;
Admin
Why haven't anyone said the obvious:
// inc i by 11
(++(++(++(++(++(++(++(++(++(++(++i)))))))))));
Admin
No, it's something like this:
When you do numbers larger than 1, you have to split up the plusses you see.
Admin
This is much faster in SQL, so he should use a database:
declare @numTable table ( input int identity, answer int )
declare @i int
set @i = 5000 --substitute the value of i you need to increment as a param in stored proc
declare @counter int
set @counter = 0
insert into @numtable values ( @counter + 12 ) -- identity seed is 1, so we have to use 12
set @counter = @counter + 1
while ( select max(answer) from @numtable ) < ( @i + 11 )
begin
insert into @numtable values ( @counter + 12 )
set @counter = @counter + 1
end
select answer from @numtable where input = @i
Admin
First I thought, this afro-american be trippin'. Then...
>>> 'WTF'*2
'WTFWTF'
Brillant!
Admin
You clowns are all doing it wrong. Here's what you do:
Admin
Nothing cracks me up more than seeing critics try to write "funny" code - only to have it backfire.
Admin
template <class T>
T inc11( T x ) {
return ++++++++++++++++++++++x;
}
Admin
I get this sorta stuff from my coworker all the time!
<FONT color=#0000ff>coworker</FONT>: i have a question...
<FONT color=#ff0000>me</FONT>: alright
<FONT color=#0000ff>coworker</FONT>: i have two subtotal amounts... 31.90 and 49.90... when i add them up in my code, i get the total amount of 99.80... which is way wrong
<FONT color=#0000ff>coworker</FONT>: my code is: this_subtotalprice = subtotal + subtotal
<FONT color=#0000ff>coworker</FONT>: any ideas why it's giving me an extra 19?
*sound of head banging on desk reverberate across world*
Admin
Was this a one-time question, or does he come back with another number?
Nathan: Okay Steve... I incremented 11 times.
Steve: How bout that!
Nathan: Yeah - Turns out, it was too much.
Steve: Sorry to hear that.
Nathan: So... you know how the -- will decrement right?
Admin
Admin
I get to use templates! Nobody told me I could use templates!
There, now you can increment by lots of different values! Just do
incby<11>(i);
Admin
Then Steve responds:
Steve: Nathan...hang on a minute...
Steve: { gets on the phone with "coworker" from the thread above }
Steve: coworker...hey, it's Steve.
Steve: Listen, Nathan's in my office... "N-A-T-H-A-N" yeah.
Steve: He's got a project that the two of you should collaborate on.
Steve: He'll be right up.
-- sorry Mike.
Admin
A few days later...
Nathan: Hey Steve, thanks, that project really came out great.
Steve: sniggers Yeah, great.
Nathan: Do you mind if I just ask you one more question?
Steve: Er, ok, go for it.
Nathan: Well, I figured out how to get it back to 5 from 11, but I want to increment it by another half. How do I do that?
Steve: <_<
Admin
<FONT face=Garamond>Now, see, Nathan never said he wanted to increment by 11 QUICKLY. So, here is one alternative:</FONT>
<FONT face=Garamond color=#008000>double number_you_want_to_increment = 0;</FONT>
<FONT face=Garamond color=#008000>double numero = 11;</FONT>
<FONT face=Garamond color=#008000>double temporary_variable_to_hold_stuff = number_you_want_to_increment;</FONT>
<FONT face=Garamond color=#008000>while (numer_you_want_to_increment < temporary_variable_to_hold_stuff + 11)</FONT>
<FONT face=Garamond color=#008000>{</FONT>
<FONT color=#008000> <FONT face=Garamond>numero = numero / 2 ;</FONT></FONT>
<FONT color=#008000> <FONT face=Garamond>number_you_want_to_increment += numero;</FONT></FONT>
<FONT face=Garamond color=#008000>}</FONT>
<FONT face=Garamond>I think this gets puuuuurty close to incrementing a number by 11. [6]</FONT>
<FONT face=Garamond></FONT>
Admin
But Insidious and Elusive numbers are no problem at all!
How aboot:
x += 0xB
x += 013
x += 1110.toDecimal()
Admin
I'd like to see some code that almost adds 11.
Preferrably by introducing floating point errors.
Admin
Heh, never quite gets there, does it?