• steve (unregistered)

    wow. just wow.

  • (cs)

    After reading the description and code comment my brain hurt too much to read the code.

  • (cs)

    Um... This is TOO bad to be true....

  • amoore (unregistered)

    Is the real WTF the fact that this code is commented?

  • (cs)

    Is there any input at all that this thing does right? (Other than both inputs the same.)

  • Just Some Guy (unregistered)

    New terms for standard mathematical words (eg "dividend" for "numerator"): check.

    Absurdly wrong answers (eg 10 / 2 == 28 / 20): check.

    Allergic to exception handling that would've solved the entire problem - not patched it, but solved it: check.

    Nice.

  • (cs)

    // Version 2: Optimized

    Random r = new Random(); return r;

  • (cs)

    So, lets see...

    For 10/2 you get...

        if (10 > 2) { separateZero = 10 - 2 + 10; } // separateZero = 18
        else ...
        if ((10 + 18) == 0 || (2 + 18) == 0) { ... }
        value = (18 + 10) / (18 + 2); // value = 28 / 20
        if ( ...silly -ve test... )  { ... }
    
        return 1.4;
    
  • amoore (unregistered) in reply to Welbog
    Welbog:
    Is there any input at all that this thing does right? (Other than both inputs the same.)
    That's the only case that I can find.
  • Anonymous Coward (unregistered) in reply to steve

    OOUCHAAAAAAAAARGGH! The goggles, they do nothing!

  • Anon (unregistered)

    Alex you should warn before reading that code:

    "Please backup your mind as this WILL cause you permanent damage"...

  • Wameng Vang (unregistered)

    Junior... very very Junior..... ARRRGHH..................

    The junior developer is creating a framework for the something that the compiler/tool already handles by throwing exceptions, who knows he/she may create a try/catch framework to cover more error handling. Somebody needs to re-take Programming 101.

    What is being taught these days.....!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Junior... very very Junior..... ARRRGHH..................

  • (cs)

    Aaaaaa! WTF?!

    Seriously, I can't even figure out what the hell it's trying to do. What's wrong with:

    if (divider == 0) /* handle it however you want */;
    else return dividend/divider;
  • codemonkey (unregistered)

    I thought maybe, just maybe this was intended to work with very large numbers since it's a double...but alas, it doesn't:

    1e25 / 9e25 =~ 0.5294117647058824

    WTF!?

  • Anonymous (unregistered) in reply to LiquidFire
    LiquidFire:
    What's wrong with:
    if (divider == 0) /* handle it however you want */;
    else return dividend/divider;

    That won't work because a divider is that thing that seperates a room into two parts. A divisor however... that's a different story.

  • zv (unregistered)

    Strange results

    MessageBox.Show("100/10= " + DivisionWithZero(100, 10).ToString()); MessageBox.Show("1000000/10= " + DivisionWithZero(1000000, 10).ToString());

    Let us hope this program doesn't calculate our taxes :D

  • (cs)

    //remembers to keep the sign digit right

    Why would you need to put the sign on the right? Shouldn't it always be on the left? </sarcasm>

  • Keko (unregistered)

    0/0 = FILE_NOT_FOUND

  • Sgt. Preston (unregistered)

    It might avoid "having to get trouble with zero values," but it doesn't manage to avoid having to get trouble with overflow. Just pass it the values Double.MAX_VALUE and 1 and you'll achieve overflow as soon as you execute this line:

    if (dividend > divider) { separateZero = dividend - divider + 10; }

  • (cs)

    Wouldn't this whole thing be easier with:

    try { result = oneNumber / notherNumber; } catch (Exception.DivisionByZero) { //we just divided by zero!!!!!!!!!!!! }

  • HappilyEverAfter (unregistered)

    The person who wrote this ...code was my coworker. You can not imagine how happy I was when company decided to let him go. Alltough we got rid of the meanace he left behind hundreds and hundreds of lines bad code. But there is something good even that. I have never laughed as much when I read some of his code trough. You really should have seen how he managed to convert integer to decimal :D

  • (cs)

    Or maybe even something as exotic as (if you'll pardon my pseudo-code)

    if divider = 0 then don't even bother trying to divide because it will cause an error.

    CS101 anyone?

  • Sulka (unregistered) in reply to Welbog
    Welbog:
    Is there any input at all that this thing does right? (Other than both inputs the same.)

    Except of course when both inputs are zero. :)

  • Josh (unregistered)

    I'm not smart enough to appreciate the horror of the code, but I like the faux markup (FauXML?) in the comments. To people actually do that or is it further WTFery?

  • (cs) in reply to Wameng Vang
    Wameng Vang:
    The junior developer is creating a framework for the something that the compiler/tool already handles by throwing exceptions, who knows he/she may create a try/catch framework to cover more error handling. Somebody needs to re-take Programming 101.

    This isn't just a junior developer. If a junior developer tried to catch divide-by-zero himself, the worst he'd do is make things a little more complex by requiring a function call for division. This is an utterly incompetant junior developer. His function attempts to return an answer to the divide by zero when there is no correct answer, and in doing so breaks division for almost every other case.

  • (cs) in reply to Keko
    Keko:
    0/0 = FILE_NOT_FOUND

    No, dividing by zero yields OH SHI--

  • (cs)

    With integers it's even easier:

    lim(1/x) tends towards infinity as x approaches 0, and lim(2^(1/x)) also tends towards infinity as x approaches 0. And since the integer operation "*= 2" is identical with "<<= 2", any non-zero number divided by zero can be bit-shifted infinitely, which obviously results in 0 sooner or later, regardless of platform.

    Therefore,

    // Ultimate error-free division
    int Divide(int dividend, int divisor)
    {
        if (divisor == 0) // lim(0/x) tends towards 0 anyway
            return 0;
        return dividend / divisor;
    }
  • Not Dorothy (unregistered) in reply to HappilyEverAfter
    HappilyEverAfter:
    The person who wrote this ...code was my coworker.

    Wow, at least we now know where to go to get the best drugs. Seriously what was he thinking?

  • TopTension (unregistered)

    The pseudo XML might be used by some tool to extract documentation from the source code.

    TopTension

  • John (unregistered) in reply to Josh

    Visual Studio uses this format by default. There is not wtf in here. Doxygen (documentation program) can also read this kind of a markup.

  • John Price (unregistered) in reply to Josh

    That's standard comment markup for C#.

  • John (unregistered) in reply to John
    John:
    Visual Studio uses this format by default. There is not wtf in here. Doxygen (documentation program) can also read this kind of a markup.

    Damn. I tried to quote this: I'm not smart enough to appreciate the horror of the code, but I like the faux markup (FauXML?) in the comments. To people actually do that or is it further WTFery?

  • Just Some Guy (unregistered) in reply to n9ds
    if divider = 0 then don't even bother trying to divide because it will cause an error.

    Well, exceptions would typically be faster than running that test 1,000,000 times in the off chance that it might actually return True once.

  • Demi (unregistered)

    Although this seems C# code, I'd like to point out that Java can divide by zero without errors. System.out.println(5.0 / 0); will print "Infinity". System.out.println(0.0 / 0); will print "NaN".

  • Waffles (unregistered) in reply to Josh
    Josh:
    I'm not smart enough to appreciate the horror of the code, but I like the faux markup (FauXML?) in the comments. To people actually do that or is it further WTFery?
    Yeah, that's reasonably common. You do it so you can run a tool over your code to extract the comments, and generate a class/function reference such as http://www.icu-project.org/apiref/icu4c/index.html or http://msdn2.microsoft.com/en-us/library/system.string_members(VS.80).aspx

    It could be considered a bit of a wtf though, because people often assume that if they do this, they don't have to write any actual documentation...

  • Sgt. Preston (unregistered) in reply to Not Dorothy
    Not Dorothy:
    Seriously what was he thinking?
    That's what I'd like to know. This is truly a phenomenal WTF. I haven't the slightest idea what the coder's rationale was for any of it. He must have had something in mind, but it's a complete mystery to me. Any insights? Any guesses?
  • Anonymous German (unregistered) in reply to Just Some Guy
    Just Some Guy:
    New terms for standard mathematical words (eg "dividend" for "numerator"): check.

    Nah, this just confirms that the "coder" is a German...

  • Waffles (unregistered) in reply to Demi
    Demi:
    Although this seems C# code, I'd like to point out that Java can divide by zero without errors. System.out.println(5.0 / 0); will print "Infinity". System.out.println(0.0 / 0); will print "NaN".
    Yup, pretty much any language can do that. Including C# or C++ for that matter. It's a property of the IEEE floating point standard. Few languages bother to actually throw exceptions on a divide-by-zero.

    But of course, that doesn't really make the result "correct". It's hard to do much useful with a NaN. :)

  • Eric (unregistered) in reply to Demi
    Demi:
    Although this seems C# code, I'd like to point out that Java can divide by zero without errors. System.out.println(5.0 / 0); will print "Infinity".
    That's not always true though, 5.0 / 0 could easily be NaN or negative infinity.

    Captcha: hmm, I got this captcha once before, it's even in the auto-form-filler as the last used....

  • (cs)

    Can we combine this with isTrue() and doNothing() to get some reusability please?

  • Sgt. Preston (unregistered) in reply to Anonymous German
    Anonymous German:
    Just Some Guy:
    New terms for standard mathematical words (eg "dividend" for "numerator"): check.

    Nah, this just confirms that the "coder" is a German...

    When I was in elementary school in Canada during the late Cretacious, we called the two operands of a division the 'dividend' and the 'divisor' and called the result the 'quotient'. 'Numerator' and 'denominator' were used only in the context of the manipulation of fractions.
  • KM (unregistered) in reply to John

    It's called XML comments, and they are much better than your standard comments. By enabling compiling of XML Comments in Visual Studio, you can use a program like Sandcastle (http://www.codeplex.com/SHFB) to take your compiled code, and build a fully documented help file (or HTML file), complete with links (to your different classes and functions) and formatting derived from your code.

    Essentially you can create a whole MSDN-like help file for your code just from creating a little more descriptive and properly formatted (which the compiler checks) comments.

    -- KM

  • izb (unregistered)

    Stunning.

  • HappilyEverAfter (unregistered) in reply to Not Dorothy
    Not Dorothy:
    HappilyEverAfter:
    The person who wrote this ...code was my coworker.

    Wow, at least we now know where to go to get the best drugs. Seriously what was he thinking?

    I do not want to know. I'm afraid it would require insanity of some sort to fully understand him. So I will pass the understanding and just laugh :)

  • Beau "Porpus" Wilkinson (unregistered) in reply to John
    John:
    Visual Studio uses this format by default. There is not wtf in here. Doxygen (documentation program) can also read this kind of a markup.

    There is a potential flaw in your argument. You basically say that "Visual Studio does X by default therefore it's not a WTF." That assumes something that's not necessarily true. Some of us think that XML comments are a WTF. For instance, they lead to boilerplate type crap like:

        /// <summary>
        ///  shifts all windows
        /// </summary>
        /// <param name="processName">name of process</param>
    

    Truly, XML at its finest.

  • (cs) in reply to Vechni
    Vechni:
    Can we combine this with isVeryTrue() and doNothing() to get some reusability please?
    Fixed that for ya!
  • quantum (unregistered)

    The most surprising thing to me was that this code actually does prevent DivideByZeroErrors. Of course, so does akatherder's optimized version

  • (cs) in reply to HappilyEverAfter
    HappilyEverAfter:
    ...You really should have seen how he managed to convert integer to decimal :D
    Please, do post !!!
  • Mitch (unregistered)

    Anyone that codes like this, please leave the profession... NOW! Do not pass go, do not collect $200 ...

  • MikeC (unregistered)

    For one horrible moment, I thought something that one of my predecessors had done had come to light, that something I'd told someone in kinda-confidence had ended up on t'intertubes, and I was about to get whammy'd for breaking NDAs.

    Then I realised I'd never seen the code before, and there must be another MikeC out there somewhere. Phew. There's no way I'd forget something like that!

Leave a comment on “Division By Zero, Solved Yet Again”

Log In or post as a guest

Replying to comment #:

« Return to Article