Ask Slashdot: What's the Harm In a Default Setting For Div By Zero?
New submitter CodeInspired writes: After 20 years of programming, I've decided I'm tired of checking for div by zero. Would there be any serious harm in allowing a system wide setting that said div by zero simply equals zero? Maybe it exists already, not sure. But I run into it all the time in every language I've worked with. Does anyone want their div by zero errors to result in anything other than zero?
Burning karma here to see if anyone else has the same problem. Mod offtopic if you like.
New posts of mine aren't showing up for about half an hour typically. Do they need to be staff approved now or something?
Second, on several front page stories, I no longer have the option to post. They say, "Nothing to see here. Move along" and "Archived discussion".
I think the new design changes are pretty alright, but those two are breaking changes for me.
I won't mention names to hopefully dodge any lawyers lurking about, but "a friend" works in the airline industry and a certain commercial aircraft flight computer on receiving bad or missing data from the Inertial Reference System would happily go on reporting a value without indicating an error. Luckily, this friend became aware of the problem trying to resolve a CG issue before takeoff. Fun times.
When you have 0/0, you hit two "obvious" but contradictory rules in basic algebra:
Rule one: anything multiplied by zero is zero
Rule two: anything divided by itself is one
Mathematicians don't know which rule has precedence for 0/0, so there's no way a dumb machine can figure it out, which is why most programing languages just throw an exception if zero is the denominator.
If it works, it's obsolete
Mathematically, the result is undefined. So neither is correct. Nor is anything inbetween.
Actually IEEE floating point has a signed zero (+0 and -0 are different values) to solve exactly that. If x is positive, x/+0 is +infinity, x/-0 is -infinity. 0/0 (with any type of 0) returns NaN.
However I believe the article was talking about *integer* division by zero, not floating point.
Let me rewrite that as:
Rule 1: 0*X (where X = 1/0)
Rule 2: X/X (where X = 0)
I've had this argument often with APL fans who don't get that as obvious as 0/0 = 1 may sound, it's not mathematically sound.
If it works, it's obsolete
When you have 0/0, you hit two "obvious" but contradictory rules in basic algebra:
Rule one: anything multiplied by zero is zero
Rule two: anything divided by itself is one
Mathematicians don't know which rule has precedence for 0/0, so there's no way a dumb machine can figure it out, which is why most programing languages just throw an exception if zero is the denominator.
It's mathematically possible to do some form of 0/0 using limits and the calculus.
lim x->0 x/x = 1
Also lets you do some interesting other things, like:
lim x->0 x^2/x = 2
lim x->0 sin(x) / x = 1
Shouldn't be too hard to get a function that gives you the correct "approximate" value of the function near the indeterminate point, if there is one. It's just a bunch of special cases that you have to check for every time you do an operation that might possibly result in a div/0 error, right? Go ahead. DO IT! I'll wait.
Asking what is X divided by zero is no different than asking what is Y plus red, or what is Z times pineapple.
Actually these do make more sense.
Ah, you naive physicists. Your attempts at trying to attach the math to reality always make me smile. And your believe that the human words mean the same thing in math - that's just hilarious.
Because, it is just math. It's human invention and we can do whatever we like with it.
But to the point: division by zero is not illegal - it is simply not defined.
The usual mathematical workaround is to simply define another operation - "zivition" or whatever you like - which is just like division, but in case of X/0, it has value of X or 1 or 0 or whatever you like. Or one can even go step further and define the zivition as ternary operation: ziv(X, Y, Z) = { div(X,Y), Y!=0; Z, Y==0 }
All hope abandon ye who enter here.
When you have 0/0, you hit two "obvious" but contradictory rules in basic algebra:
Rule one: anything multiplied by zero is zero
Rule two: anything divided by itself is one
Ugh no, just no.
"Rule one: anything multiplied by zero is zero"
Yes, this is called, amongst other things, the zero property of multiplication. However 0/0 is not a multiplication and the rule is not relevant, and there is no conflict.
Secondly your "rule two" is not actually rule of algebra. There is no rule x/x = 1.
There is an identity rule for division: anything divided by one is itself (x/1 = x) but there is no rule that says x/x = 1
You can derive "rule two" from the identity rule for multiplication x*1 = x --> x/x = 1
However, that transformation always stipulates that x 0 because division by zero is undefined.
Mathematicians have no issue determine which rule has precedence, because neither rule applies to 0/0.
There is no conflict. Division by zero is specifically "undefined".
Consider the equation; x/x.
http://www.wolframalpha.com/in...
The graph of the function is a horizontal line at y=1, with a discontinuity at 0. (if x=0, x/x=0/0) So 0/0 should be 1 right? Because everywhere else on the graph x/x = 1??
http://www.wolframalpha.com/in...
Now consider the equation 2x/x.
http://www.wolframalpha.com/in...
As x approaches 0 (lim x->0) from either the left or right the limit of the equation is 2. A graph of the function is horizontal line at y=2, with a discontinuity at 0. But every where else 2x/x = 2. So shouldn't 2(0)/0 = 0/0 = 2? So 0/0 should be 2 right?
http://www.wolframalpha.com/in...
Neither. Its not defined.
Now consider the equation 1/x.
http://www.wolframalpha.com/in...
As x approaches 0 from the left it goes to negative infinity. As x approaches 0 from the right it goes to positive infinity. This graph doesn't even suggest a value for 0/0? Is it + infinity? Or - infinity?
I can write a function that makes 0/0 look like it should be anything I want.
0/0 is undefined. It doesn't violate any rules of algebra. It's a rule of algebra that division by 0 is undefined.
"In many situations, you can use L'Hopital's Rule to resolve 0/0.
No, you can't."
Yes you can.
"At least any mathematician will look at you with disgust if you claim that"
No mathematician will look at you with disgust by claiming that. Armchair wannabes, on the other hand...
Because any mathematician will know this is about programing, an algorithm context and, therefore, *in many situations* 0/0 will be a context case of f(x)/g(x) as both functions' independent variable approach zero, so a limit, that is, the very scenario L'Hopital's rule applies to.
There will be, of course, many other situations where this will not be the case, a thing both the mathematician and the parent poster are also well aware of.
>Does the fact that x/0 mean that it is not solvable?
Yes. Yes it does.
In the most trivial simplification x/0 will be either positive or negative infinity, depending on the sign of x. If x=0 then we can't even say that much.
It's not a matter of "we haven't figured out how to wrap it up nice and neat", it's "We've tried to wrap it up nice and neat, but determined that the behavior is *extremely* context-sensitive and provably CAN'T be wrapped up". If a function has a discontinuity in the form 0/0 then you can "creep up" on it, evaluating the function at smaller and smaller distances from the discontinuity, and depending on the *exact* details the function may approach negative infinity, positive infinity, 1, 2, -237.428, or *any* other value. Or it may not approach any value at all - some functions will have a different value depending on whether you're creeping up from the left or the right, and some will even begin oscillating wildly with a frequency approaching infinity as they approach the discontinuity, so that it's not possible to get *any* approximate answer.
THAT is what is generally meant when a mathematician says an operation is undefined. Not that we don't understand it, but that we DO understand it, and no general answer is possible.
It's a very different situation from Sqrt(-1), which had provably consistent behavior [ sqrt(-1)^2 = -1 ] but simply couldn't be represented on the standard number line, requiring expanding to a two-dimensional complex number system in order to represent its behavior.
--- Most topics have many sides worth arguing, allow me to take one opposite you.