Library.Math.Functions.3rdGrade.FindTheRemainder()

« Return to Article
  • Sean 2005-12-07 14:58
    I'd like to see his power raising and factorial methods.<br>
  • dpm 2005-12-07 15:05
    Don't blame the original programmer.&nbsp; Everyone knows that subtracting is much faster than dividing.<br>

    <br>

    ok<br>

    dpm
  • Manni 2005-12-07 15:07
    Many thanks to Alex for not adding on to the intro for that WTF. Honestly, the code alone was enough for me to shoot milk out of my nose, which was weird because I was drinking Cherry Coke at the time....
  • gonzo 2005-12-07 15:14
    Manni:
    Honestly, the code alone was enough for me to shoot milk out of my nose, which was weird because I was drinking Cherry Coke at the time....
    <br><br>LOL<br><br>As for the OP, the real WTF is obviously the usage of the Solaris C++ compiler.<br>
  • Oliver Klozoff 2005-12-07 15:32
    Good god...<br><br>Even before I knew I could use % to do remainder calculations (I just wanted the remainder. "Modulus"? Never heard of that before... what's it do?), I wrote the equivalent to this code:<br><br>int result = value / divisor;<br>nit remainder = value - (result * divisor);<br><br>And when I first explored C after using Pascal for years (where integer division is 'div' and modulus is 'mod'), I easily understood '/' for division, but even before I learned that '%' was modulus, I found this standard function:<br><br>printf("Dividing %d by %d:\n", value, divisor);<br>
    div_t r = div(value, divisor);<br>printf("Result = %d\n", r.quot);<br>printf("Remainder = %d\n", r.rem);<br><br><br>And if anybody's wondering, I was self-taught in Pascal *and* C, which is *why* it took so long before I learned about '%'.<br><br>
  • kipthegreat 2005-12-07 15:51
    Oliver Klozoff:
    Good god...<br><br>Even before I knew I could use % to do remainder calculations (I just wanted the remainder. "Modulus"? Never heard of that before... what's it do?), I wrote the equivalent to this code:<br><br>int result = value / divisor;<br>nit remainder = value - (result * divisor);<br><br>And when I first explored C after using Pascal for years (where integer division is 'div' and modulus is 'mod'), I easily understood '/' for division, but even before I learned that '%' was modulus, I found this standard function:<br><br>printf("Dividing %d by %d:\n", value, divisor);<br>
    div_t r = div(value, divisor);<br>printf("Result = %d\n", r.quot);<br>printf("Remainder = %d\n", r.rem);<br><br><br>And if anybody's wondering, I was self-taught in Pascal *and* C, which is *why* it took so long before I learned about '%'.<br><br>
    <br><br>I remember doing something like this on my first programming language.. TI-83's version of BASIC.&nbsp; :)<br><br>I don't remember the syntax so I'll use C++ style:<br><br><div style="margin-left: 40px;"><font size="2"><span style="font-family: Courier New;">double tmp = value / divisor;&nbsp; //where this is floating point division on a TI-83</span></font><br style="font-family: Courier New;"><font size="2"><span style="font-family: Courier New;">int result = ipart(tmp);&nbsp;&nbsp; // ipart() is "integer part", the part before the decimal (similar to floor())</span></font><br style="font-family: Courier New;"><font size="2"><span style="font-family: Courier New;">int remainder = round( divisor * fpart(tmp) );&nbsp;&nbsp; //where fpart() is fraction part.. the number starting at the decimal point.</span></font><br></div><br><br>
  • Oliver Klozoff 2005-12-07 16:03
    Let's say, for the sake of argument, that you don't even know about the *divide* operator (/);<br>here's an implementation that works for non-negative (&gt;=0) dividends and positive (&gt;0) divisors (it would be simple to modify it in order to support the full range of signed integers).<br><br>&nbsp;&nbsp;&nbsp; // get the quotient (getmodulus=false) or remainder (getmodulus=true)<br>&nbsp;&nbsp;&nbsp; static int divide(int dividend, int divisor, bool getmodulus) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // this code is C#, but should work as C if you adjust the exception stuff to something appropriate<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (dividend &lt; 0 || divisor &lt; 0) { throw new Exception("too complicated"); }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (divisor == 0) { throw new Exception("Division by zero"); }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (dividend &lt; divisor) return getmodulus ? dividend : 0;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int quotient = 0;&nbsp;&nbsp;&nbsp; // if dividend &gt;= divisor, there is at least one.<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (dividend &gt;= divisor) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int quot2 = 1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int divisor2 = divisor;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (divisor2 &lt;= (dividend &gt;&gt; 1) ) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; quot2&lt;&lt;=1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; divisor2&lt;&lt;=1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dividend -= divisor2;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; quotient |= quot2;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (getmodulus) return dividend;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return quotient;<br>&nbsp;&nbsp;&nbsp; }<br><br><br>In fact, a function much like this one is often embedded in programs compiled for a processor that has no divide instruction (which is actually the case for a majority of RISC-type processors).<br><br>Golf at will, guys!<br>
  • CornedBee 2005-12-07 16:08
    You know what the problem with this story is? It's too obvious. When I
    saw the post title, I already knew what the code would be.<br>
    <br>
    That it's at a defense contractor scares me a little, though.<br>
  • Joost_ 2005-12-07 16:40
    <span style="font-family: courier new;">Pfft. I used to work at Lockheed where we would use lookup tables to avoid calculation of modulo. </span><br style="font-family: courier new;">
    <br style="font-family: courier new;">
    <span style="font-family: courier new;">Vector generateModuloLookup(int table, int max) {</span><br style="font-family: courier new;">
    <span style="font-family: courier new;">&nbsp; Vector lookup = new Vector();<br>
    </span><span style="font-family: courier new;">&nbsp; for(int i = 0; i &lt; max; i++)<br>
    &nbsp; {<br>
    &nbsp;&nbsp;&nbsp; Integer value = new Integer(i * table);<br style="font-family: courier new;">
    </span><span style="font-family: courier new;"></span><span style="font-family: courier new;">&nbsp;&nbsp;&nbsp; lookup.Add(value);<br>
    &nbsp; }<br>
    &nbsp; return lookup;<br style="font-family: courier new;">
    </span><span style="font-family: courier new;"></span><span style="font-family: courier new;">}<br>
    <br>
    int lookupModule(Vector lookup, Integer number)<br>
    {<br>
    &nbsp; </span><span style="font-family: courier new;">return lookup.indexOf(number);</span><br>
    <span style="font-family: courier new;"><br>
    int lookupModulo(Vector lookup, int number)<br>
    {<br>
    &nbsp; return lookupModulo(new Integer(number));<br>
    }<br>
    </span><br style="font-family: courier new;">
  • mazurek 2005-12-07 18:17
    Joost_:
    <span style="font-family: courier new;">Pfft. I used to work at Lockheed where we would use lookup tables to avoid calculation of modulo. </span><br style="font-family: courier new;">
    <br>
    <br>
    Now that's the real wtf!!! My eyes - the googles, they're not helping!!!<br>
  • adv 2005-12-07 18:37
    I'll have to admit to having made this mistake.<br><br>Of course, I was taking my first programming course in high school at the time...<br>
  • Darax The Good 2005-12-07 18:44
    <P>something similar-but worse.&nbsp; Anything to avoid spinning up the FPU.&nbsp; This was shoved into something quite a bit more complex-and of course the variable names were j and z instead of value and remainder-so it took a bit to figure out exactly what was going on.</P>
    <P>void divide(int x, int y) // x/y</P>
    <P>{</P>
    <P>&nbsp;&nbsp;&nbsp;int value=0, remainder;</P>
    <P>&nbsp;&nbsp; While (x &gt; y)</P>
    <P>&nbsp;&nbsp;&nbsp;&nbsp;{</P>
    <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value++;</P>
    <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x-=y;</P>
    <P>&nbsp;&nbsp;&nbsp;}</P>
    <P>remainder = x;</P>
    <P>}</P>
  • emurphy 2005-12-07 20:00
    At least it was only operating on integers, otherwise it could also get into an infinite loop when value/divisor is so large that (value-divisor) gets rounded to (value).<br><br>
  • Just 2005-12-07 20:35
    <P>I could have sworn that C++ only allows for one implicit conversion, not 3 as described in the WTF. Is my memory failing, or is this a Solaris compiler extension (highly unlikely), or is the WTF submitter mistaken?</P>
    <P>BTW, I was forced to register to post this, as the spam tester thing that non-registered posters have to go to (where you need to enter the characters displayed in an image) wasn't working. I just got a red x. Is this a deliberate ploy to get more registered users? ;)</P>
  • Kevin S 2005-12-07 22:04
    Hey everyone! Long time reader, but only now really have something that
    is somewhat relevant =P&nbsp; I'm currently in a Senior Design course,
    and we are having to create a BREW game for a cellphone.&nbsp; The
    class is divided up into small groups, and each group has their own
    seperate game they are creating, and our 4 person group was to create a
    cell phone represenation of Trogdor. But anyway, one of our group
    members has basically been useless the entire term, but recently we
    gave him one simple task to complete in a week.&nbsp; He was to make it
    where the player was rewarded with a free man ("Mans") every 50 points,
    and to also give 30 points at the end of every level.&nbsp; This is
    what he came up with, and logged 5 hours to create it.&nbsp; Its called
    at the end of every level.&nbsp; We of course just rewrote it and
    decided we were better off if he stuck to busy work.<br>
    <br>
    //extra man every 50 points <br>
    #define BONUS_MAN 50<br>
    score = pMe-&gt;nScore; <br>
    <br>
    while (score &gt; BONUS_MAN) <br>
    { <br>
    &nbsp;&nbsp;&nbsp; score -= BONUS_MAN; <br>
    }<br>
    if((score + 30) &gt; BONUS_MAN) <br>
    { <br>
    &nbsp;&nbsp;&nbsp; pMe-&gt;nMans += 1; <br>
    } <br>
    score = 0; <br>
    pMe-&gt;nScore += 30;<br>
  • nickf 2005-12-07 23:35
    called only at the end of each level? but i want my extra mans now! i
    also like how he sets score = 0 for no apparent reason. Also, what
    happens if you're so good that you score over 100 points in a level?<br>
    all that aside, the REAL wtf there is that he didn't use "pMe-&gt;nMans++;"<br><br><br>(on a side note - i had to register as well - the anti-spam protection seems to have upgraded itself to anti-post protection. and since i'm on the topic of bagging the forum software, i can't click in the textarea to type a reply, i have to click somewhere else and tab my way through all the emoticons to get there. can't click on any emoticons either...)<br>
  • mjonhanson 2005-12-08 00:34
    That's nothing.&nbsp; When I was a young 'snapper programmer, we didn't
    have operators to do addition, all we had was a stick and some sand.<br>
  • Drak 2005-12-08 01:20
    <P>Is the problem with the code so obvious to everyone? I've not seen any of the usual comments aout how it is broken, so I'll start...</P>
    <P>Try 9&nbsp;for value and 5 for divisor... I guess you get an endless loop?</P>
    <P>So this function should be called makeSureNumberIsWhollyDivisibleOrElseGoIntoComa() ?</P>
    <P>Drak</P>
  • anonymous_ 2005-12-08 02:43
    Drak:

    <p>Is the problem with the code so obvious to
    everyone? I've not seen any of the usual comments aout how it is
    broken, so I'll start...</p>

    <p>Try 9&nbsp;for value and 5 for divisor... I guess you get an endless loop?</p>

    <p>So this function should be called makeSureNumberIsWhollyDivisibleOrElseGoIntoComa() ?</p>

    <p>Drak</p>
    <br>

    value is 9, divisor is 5<br>

    remainder is set to 9<br>

    <br>

    remainder is not 0, enter while<br>

    remainder is &gt; divisor, take if<br>

    remainder is 9 - 5 (which is 4)<br>

    test while again<br>

    remainder is not 0<br>

    remander is 4, divisor is 5<br>

    take else<br>

    break<br>

    <br>

    done<br>

    <br>

    <br>

    <br>

    wait.. what?&nbsp; Do not allow replies to this post check box?&nbsp; and the captcha image isn't showing up...
  • raph 2005-12-08 04:43
    <P>
    Oliver Klozoff:
    Let's say, for the sake of argument, that you don't even know about the *divide* operator (/);<BR>here's an implementation that works for non-negative (&gt;=0) dividends and positive (&gt;0) divisors (it would be simple to modify it in order to support the full range of signed integers).<BR><BR>&nbsp;&nbsp;&nbsp; // get the quotient (getmodulus=false) or remainder (getmodulus=true)<BR>&nbsp;&nbsp;&nbsp; static int divide(int dividend, int divisor, bool getmodulus) {<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // this code is C#, but should work as C if you adjust the exception stuff to something appropriate<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (dividend &lt; 0 || divisor &lt; 0) { throw new Exception("too complicated"); }<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (divisor == 0) { throw new Exception("Division by zero"); }<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (dividend &lt; divisor) return getmodulus ? dividend : 0;<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int quotient = 0;&nbsp;&nbsp;&nbsp; // if dividend &gt;= divisor, there is at least one.<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (dividend &gt;= divisor) {<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int quot2 = 1;<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int divisor2 = divisor;<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (divisor2 &lt;= (dividend &gt;&gt; 1) ) {<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; quot2&lt;&lt;=1;<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; divisor2&lt;&lt;=1;<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dividend -= divisor2;<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; quotient |= quot2;<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (getmodulus) return dividend;<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return quotient;<BR>&nbsp;&nbsp;&nbsp; }<BR><BR><BR>In fact, a function much like this one is often embedded in programs compiled for a processor that has no divide instruction (which is actually the case for a majority of RISC-type processors).<BR><BR>Golf at will, guys!<BR>
    </P>
    <P>I usedc to do soemthing similar on a Marconi Myriad computer, except that I used right shifts instead of left.&nbsp;The operation times were add/sub 2 microsecs, multiplye 24 microsecs, divide 48 microsecs. Multiplication and division by certain special numbers, e.g. 10 which only has two bits set, was faster by shift-and-add/subtract than by hardware multiplication/division.</P>
    <P>Incidentally, division was rounded fractional division, not integer truncated division, so even after a hardware division you still had to do a 1-place shift and check for a negative remainder.</P>
  • trollable 2005-12-08 06:24
    My Sun5 is unplugged and i don't remember if the 68000 supports neither
    the modulo instruction nor the div one. A possible guess is the pasted
    code may run faster on the old Solaris/68000 combination. The fact it
    is C++and/or Solaris doesn't really matter, the CPU does.<br>
    <br>
    <br>
    <br>
  • haveworld 2005-12-08 08:05
    Drak:
    <p>Is the problem with the code so obvious to
    everyone? I've not seen any of the usual comments aout how it is
    broken, so I'll start...</p>
    <p>Try 9&nbsp;for value and 5 for divisor... I guess you get an endless loop?</p>
    <p>So this function should be called makeSureNumberIsWhollyDivisibleOrElseGoIntoComa() ?</p>
    <p>Drak</p>
    <br>
    <br>
    Copy the code and test with those values and come back and tell us if you went into a Coma...
  • stevekj 2005-12-08 08:19
    OK, so I can't think of how this code could result in an infinite
    loop.&nbsp; If the divisor were zero, the code would generate a
    divide-by-zero exception on most systems.&nbsp; Even a negative
    (non-zero) divisor would have to result in a wraparound condition
    eventually, given a finite size of integer, which would terminate the
    loop after a while (depending on size of integer, magnitude of divisor,
    and speed of CPU).<br>
    <br>
  • trollable 2005-12-08 08:36
    stevekj:
    OK, so I can't think of how this code could result in an infinite
    loop.&nbsp; /.../ (depending on size of integer, magnitude of divisor,
    and speed of CPU).
    <br>
    Solaris was designed for the 68000, 8MHz. The loop is about 15 cycles
    (raw approximate). The value is an epoch, the divisor is not specified
    but is probably small. Now some math is required. The OP should have
    mention the hardware :(<br>
  • Charlie Marlow 2005-12-08 08:58
    &nbsp;&nbsp;&nbsp; It was not specified in the original post, but if the divisor ends up being a negative number during all of those conversions, it will result in an infinate loop.<br>
  • Phill 2005-12-08 09:48
    <P>
    mjonhanson:
    That's nothing.&nbsp; When I was a young 'snapper programmer, we didn't have operators to do addition, all we had was a stick and some sand.<BR>
    </P>
    <P>You were lucky. I could only have dreamed of having a stick and some sand. All I had for addition was a concrete wall to bang my head against while my father whipped me with his belt.</P>
  • XoK 2005-12-08 10:06
    <P>What about ...</P>
    <P>int remainder = value - (floor(result) * divisor);</P>
    <P>No " % " thing :)</P>
    <P>&nbsp;</P>
    <P>
    Alex Papadimoulis:
    </P>
    <P>int result=value/divisor;<BR>int remainder=value%divisor;
    <P>

    <P>&nbsp;</P>
  • Disgruntled DBA 2005-12-08 11:51
    Phill:
    <p>
    mjonhanson:
    That's nothing.&nbsp;
    When I was a young 'snapper programmer, we didn't have operators to do
    addition, all we had was a stick and some sand.<br>
    </p>
    <p>You were lucky. I could only have dreamed of having a stick and some
    sand. All I had for addition was a concrete wall to bang my head
    against while my father whipped me with his belt.</p>
    <br>
    <br>
    Luxury.<br>
    <br>
    When I was a junior programmer, the Senior programmer would line us up
    in rows of 8, and club us over the head to punch out holes in the
    formation.&nbsp; The surviving members would have to run in unison into
    the reader machine, in order to load the program.<br>
  • luke727 2005-12-08 12:19
    <P>As that other guy said, the forum is broken.&nbsp; When not logged in the captcha images are not showing so you can't post anonymously, and now there is a "Do not allow replies to this post" checkbox.&nbsp; A real WTF in itself as generally a forum is a place to exchange information and ideas.&nbsp; I have been here a w<FONT style="BACKGROUND-COLOR: #ffffff">hil</FONT>e (anonymously) and agree with apparently many other people that this forum software...what is the word I'm looking for...oh yeah...</P>
    <P><FONT style="BACKGROUND-COLOR: #000000" face=Arial color=#ff0000 size=72><STRONG><EM><U>SUCKS!!!</U></EM></STRONG></FONT></P>
    <P>Anyway, back on topic.&nbsp; This is not really a WTF.&nbsp; The guy knew what he was doing and it is intentional; the other people just don't understand how brillant it is.&nbsp; A good practice when developing software is to add a few delay loops here and there, i.e. something like</P><PRE>for(int i = 0; i &lt; 1000000; i++)<BR>{<BR>&nbsp;&nbsp;&nbsp;int wtf = i * (i + 1) / 2;<BR>}<BR></PRE>
    <P>Then every so often you can just take one of the loops out and claim you optimized it.&nbsp; Everyone will be in awe of your awesomeness (naturally).</P>
    <P>As I sit here ready to post it I notice that there really is no Preview button (I thought it was just a joke or something).&nbsp; This is quite possibly the biggest WTF of them all.&nbsp; I cannot think of any forum that I have used anywhere anytime that did not have some kind of Preview option available.&nbsp; I had to edit the HTML by hand for the code,&nbsp;and if the formatting of this post does not come out right I will be forced to kick a puppy.</P>
    <P>I pray there is nothing below this line...</P>
    <P><IMG style="WIDTH: 796px; HEIGHT: 327px" height=26 src="/freetextbox3/images/toolbar.horizontal.start.gif" width=796 align=center></P>
  • luke727 2005-12-08 12:25
    <P>That is supposed to be big red text on black background "SUCKS!!!", but it came out regular size black on black (further proving my point).</P>
    <P>The broken image on the bottom is more disturbing, though.&nbsp; I dragged one of the menu placeholders (whatever the fuck they are called, the three-dot thing on the left of the menu strips) into the text box.&nbsp; It results in a resizable bitmap in the text area.&nbsp; My only question is WTF?</P>
  • stevekj 2005-12-08 13:10
    trollable:
    stevekj:
    OK, so I can't think of how this code could result in an infinite
    loop.&nbsp; /.../ (depending on size of integer, magnitude of divisor,
    and speed of CPU).
    <br>
    Solaris was designed for the 68000, 8MHz. The loop is about 15 cycles
    (raw approximate). The value is an epoch, the divisor is not specified
    but is probably small. Now some math is required. The OP should have
    mention the hardware :(<br>
    <br>
    <br>
    OK, with a bit of quick math it looks like it could easily run for an
    hour or so.&nbsp; Not quite an infinite loop, but could easily be
    mistaken for one by a tester!<br>
    <br>
  • mjonhanson 2005-12-08 13:22
    Disgruntled DBA:
    Phill:
    <p>
    mjonhanson:
    That's nothing.&nbsp;
    When I was a young 'snapper programmer, we didn't have operators to do
    addition, all we had was a stick and some sand.<br>
    </p>
    <p>You were lucky. I could only have dreamed of having a stick and some
    sand. All I had for addition was a concrete wall to bang my head
    against while my father whipped me with his belt.</p>
    <br>
    <br>
    Luxury.<br>
    <br>
    When I was a junior programmer, the Senior programmer would line us up
    in rows of 8, and club us over the head to punch out holes in the
    formation.&nbsp; The surviving members would have to run in unison into
    the reader machine, in order to load the program.<br>
    <br>
    <br>
    Machine readers were something only in the data centers of the
    rich.&nbsp; Their Pinkertons used to come into our data center and tear
    up all the paper tape we made with our teeth.<br>
  • stevekj 2005-12-08 13:34
    Charlie Marlow:
    &nbsp;&nbsp;&nbsp; It was not specified in
    the original post, but if the divisor ends up being a negative number
    during all of those conversions, it will result in an infinate loop.<br>
    <br>
    <br>
    Are you sure?&nbsp; Bear in mind that in normal signed integer
    arithmetic, which is what this looks like, a series of additions will
    eventually wrap around to a negative number... and although the
    hardware will probably detect this overflow condition, the software
    usually doesn't.<br>
  • voodooc 2005-12-08 13:39
    mjonhanson:
    Disgruntled DBA:
    Phill:
    <p>
    mjonhanson:
    That's nothing.&nbsp;
    When I was a young 'snapper programmer, we didn't have operators to do
    addition, all we had was a stick and some sand.<br>
    </p>
    <p>You were lucky. I could only have dreamed of having a stick and some
    sand. All I had for addition was a concrete wall to bang my head
    against while my father whipped me with his belt.</p>
    <br>
    <br>
    Luxury.<br>
    <br>
    When I was a junior programmer, the Senior programmer would line us up
    in rows of 8, and club us over the head to punch out holes in the
    formation.&nbsp; The surviving members would have to run in unison into
    the reader machine, in order to load the program.<br>
    <br>
    <br>
    Machine readers were something only in the data centers of the
    rich.&nbsp; Their Pinkertons used to come into our data center and tear
    up all the paper tape we made with our teeth.<br>
    <br>
    <br>
    Right.<br>
    <br>
    When I was a young lad, we had to get to work at 3 AM, 2 hours before
    we went to bed, where we would have to sit in six status meetings with
    fifteen other coworkers and explain all the projects we were working on
    to a parapalegic baboon with down's syndrome.&nbsp; Our company was too
    poor to afford paper tape, so we had to carve little 1's and 0's into
    the bones of our own forearms with a staple remover.&nbsp; Applications
    were 'interpreted' by a washed up witch doctor who had escaped from the
    Dominican Republic after killing twelve people with a blunt
    instrument.&nbsp; We would have to cut off our forearms, boil the flesh
    off them, and give them to 'Doc', who would find meaning in our
    bones.&nbsp; Instead of coffee, we had to lick the varnish off our
    desks, and instead of a lunch break, we got to drink our own boiled-off
    flesh.<br>
    <br>
    And when we got home, our dad would cut us in two with a bread knife and dance about on our graves singing hallelujah.<br>
    <br>
    vc<br>
  • Charlie Marlow 2005-12-08 13:43
    That is true. I guess that, like the other case, it would take so long to run that it would appear to be stuck. Of course, the answer received from such a situation for the remainder would be a little off.<br>
  • Richard Nixon 2005-12-08 14:11
    voodooc:
    mjonhanson:
    Disgruntled DBA:
    Phill:
    <p>
    mjonhanson:
    That's nothing.&nbsp;
    When I was a young 'snapper programmer, we didn't have operators to do
    addition, all we had was a stick and some sand.<br>
    </p>
    <p>You were lucky. I could only have dreamed of having a stick and some
    sand. All I had for addition was a concrete wall to bang my head
    against while my father whipped me with his belt.</p>
    <br>
    <br>
    Luxury.<br>
    <br>
    When I was a junior programmer, the Senior programmer would line us up
    in rows of 8, and club us over the head to punch out holes in the
    formation.&nbsp; The surviving members would have to run in unison into
    the reader machine, in order to load the program.<br>
    <br>
    <br>
    Machine readers were something only in the data centers of the
    rich.&nbsp; Their Pinkertons used to come into our data center and tear
    up all the paper tape we made with our teeth.<br>
    <br>
    <br>
    Right.<br>
    <br>
    When I was a young lad, we had to get to work at 3 AM, 2 hours before
    we went to bed, where we would have to sit in six status meetings with
    fifteen other coworkers and explain all the projects we were working on
    to a parapalegic baboon with down's syndrome.&nbsp; Our company was too
    poor to afford paper tape, so we had to carve little 1's and 0's into
    the bones of our own forearms with a staple remover.&nbsp; Applications
    were 'interpreted' by a washed up witch doctor who had escaped from the
    Dominican Republic after killing twelve people with a blunt
    instrument.&nbsp; We would have to cut off our forearms, boil the flesh
    off them, and give them to 'Doc', who would find meaning in our
    bones.&nbsp; Instead of coffee, we had to lick the varnish off our
    desks, and instead of a lunch break, we got to drink our own boiled-off
    flesh.<br>
    <br>
    And when we got home, our dad would cut us in two with a bread knife and dance about on our graves singing hallelujah.<br>
    <br>
    vc<br>
    <br><br>Does anyone (besides the "people" who write this crap) still think these types of "jokes" are even the slightest bit amusing? It just gets worse and worse the more idiots who pile on.<br><br>Sincerely,<br><br>Richard Nixon<br>
  • voodooc 2005-12-08 14:34
    Richard Nixon:
    <br><br>Does anyone (besides the "people"
    who write this crap) still think these types of "jokes" are even the
    slightest bit amusing? It just gets worse and worse the more idiots who
    pile on.<br><br>Sincerely,<br><br>Richard Nixon<br>
    <br>
    <br>
    It's an Idiot Orgy!&nbsp; Thank you for contributing your genetic material.<br>
    <br>
    Hm, how many limbs do you have?&nbsp; And do you like to wander around
    the African veldt?&nbsp; And how big is your forehead?&nbsp; I only ask
    because you remind me of someone I used to work for.<br>
    <br>
    vc<br>
  • hank miller 2005-12-08 14:49
    luke727:
    [typical rant about this forum]<br>
    <p>&nbsp; My only question is WTF?</p>
    <br>
    <br>
    Welcome to the club.&nbsp;&nbsp; The rest of us have been seen the same problems.<br>
    <br>
    There is good reason there is no preview button - anything previewed
    NEVER showed up the same when posted, while if you didn't preview (just
    trusting to the devil) once in a while things look like you want them
    to.<br>
    <br>
    The only good part about this is people will believe me when I blame my
    grammar and spelling errors on the forum.&nbsp;&nbsp; Anywhere else
    they blame me.<br>
  • Richard Nixon 2005-12-08 15:22
    voodooc:
    Richard Nixon:
    <br><br>Does anyone (besides the "people"
    who write this crap) still think these types of "jokes" are even the
    slightest bit amusing? It just gets worse and worse the more idiots who
    pile on.<br><br>Sincerely,<br><br>Richard Nixon<br>
    <br>
    <br>
    It's an Idiot Orgy!&nbsp; Thank you for contributing your genetic material.<br>
    <br>
    Hm, how many limbs do you have?&nbsp; And do you like to wander around
    the African veldt?&nbsp; And how big is your forehead?&nbsp; I only ask
    because you remind me of someone I used to work for.<br>
    <br>
    vc<br>
    <br><br>You're trying too hard.<br><br>Sincerely,<br><br>Richard Nixon<br>
  • voodooc 2005-12-08 15:32
    Richard Nixon:
    voodooc:
    <br>
    It's an Idiot Orgy!&nbsp; Thank you for contributing your genetic material.<br>
    <br>
    Hm, how many limbs do you have?&nbsp; And do you like to wander around
    the African veldt?&nbsp; And how big is your forehead?&nbsp; I only ask
    because you remind me of someone I used to work for.<br>
    <br>
    vc<br>
    <br><br>You're trying too hard.<br><br>Sincerely,<br><br>Richard Nixon<br>
    <br>
    <br>
    "I apologize for the length of this insult, but I didn't have time to make it shorter."<br>
    <br>
    vc<br>
  • Gene Wirchenko 2005-12-08 15:37
    hank miller:
    Welcome to the club.&nbsp;&nbsp; The rest of us have been seen the same problems.<br>
    <br>
    There is good reason there is no preview button - anything previewed
    NEVER showed up the same when posted, while if you didn't preview (just
    trusting to the devil) once in a while things look like you want them
    to.
    <br><br>It worked fine for me.&nbsp; I did not ever try HTML though, and switching between that and Design apparently did cause trouble.<br><br>Sincerely,<br><br>Gene Wirchenko<br><br>
  • Maurits 2005-12-08 19:58
    Joost_:
    <span style="font-family: courier new;">Pfft. I used to work at Lockheed where we would use lookup tables to avoid calculation of modulo. </span><br style="font-family: courier new;">
    <br style="font-family: courier new;">
    <span style="font-family: courier new;">Vector generateModuloLookup(int table, int max) {</span><br style="font-family: courier new;">
    <span style="font-family: courier new;">&nbsp; Vector lookup = new Vector();<br>
    </span><span style="font-family: courier new;">&nbsp; for(int i = 0; i &lt; max; i++)<br>
    &nbsp; {<br>
    &nbsp;&nbsp;&nbsp; Integer value = new Integer(i * table);<br style="font-family: courier new;">
    </span><span style="font-family: courier new;"></span><span style="font-family: courier new;">&nbsp;&nbsp;&nbsp; lookup.Add(value);<br>
    &nbsp; }<br>
    &nbsp; return lookup;<br style="font-family: courier new;">
    </span><span style="font-family: courier new;"></span><span style="font-family: courier new;">}<br>
    <br>
    int lookupModule(Vector lookup, Integer number)<br>
    {<br>
    &nbsp; </span><span style="font-family: courier new;">return lookup.indexOf(number);</span><br>
    <span style="font-family: courier new;"><br>
    int lookupModulo(Vector lookup, int number)<br>
    {<br>
    &nbsp; return lookupModulo(new Integer(number));<br>
    }<br>
    </span><br style="font-family: courier new;">
    <br>
    <br>
    Lookup tables I can understand.&nbsp; But surely generating them at compile time is better than generating them at run time!<br>
  • waynerad 2005-12-08 21:03
    Everyone is commenting on the modulo. But what about the gazillion time classes that all invoke each other's constructors and do time conversions just to store a single date/time??? That's the real crazy part man.
  • Quinnum 2005-12-08 21:03
    trollable:
    My Sun5 is unplugged and i don't remember if the 68000 supports neither
    the modulo instruction nor the div one. A possible guess is the pasted
    code may run faster on the old Solaris/68000 combination. The fact it
    is C++and/or Solaris doesn't really matter, the CPU does.<br>
    <br>
    <br>
    <br>
    The 68K had the DIV instruction which resulted in the quotient and the remainder in one (slightly annoying) package.<br>
    <br>
    e.g. <span style="font-family: courier new; font-weight: bold;">DIV D0,D1</span> <br>
    <br>
    would divide the full 32 bit value of D1 by the lower 16 bits of D0,
    resulting in the quotient being in the lower 16 bits and the remainder
    in the upper 16 bits of D1. Which was a pain in the arse if the
    quotient was greater than a 16 bit result, like trying to divide 2^31
    by 10 for example.<br>
    <br>
    Still - 68K assembly was a joy to program back in the heady Amiga days,
    unlike the shitty x86 and it's crappy instruction set and segments.
    bleh...<br>
    <br>
  • RC 2005-12-11 21:59
    NAME<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; div, ldiv, lldiv, imaxdiv - compute quotient and remainder of an integer division<br>
    <br>
    SYNOPSIS<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #include &lt;stdlib.h&gt;<br>
    <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; div_t div(int numerator, int denominator);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ldiv_t ldiv(long numerator, long denominator);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lldiv_t lldiv(long long numerator, long long denominator);<br>
    <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #include &lt;inttypes.h&gt;<br>
    <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; imaxdiv_t imaxdiv(intmax_t numerator, intmax_t denominator);<br>
    <br>
    DESCRIPTION<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The&nbsp; div()&nbsp; function
    computes the value numerator/denominator and returns the quotient and<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; remainder in a structure named
    div_t that contains two&nbsp; integer&nbsp; members&nbsp; (in&nbsp;
    unspecified<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; order)&nbsp; named&nbsp;
    quot&nbsp; and rem.&nbsp; The quotient is rounded towards zero.&nbsp;
    The result satisfies<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; quot*denominator+rem = numerator.<br>
    <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The ldiv() and lldiv() and
    imaxdiv() functions do the same, dividing numbers of the&nbsp; indi-<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cated&nbsp; type&nbsp; and&nbsp;
    returning&nbsp; the result in a structure of the indicated name, in all
    cases<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; with fields quot and rem of the same type as the function arguments.<br>
    <br>
    RETURN VALUE<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The div_t (etc.) structure.<br>
    <br>
    EXAMPLE<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; After<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; div_t q = div(-5, 3);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; the values q.quot and q.rem are -1 and -2, respectively.<br>
    <br>
    CONFORMING TO<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SVID 3, BSD 4.3, ISO 9899.&nbsp; The functions lldiv() and imaxdiv() were added in ISO C99.<br>
    <br>
    SEE ALSO<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abs(3), remainder(3)
  • anrieff 2005-12-12 11:18
    The loop in the WTF is actually faster than the modulo operator, even
    on the IA32, if the value is less than eight times the divisor. So,
    before considering this as a true WTF we must know the divisor.<br>
  • Just 2005-12-12 21:09
    Just:

    <P>I could have sworn that C++ only allows for one implicit conversion, not 3 as described in the WTF. Is my memory failing, or is this a Solaris compiler extension (highly unlikely), or is the WTF submitter mistaken?</P>
    <P>
    </P>
    <P>Anyone know the answer to this? It's really bugging me now.</P>
  • Andreas 2005-12-14 06:02
    <P>Our company once had the (dubious) pleasure to&nbsp;further develop a product from a company we bought. I wasn't actively involved with that (fortunately) but the story goes that&nbsp;in the code a similar function (as FindTheRemainder())&nbsp;was found, only after&nbsp;it the following "computation" was performed:</P><FONT face="Courier New"><PRE>int real_reminder = 0;
    while(remainder &gt; 0)
    {
    &nbsp;&nbsp;&nbsp;real_reminder++;
    &nbsp;&nbsp;&nbsp;remainder--;
    }</FONT></PRE>
  • aniefer 2005-12-15 16:51
    Just:
    Just:

    <p>I could have sworn that C++ only allows for one implicit conversion,
    not 3 as described in the WTF. Is my memory failing, or is this a
    Solaris compiler extension (highly unlikely), or is the WTF submitter
    mistaken?</p>
    <p>
    </p>
    <p>Anyone know the answer to this? It's really bugging me now.</p>
    <br>
    <br>
    Without taking too long to consider whether or not I'm forgetting something, from the C++ spec:<br>
    section 12.3:  "At most one user-defined conversion (constructor
    of conversion function) is implicitly applied to a single value."<br>
    <br>
    section 13.3.3.1.2:  during function resolution, a user-defined
    conversion sequence consists of an initial standard conversion ,
    followed by a user-defined conversion, followed by a second standard
    conversion sequence.<br>
  • Foo 2005-12-17 10:01
    And you try ta tell that to the kids these days and they won't believe you!



    (for those who the joke completely misses, listen/watch the Four Yorkshiremen sketch by Monty Python)
    (as if people who haven't seen it deserve to be called programmers or something...)
  • Foo 2005-12-17 10:04
    Richard Nixon:
    Does anyone (besides the "people" who write this crap) still think these types of "jokes" are even the slightest bit amusing? It just gets worse and worse the more idiots who pile on.<br><br>Sincerely,<br><br>Richard Nixon<br>


    And yes, pretty much fell out of my chair at each one of them (at least, the ones mimicing the sketch)
  • ninjabob7 2008-09-09 19:44
    Wait... does TI-BASIC have modulo??
    TI-BASIC is an extremely WTFy language. It has no comments. And practically no string support. And then there's "increment and skip if greater than" which only belongs in an assembly language. It's possible to do some fairly interesting stuff though. I've written a chess game, an arcade-like game, and I'm working on a solitaire game.

    And TRWTF is that the OP refers to the "while" loop as a "for" loop. Not that there's a whole lot of difference in a C-style language, but still.