Comment On Division By Zero, Solved Yet Again

I thought that, when James Anderson announced the invention of nullity (i.e. Φ, or 0/0), we had finally gotten past the whole "impossibility" of dividing by zero. I don't know about you, but after installing the latest service pack including the "nullity patch," my programs happily hum along when they divide by zero. So why then did Mike C.'s predecessor bother with a DivisionWithZero method ... and why did he do it like this... [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Division By Zero, Solved Yet Again

2007-10-15 09:25 • by steve (unregistered)
wow. just wow.

Re: Division By Zero, Solved Yet Again

2007-10-15 09:25 • by shakin
After reading the description and code comment my brain hurt too much to read the code.

Re: Division By Zero, Solved Yet Again

2007-10-15 09:31 • by death
Um... This is TOO bad to be true....

Re: Division By Zero, Solved Yet Again

2007-10-15 09:32 • by amoore (unregistered)
Is the real WTF the fact that this code is commented?

Re: Division By Zero, Solved Yet Again

2007-10-15 09:34 • by Welbog
Is there any input at all that this thing does right? (Other than both inputs the same.)

Let us count the ways...

2007-10-15 09:35 • by 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.

Re: Division By Zero, Solved Yet Again

2007-10-15 09:39 • by akatherder
// Version 2: Optimized

Random r = new Random();
return r;

Re: Division By Zero, Solved Yet Again

2007-10-15 09:39 • by GettinSadda
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;

Re: Division By Zero, Solved Yet Again

2007-10-15 09:41 • by amoore (unregistered)
157130 in reply to 157126
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.

Re: Division By Zero, Solved Yet Again

2007-10-15 09:43 • by Anonymous Coward (unregistered)
157131 in reply to 157122
OOUCHAAAAAAAAARGGH! The goggles, they do nothing!

Re: Division By Zero, Solved Yet Again

2007-10-15 09:44 • by Anon (unregistered)
Alex you should warn before reading that code:

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

Re: Division By Zero, Solved Yet Again

2007-10-15 09:53 • by 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..................

Re: Division By Zero, Solved Yet Again

2007-10-15 09:53 • by LiquidFire
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;

Re: Division By Zero, Solved Yet Again

2007-10-15 09:55 • by 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!?

Re: Division By Zero, Solved Yet Again

2007-10-15 09:59 • by Anonymous (unregistered)
157138 in reply to 157136
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.

Re: Division By Zero, Solved Yet Again

2007-10-15 10:00 • by 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

Re: Division By Zero, Solved Yet Again

2007-10-15 10:02 • by ParkinT
//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>

Re: Division By Zero, Solved Yet Again

2007-10-15 10:02 • by Keko (unregistered)
0/0 = FILE_NOT_FOUND

Re: Division By Zero, Solved Yet Again

2007-10-15 10:05 • by 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; }

Re: Division By Zero, Solved Yet Again

2007-10-15 10:06 • by ParkinT
Wouldn't this whole thing be easier with:

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

Re: Division By Zero, Solved Yet Again

2007-10-15 10:17 • by 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

Re: Division By Zero, Solved Yet Again

2007-10-15 10:20 • by n9ds
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?

Re: Division By Zero, Solved Yet Again

2007-10-15 10:23 • by Sulka (unregistered)
157148 in reply to 157126
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. :)

Re: Division By Zero, Solved Yet Again

2007-10-15 10:27 • by 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?

Re: Division By Zero, Solved Yet Again

2007-10-15 10:27 • by SuperousOxide
157150 in reply to 157135
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.

Re: Division By Zero, Solved Yet Again

2007-10-15 10:27 • by Quietust
157151 in reply to 157141
Keko:
0/0 = FILE_NOT_FOUND


No, dividing by zero yields OH SHI--

Re: Division By Zero, Solved Yet Again

2007-10-15 10:32 • by Faxmachinen
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;
}

Re: Division By Zero, Solved Yet Again

2007-10-15 10:34 • by Not Dorothy (unregistered)
157155 in reply to 157146
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?

Re: Division By Zero, Solved Yet Again

2007-10-15 10:34 • by TopTension (unregistered)
The pseudo XML might be used by some tool to extract documentation from the source code.

TopTension

Re: Division By Zero, Solved Yet Again

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

Re: Division By Zero, Solved Yet Again

2007-10-15 10:36 • by John Price (unregistered)
157158 in reply to 157149
That's standard comment markup for C#.

Re: Division By Zero, Solved Yet Again

2007-10-15 10:37 • by John (unregistered)
157159 in reply to 157157
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?

Re: Division By Zero, Solved Yet Again

2007-10-15 10:45 • by Just Some Guy (unregistered)
157161 in reply to 157147
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.

Re: Division By Zero, Solved Yet Again

2007-10-15 10:48 • by 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".

Re: Division By Zero, Solved Yet Again

2007-10-15 10:49 • by Waffles (unregistered)
157163 in reply to 157149
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...

Re: Division By Zero, Solved Yet Again

2007-10-15 10:49 • by Sgt. Preston (unregistered)
157164 in reply to 157155
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?

Re: Let us count the ways...

2007-10-15 10:52 • by Anonymous German (unregistered)
157166 in reply to 157127
Just Some Guy:
New terms for standard mathematical words (eg "dividend" for "numerator"):
check.


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

Re: Division By Zero, Solved Yet Again

2007-10-15 10:52 • by Waffles (unregistered)
157167 in reply to 157162
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. :)

Re: Division By Zero, Solved Yet Again

2007-10-15 10:55 • by Eric (unregistered)
157168 in reply to 157162
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....

Re: Division By Zero, Solved Yet Again

2007-10-15 10:55 • by Vechni
Can we combine this with isTrue() and doNothing() to get some reusability please?

Re: Let us count the ways...

2007-10-15 10:57 • by Sgt. Preston (unregistered)
157171 in reply to 157166
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.

Re: Division By Zero, Solved Yet Again

2007-10-15 10:59 • by KM (unregistered)
157172 in reply to 157159
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

Re: Division By Zero, Solved Yet Again

2007-10-15 10:59 • by izb (unregistered)
Stunning.

Re: Division By Zero, Solved Yet Again

2007-10-15 11:05 • by HappilyEverAfter (unregistered)
157176 in reply to 157155
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 :)

Re: Division By Zero, Solved Yet Again

2007-10-15 11:08 • by Beau "Porpus" Wilkinson (unregistered)
157178 in reply to 157157
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.

Re: Division By Zero, Solved Yet Again

2007-10-15 11:10 • by snoofle
157179 in reply to 157169
Vechni:
Can we combine this with isVeryTrue() and doNothing() to get some reusability please?

Fixed that for ya!

Re: Division By Zero, Solved Yet Again

2007-10-15 11:11 • by quantum (unregistered)
The most surprising thing to me was that this code actually does prevent DivideByZeroErrors. Of course, so does akatherder's optimized version

Re: Division By Zero, Solved Yet Again

2007-10-15 11:11 • by snoofle
157181 in reply to 157146
HappilyEverAfter:
...You really should have seen how he managed to convert integer to decimal :D

Please, do post !!!

Re: Division By Zero, Solved Yet Again

2007-10-15 11:15 • by Mitch (unregistered)
Anyone that codes like this, please leave the profession... NOW! Do not pass go, do not collect $200 ...

Re: Division By Zero, Solved Yet Again

2007-10-15 11:15 • by 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!
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment