James Newton's story needs no introduction ... so ... enjoy! [expand full text]
 « Prev Page 1 | Page 2 Next »

2005-12-07 14:58 • by Sean
 I'd like to see his power raising and factorial methods.

2005-12-07 15:05 • by dpm
 Don't blame the original programmer.  Everyone knows that subtracting is much faster than dividing. ok dpm

2005-12-07 15:07 • by Manni
 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....

2005-12-07 15:14 • by gonzo
 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....LOLAs for the OP, the real WTF is obviously the usage of the Solaris C++ compiler.

2005-12-07 15:32 • by Oliver Klozoff
 Good god...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:int result = value / divisor;nit remainder = value - (result * divisor);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:printf("Dividing %d by %d:\n", value, divisor); div_t r = div(value, divisor);printf("Result = %d\n", r.quot);printf("Remainder = %d\n", r.rem);And if anybody's wondering, I was self-taught in Pascal *and* C, which is *why* it took so long before I learned about '%'.

2005-12-07 15:51 • by kipthegreat
 Oliver Klozoff:Good god...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:int result = value / divisor;nit remainder = value - (result * divisor);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:printf("Dividing %d by %d:\n", value, divisor); div_t r = div(value, divisor);printf("Result = %d\n", r.quot);printf("Remainder = %d\n", r.rem);And if anybody's wondering, I was self-taught in Pascal *and* C, which is *why* it took so long before I learned about '%'.I remember doing something like this on my first programming language.. TI-83's version of BASIC.  :)I don't remember the syntax so I'll use C++ style:double tmp = value / divisor;  //where this is floating point division on a TI-83int result = ipart(tmp);   // ipart() is "integer part", the part before the decimal (similar to floor())int remainder = round( divisor * fpart(tmp) );   //where fpart() is fraction part.. the number starting at the decimal point.

2005-12-07 16:03 • by Oliver Klozoff
 Let's say, for the sake of argument, that you don't even know about the *divide* operator (/);here's an implementation that works for non-negative (>=0) dividends and positive (>0) divisors (it would be simple to modify it in order to support the full range of signed integers).    // get the quotient (getmodulus=false) or remainder (getmodulus=true)    static int divide(int dividend, int divisor, bool getmodulus) {        // this code is C#, but should work as C if you adjust the exception stuff to something appropriate        if (dividend < 0 || divisor < 0) { throw new Exception("too complicated"); }        if (divisor == 0) { throw new Exception("Division by zero"); }        if (dividend < divisor) return getmodulus ? dividend : 0;        int quotient = 0;    // if dividend >= divisor, there is at least one.        while (dividend >= divisor) {            int quot2 = 1;            int divisor2 = divisor;            while (divisor2 <= (dividend >> 1) ) {                quot2<<=1;                divisor2<<=1;            }            dividend -= divisor2;            quotient |= quot2;        }        if (getmodulus) return dividend;        return quotient;    }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).Golf at will, guys!

2005-12-07 16:08 • by CornedBee
 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. That it's at a defense contractor scares me a little, though.

2005-12-07 16:40 • by Joost_
 Pfft. I used to work at Lockheed where we would use lookup tables to avoid calculation of modulo. Vector generateModuloLookup(int table, int max) {   Vector lookup = new Vector();   for(int i = 0; i < max; i++)   {     Integer value = new Integer(i * table);     lookup.Add(value);   }   return lookup; } int lookupModule(Vector lookup, Integer number) {   return lookup.indexOf(number); int lookupModulo(Vector lookup, int number) {   return lookupModulo(new Integer(number)); }

2005-12-07 18:17 • by mazurek
 Joost_:Pfft. I used to work at Lockheed where we would use lookup tables to avoid calculation of modulo. Now that's the real wtf!!! My eyes - the googles, they're not helping!!!

 I'll have to admit to having made this mistake.Of course, I was taking my first programming course in high school at the time...

2005-12-07 18:44 • by Darax The Good
 something similar-but worse.  Anything to avoid spinning up the FPU.  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. void divide(int x, int y) // x/y {    int value=0, remainder;    While (x > y)     {       value++;       x-=y;    } remainder = x; }

2005-12-07 20:00 • by emurphy
 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).

2005-12-07 20:35 • by Just
 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? 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? ;)

2005-12-07 22:04 • by Kevin S
 Hey everyone! Long time reader, but only now really have something that is somewhat relevant =P  I'm currently in a Senior Design course, and we are having to create a BREW game for a cellphone.  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.  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.  This is what he came up with, and logged 5 hours to create it.  Its called at the end of every level.  We of course just rewrote it and decided we were better off if he stuck to busy work. //extra man every 50 points #define BONUS_MAN 50 score = pMe->nScore; while (score > BONUS_MAN) {     score -= BONUS_MAN; } if((score + 30) > BONUS_MAN) {     pMe->nMans += 1; } score = 0; pMe->nScore += 30;

2005-12-07 23:35 • by nickf
 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? all that aside, the REAL wtf there is that he didn't use "pMe->nMans++;"(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...)

2005-12-08 00:34 • by mjonhanson
 That's nothing.  When I was a young 'snapper programmer, we didn't have operators to do addition, all we had was a stick and some sand.

2005-12-08 01:20 • by Drak
 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... Try 9 for value and 5 for divisor... I guess you get an endless loop? So this function should be called makeSureNumberIsWhollyDivisibleOrElseGoIntoComa() ? Drak

2005-12-08 02:43 • by anonymous_
 Drak: 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... Try 9 for value and 5 for divisor... I guess you get an endless loop? So this function should be called makeSureNumberIsWhollyDivisibleOrElseGoIntoComa() ? Drak value is 9, divisor is 5 remainder is set to 9 remainder is not 0, enter while remainder is > divisor, take if remainder is 9 - 5 (which is 4) test while again remainder is not 0 remander is 4, divisor is 5 take else break done wait.. what?  Do not allow replies to this post check box?  and the captcha image isn't showing up...

2005-12-08 04:43 • by raph
 Oliver Klozoff:Let's say, for the sake of argument, that you don't even know about the *divide* operator (/);here's an implementation that works for non-negative (>=0) dividends and positive (>0) divisors (it would be simple to modify it in order to support the full range of signed integers).    // get the quotient (getmodulus=false) or remainder (getmodulus=true)    static int divide(int dividend, int divisor, bool getmodulus) {        // this code is C#, but should work as C if you adjust the exception stuff to something appropriate        if (dividend < 0 || divisor < 0) { throw new Exception("too complicated"); }        if (divisor == 0) { throw new Exception("Division by zero"); }        if (dividend < divisor) return getmodulus ? dividend : 0;        int quotient = 0;    // if dividend >= divisor, there is at least one.        while (dividend >= divisor) {            int quot2 = 1;            int divisor2 = divisor;            while (divisor2 <= (dividend >> 1) ) {                quot2<<=1;                divisor2<<=1;            }            dividend -= divisor2;            quotient |= quot2;        }        if (getmodulus) return dividend;        return quotient;    }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).Golf at will, guys! I usedc to do soemthing similar on a Marconi Myriad computer, except that I used right shifts instead of left. 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. 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.

2005-12-08 06:24 • by 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.

2005-12-08 08:05 • by haveworld
 Drak: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... Try 9 for value and 5 for divisor... I guess you get an endless loop? So this function should be called makeSureNumberIsWhollyDivisibleOrElseGoIntoComa() ? Drak Copy the code and test with those values and come back and tell us if you went into a Coma...

2005-12-08 08:19 • by stevekj
 OK, so I can't think of how this code could result in an infinite loop.  If the divisor were zero, the code would generate a divide-by-zero exception on most systems.  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).

2005-12-08 08:36 • by trollable
 stevekj:OK, so I can't think of how this code could result in an infinite loop.  /.../ (depending on size of integer, magnitude of divisor, and speed of CPU). 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 :(

2005-12-08 08:58 • by Charlie Marlow
 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.

2005-12-08 09:48 • by Phill
 mjonhanson:That's nothing.  When I was a young 'snapper programmer, we didn't have operators to do addition, all we had was a stick and some sand. 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.

2005-12-08 10:06 • by XoK
 What about ... int remainder = value - (floor(result) * divisor); No " % " thing :)   Alex Papadimoulis: int result=value/divisor;int remainder=value%divisor;

2005-12-08 11:51 • by Disgruntled DBA
 Phill: mjonhanson:That's nothing.  When I was a young 'snapper programmer, we didn't have operators to do addition, all we had was a stick and some sand. 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. Luxury. 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.  The surviving members would have to run in unison into the reader machine, in order to load the program.

### The forum is broken.

2005-12-08 12:19 • by luke727
 As that other guy said, the forum is broken.  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.  A real WTF in itself as generally a forum is a place to exchange information and ideas.  I have been here a while (anonymously) and agree with apparently many other people that this forum software...what is the word I'm looking for...oh yeah... SUCKS!!! Anyway, back on topic.  This is not really a WTF.  The guy knew what he was doing and it is intentional; the other people just don't understand how brillant it is.  A good practice when developing software is to add a few delay loops here and there, i.e. something like`for(int i = 0; i < 1000000; i++){   int wtf = i * (i + 1) / 2;}` Then every so often you can just take one of the loops out and claim you optimized it.  Everyone will be in awe of your awesomeness (naturally). 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).  This is quite possibly the biggest WTF of them all.  I cannot think of any forum that I have used anywhere anytime that did not have some kind of Preview option available.  I had to edit the HTML by hand for the code, and if the formatting of this post does not come out right I will be forced to kick a puppy. I pray there is nothing below this line...

### Well, that didn't work well.

2005-12-08 12:25 • by luke727
 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). The broken image on the bottom is more disturbing, though.  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.  It results in a resizable bitmap in the text area.  My only question is WTF?

2005-12-08 13:10 • by stevekj
 trollable: stevekj:OK, so I can't think of how this code could result in an infinite loop.  /.../ (depending on size of integer, magnitude of divisor, and speed of CPU). 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 :( OK, with a bit of quick math it looks like it could easily run for an hour or so.  Not quite an infinite loop, but could easily be mistaken for one by a tester!

2005-12-08 13:22 • by mjonhanson
 Disgruntled DBA: Phill: mjonhanson:That's nothing.  When I was a young 'snapper programmer, we didn't have operators to do addition, all we had was a stick and some sand. 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. Luxury. 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.  The surviving members would have to run in unison into the reader machine, in order to load the program. Machine readers were something only in the data centers of the rich.  Their Pinkertons used to come into our data center and tear up all the paper tape we made with our teeth.

2005-12-08 13:34 • by stevekj
 Charlie Marlow:    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. Are you sure?  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.

2005-12-08 13:39 • by voodooc

2005-12-08 13:43 • by Charlie Marlow
 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.

2005-12-08 14:11 • by Richard Nixon

2005-12-08 14:34 • by voodooc
 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.Sincerely,Richard Nixon It's an Idiot Orgy!  Thank you for contributing your genetic material. Hm, how many limbs do you have?  And do you like to wander around the African veldt?  And how big is your forehead?  I only ask because you remind me of someone I used to work for. vc

### Re: Well, that didn't work well.

2005-12-08 14:49 • by hank miller
 luke727:[typical rant about this forum]   My only question is WTF? Welcome to the club.   The rest of us have been seen the same problems. 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. The only good part about this is people will believe me when I blame my grammar and spelling errors on the forum.   Anywhere else they blame me.

2005-12-08 15:22 • by Richard Nixon
 voodooc: 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.Sincerely,Richard Nixon It's an Idiot Orgy!  Thank you for contributing your genetic material. Hm, how many limbs do you have?  And do you like to wander around the African veldt?  And how big is your forehead?  I only ask because you remind me of someone I used to work for. vcYou're trying too hard.Sincerely,Richard Nixon

2005-12-08 15:32 • by voodooc
 Richard Nixon: voodooc: It's an Idiot Orgy!  Thank you for contributing your genetic material. Hm, how many limbs do you have?  And do you like to wander around the African veldt?  And how big is your forehead?  I only ask because you remind me of someone I used to work for. vcYou're trying too hard.Sincerely,Richard Nixon "I apologize for the length of this insult, but I didn't have time to make it shorter." vc

### Re: Well, that didn't work well.

2005-12-08 15:37 • by Gene Wirchenko
 hank miller:Welcome to the club.   The rest of us have been seen the same problems. 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.It worked fine for me.  I did not ever try HTML though, and switching between that and Design apparently did cause trouble.Sincerely,Gene Wirchenko

2005-12-08 19:58 • by Maurits
 Joost_:Pfft. I used to work at Lockheed where we would use lookup tables to avoid calculation of modulo. Vector generateModuloLookup(int table, int max) {   Vector lookup = new Vector();   for(int i = 0; i < max; i++)   {     Integer value = new Integer(i * table);     lookup.Add(value);   }   return lookup; } int lookupModule(Vector lookup, Integer number) {   return lookup.indexOf(number); int lookupModulo(Vector lookup, int number) {   return lookupModulo(new Integer(number)); } Lookup tables I can understand.  But surely generating them at compile time is better than generating them at run time!

 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.

2005-12-08 21:03 • by Quinnum
 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. The 68K had the DIV instruction which resulted in the quotient and the remainder in one (slightly annoying) package. e.g. DIV D0,D1 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. 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...

2005-12-11 21:59 • by RC
 NAME        div, ldiv, lldiv, imaxdiv - compute quotient and remainder of an integer division SYNOPSIS        #include        div_t div(int numerator, int denominator);        ldiv_t ldiv(long numerator, long denominator);        lldiv_t lldiv(long long numerator, long long denominator);        #include        imaxdiv_t imaxdiv(intmax_t numerator, intmax_t denominator); DESCRIPTION        The  div()  function computes the value numerator/denominator and returns the quotient and        remainder in a structure named div_t that contains two  integer  members  (in  unspecified        order)  named  quot  and rem.  The quotient is rounded towards zero.  The result satisfies        quot*denominator+rem = numerator.        The ldiv() and lldiv() and imaxdiv() functions do the same, dividing numbers of the  indi-        cated  type  and  returning  the result in a structure of the indicated name, in all cases        with fields quot and rem of the same type as the function arguments. RETURN VALUE        The div_t (etc.) structure. EXAMPLE        After                div_t q = div(-5, 3);        the values q.quot and q.rem are -1 and -2, respectively. CONFORMING TO        SVID 3, BSD 4.3, ISO 9899.  The functions lldiv() and imaxdiv() were added in ISO C99. SEE ALSO        abs(3), remainder(3)

2005-12-12 11:18 • by anrieff
 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.

2005-12-12 21:09 • by Just
 Just: 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? Anyone know the answer to this? It's really bugging me now.

2005-12-14 06:02 • by Andreas
 Our company once had the (dubious) pleasure to further develop a product from a company we bought. I wasn't actively involved with that (fortunately) but the story goes that in the code a similar function (as FindTheRemainder()) was found, only after it the following "computation" was performed:```int real_reminder = 0; while(remainder > 0) {    real_reminder++;    remainder--; }```