Comment On If ++ Increments ... (++)

Another slow day, another revisited post. Even if you've seen the original, I highly recommend checking out the comments posted. There you will find a number of solutions (five pages worth) to the problem that Steve Local's ... less gifted ... colleague was having in C# ... [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: If ++ Increments ... (++)

2005-12-29 14:11 • by Sammie
LOL wtf ...

Re: If ++ Increments ... (++)

2005-12-29 14:15 • by ellion
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

Re: If ++ Increments ... (++)

2005-12-29 14:23 • by R.Flowers

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...'*

Re: If ++ Increments ... (++)

2005-12-29 14:27 • by community
the real wtf here is that the original code doesn't check for integer overflow

Re: If ++ Increments ... (++)

2005-12-29 14:30 • by Maz

Wouldn't it have been obvious...


variable(++)(++)(++)(++)(++)(++)(++)(++)(++)(++)(++)


or to mix it up a little


(++)(++)(++)(++)(++)variable(++)(++)(++)(++)(++)(++)

Re: If ++ Increments ... (++)

2005-12-29 14:36 • by ferrengi
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++;



Re: If ++ Increments ... (++)

2005-12-29 14:41 • by Gene Wirchenko
Alex Papadimoulis:

Another slow day, another revisited post. Even if you've seen the original, I highly recommend checking out the comments posted. There you will find a number of solutions (five pages worth) to the problem that Steve Local's ... less gifted ... colleague was having in C# ...


Nathan: Steve, you know how ++ will increment, right?
Steve: Right ....
Nathan: Okay, so how do you increment by 11?



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

Re: If ++ Increments ... (++)

2005-12-29 14:44 • by mike
54942 in reply to 54939
what's wrong with x + 11;

Re: If ++ Increments ... (++)

2005-12-29 14:48 • by take that spam

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.

Re: If ++ Increments ... (++)

2005-12-29 14:51 • by David
Actionscript Version

//Frame 1
var i=0;
gotoAndPlay(2);

//frame 2
i++;
nextFrame();

//frame 3
if(i <11){
prevFrame();
}

Re: If ++ Increments ... (++)

2005-12-29 14:58 • by Anonymous
54948 in reply to 54935
for (i = 0; i &lt; 11; i++) {

    i++;

}

Re: If ++ Increments ... (++)

2005-12-29 15:00 • by Charlie Marlow
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);
    }

Re: If ++ Increments ... (++)

2005-12-29 15:02 • by Charlie Marlow
54950 in reply to 54949
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.

Re: If ++ Increments ... (++)

2005-12-29 15:05 • by Grant
Too bad none of the suggested solutions are thread-safe. 

Re: If ++ Increments ... (++)

2005-12-29 15:06 • by Chucara
54952 in reply to 54947
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;
    }
}

Re: If ++ Increments ... (++)

2005-12-29 15:17 • by andy
const int FORTYTWO = 11;



i += FORTYTWO;

Re: If ++ Increments ... (++)

2005-12-29 15:37 • by jayinbmore
A Modern C++ version:



// Abstract class for Incrementor interface

template

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 Incrementor : public IncrementorBase

{

public:

 Incrementor()  {}

 virtual ~Incrementor() {}

 virtual  T operator+(const T& rhs) const { return T(rhs
+ Amount); } // it's safe to do this becuase addition is commutative

};

typedef Incrementor IntElevenIncrementor;



int IncrementByEleven(int i)

{

 IntElevenIncrementor i11;

 return incr+i;

}







Re: If ++ Increments ... (++)

2005-12-29 15:40 • by Andy
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 (++)





Re: If ++ Increments ... (++)

2005-12-29 15:47 • by RogerC
54957 in reply to 54942
Anonymous:
what's wrong with x + 11;


x++ increments x. x + 11 does not affect the value of x.

Re: If ++ Increments ... (++)

2005-12-29 15:48 • by Adam
54958 in reply to 54953

Anonymous:
const int FORTYTWO = 11;

i += FORTYTWO;


 


I love it!

Re: If ++ Increments ... (++)

2005-12-29 16:05 • by Otto
i = i + ((i << 3) + (i << 2) - i) / i;

Re: If ++ Increments ... (++)

2005-12-29 16:06 • by eimaj2nz
54960 in reply to 54958
Anonymous:

Anonymous:
const int FORTYTWO = 11;
i += FORTYTWO;

I love it!


That's right... can't have any of those pesky "magic numbers" floating around in the code.

Re: If ++ Increments ... (++)

2005-12-29 16:13 • by Calophi
54961 in reply to 54957
RogerC:
Anonymous:
what's wrong with x + 11;


x++ increments x. x + 11 does not affect the value of x.



...it does if you make it this:

x = x+11;

So really, even not knowing about a += isn't a valid excuse.

Re: If ++ Increments ... (++)

2005-12-29 16:42 • by Joost_
54963 in reply to 54961
if(x==5) {


  x *= 2;



  x++;



}



else {



  raise IllegalValueException("aaaarrrgh!\n")



}

Re: If ++ Increments ... (++)

2005-12-29 16:46 • by asdyt
54964 in reply to 54961
Much simpler in Python.

i += int(str(ord("\x01"))*2)

Re: If ++ Increments ... (++)

2005-12-29 16:52 • by TankerJoe
Just because this comment deserves it.... 







    i++;i--;i++;i--;i++;
i++; i++;
i++; i--; i--; i++;
i++; i--; i--; i++;
i++; i++;
i++; i--; i++;
i++; i++;
i++; i--; i--; i++;
i++; i--;i--; i++;
i++; i++;
i++;i--;i++;i--;i++;





Re: If ++ Increments ... (++)

2005-12-29 17:18 • by EV
I always thought it was something like this (excuse me my formatting, I really don't know how to use this forum ;)):

Private
Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long,
ByVal dy As Long, ByVal cButtons As Long,
ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
Private Const MOUSEEVENTF_MIDDLEUP = &H40
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Private Sub inc(iNumToInc11Times As Integer)
SetCursorPos 32, Screen.Height / 15 - 10
mouse_event MOUSEEVENTF_ABSOLUTE, 32, Screen.Height / 15 - 10, 1, 1
mouse_event MOUSEEVENTF_LEFTDOWN, 32, Screen.Height / 15 - 10, 1, 1
mouse_event MOUSEEVENTF_LEFTUP, 32, Screen.Height / 15 - 10, 1, 1
SendKeys ("R")
SendKeys ("calc")
SendKeys (vbKeyReturn)
SendKeys (iNumToInc11Times + "+11")
SendKeys (vbKeyReturn)
' TODO: Add some keys to copy it to the clipboard
inc = Clipboard.GetText()
End Sub

Re: If ++ Increments ... (++)

2005-12-29 17:52 • by Seriously_WTF
How about:

const long Eleven1th         = 11/1
inline long incrementByEleven(long val) return val + Eleven1th

Re: If ++ Increments ... (++)

2005-12-29 17:57 • by Kiss me, I'm Polish
54971 in reply to 54965

Why not use tools that you have for free.


function AddElevenToX( int x ) {
  System.Clipboard.put( x );
  System.run("calc.exe");
  System.keyboard.press("V", "Ctrl");
  System.keyboard.press("+", none);
  System.keyboard.press("1", none);
  for ( i = 1; i <= 11; i++ ) {
    System.keyboard.press( "=", none);
  }
  System.keyboard.press("C", "Ctrl");
  System.Clipboard.get( x );
 return x;
}

Re: If ++ Increments ... (++)

2005-12-29 18:24 • by GuruBuckaroo
54973 in reply to 54954
Anonymous:
A Modern C++ version:



// Abstract class for Incrementor interface

template

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 Incrementor : public IncrementorBase

{

public:

 Incrementor()  {}

 virtual ~Incrementor() {}

 virtual  T operator+(const T& rhs) const { return T(rhs
+ Amount); } // it's safe to do this becuase addition is commutative

};

typedef Incrementor IntElevenIncrementor;



int IncrementByEleven(int i)

{

 IntElevenIncrementor i11;

 return incr+i;

}


And this is exactly why I refuse to learn C++.

Re: If ++ Increments ... (++)

2005-12-29 18:25 • by poss
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; }

...





Re: If ++ Increments ... (++)

2005-12-29 18:27 • by Lobachevsky

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/perl
my $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";

Re: If ++ Increments ... (++)

2005-12-29 18:27 • by Rick
54976 in reply to 54971
Since we are being silly.

i += 011 + 1 + 1;

 

Re: If ++ Increments ... (++)

2005-12-29 18:42 • by Mikademus
54977 in reply to 54976
Why haven't anyone said the obvious:



// inc i by 11

(++(++(++(++(++(++(++(++(++(++(++i)))))))))));

Re: If ++ Increments ... (++)

2005-12-29 18:42 • by Omnifarious

No, it's something like this:


i = ((i / 11) + 1) * 11 + (i % 11);

When you do numbers larger than 1, you have to split up the plusses you see.

Re: If ++ Increments ... (++)

2005-12-29 19:13 • by maldrich
54986 in reply to 54978
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




Re: If ++ Increments ... (++)

2005-12-29 19:41 • by Joost_
54987 in reply to 54964
Anonymous:
Much simpler in Python.



i += int(str(ord("\x01"))*2)






First I thought, this afro-american be trippin'. Then...





>>> 'WTF'*2


'WTFWTF'





Brillant!

Re: If ++ Increments ... (++)

2005-12-29 20:49 • by Cheem
54990 in reply to 54987
You clowns are all doing it wrong. Here's what you do:
public int incrementBy11(int thisIsTheNumberThatYouWantToIncrementBy11)

{
for(int i = 0; i < 1; i++)
{

//Loop is unrolled for tEh 1337 f457
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
thisIsTheNumberThatYouWantToIncrementBy11 =
thisIsTheNumberThatYouWantToIncrementBy11 + 1;
}
}

Re: If ++ Increments ... (++)

2005-12-29 21:20 • by Xarium
54992 in reply to 54948
Anonymous:
for (i = 0; i < 11; i++) {

    i++;

}


Nothing cracks me up more than seeing critics try to write "funny" code - only to have it backfire.



Re: If ++ Increments ... (++)

2005-12-29 22:58 • by Mihai
template
T inc11( T x ) {
    return ++++++++++++++++++++++x;
}

Re: If ++ Increments ... (++)

2005-12-29 23:05 • by Mike

I get this sorta stuff from my coworker all the time!


coworker: i have a question...
me: alright
coworker: 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
coworker: my code is: this_subtotalprice = subtotal + subtotal
coworker: any ideas why it's giving me an extra 19?


*sound of head banging on desk reverberate across world*

Re: If ++ Increments ... (++)

2005-12-30 00:13 • by olddog

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?

Re: If ++ Increments ... (++)

2005-12-30 00:38 • by bigkm
55001 in reply to 55000
olddog:
Nathan: So... you know how the -- will decrement right?

lol

Re: If ++ Increments ... (++)

2005-12-30 01:08 • by Omnifarious

I get to use templates! Nobody told me I could use templates!


template <size_t incval, typename T>

class _inc_class {
public:
static T &incby(T &v) {
return ++_inc_class<incval - 1, T>::incby(v);
}
};

template <typename T>
class _inc_class<0, T> {
public:
static T &incby(T &v) {
return v;
}
};

template <size_t incval, typename T>
T &incby(T &v)
{
return _inc_class<incval, T>::incby(v);
}

There, now you can increment by lots of different values! Just do incby<11>(i);


Re: If ++ Increments ... (++)

2005-12-30 01:10 • by olddog
55003 in reply to 55000

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.

Re: If ++ Increments ... (++)

2005-12-30 03:38 • by Vector
55007 in reply to 55003
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: <_<

Re: If ++ Increments ... (++)

2005-12-30 03:45 • by BiggBru

Now, see, Nathan never said he wanted to increment by 11 QUICKLY. So, here is one alternative:


double number_you_want_to_increment = 0;


double numero = 11;


double temporary_variable_to_hold_stuff = number_you_want_to_increment;


while (numer_you_want_to_increment < temporary_variable_to_hold_stuff + 11)


{


   numero = numero / 2 ;


   number_you_want_to_increment += numero;


}


I think this gets puuuuurty close to incrementing a number by 11. [6]


 

Re: If ++ Increments ... (++)

2005-12-30 04:28 • by dhromed
55009 in reply to 54960
eimaj2nz:
Anonymous:

Anonymous:
const int FORTYTWO = 11;
i += FORTYTWO;

I love it!


That's right... can't have any of those pesky "magic numbers" floating around in the code.


But Insidious and Elusive numbers are no problem at all!

How aboot:

x += 0xB

x += 013

x += 1110.toDecimal()

Re: If ++ Increments ... (++)

2005-12-30 04:36 • by dhromed
55010 in reply to 55009
I'd like to see some code that almost adds 11.

Preferrably by introducing floating point errors.

Re: If ++ Increments ... (++)

2005-12-30 05:10 • by KewlKat
55014 in reply to 55008
BiggBru:

Now, see, Nathan never said he wanted to increment by 11 QUICKLY. So, here is one alternative:


double number_you_want_to_increment = 0;


double numero = 11;


double temporary_variable_to_hold_stuff = number_you_want_to_increment;


while (numer_you_want_to_increment < temporary_variable_to_hold_stuff + 11)


{


   numero = numero / 2 ;


   number_you_want_to_increment += numero;


}


I think this gets puuuuurty close to incrementing a number by 11. [6]


 



Heh, never quite gets there, does it?

« PrevPage 1 | Page 2 | Page 3Next »

Add Comment