Slashdot Mirror


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?

22 of 1,067 comments (clear)

  1. Yes by Fwipp · · Score: 5, Insightful

    Does anyone want their div by zero errors to result in anything other than zero?

    Yes.

    1. Re:Yes by AthanasiusKircher · · Score: 5, Funny

      Right, I don't even... ehh... totally confused. It's not aprils fools right? Did this article get approved just to mock the submitter, or has Slashdot gone totally of the rails?

      Well, Slashdot recently implemented a new engine for approving articles, but there was a place in the code where one could end up dividing by zero, and they just decided to arbitrarily set that value to "post a random nonsensical Ask Slashdot question."

      So, Timothy screwed something up... and, well, rather than throwing up an exception -- VOILA... this story was approved!

      I'm surprised you haven't noticed this before -- I think it's how most "Ask Slashdot" questions get posted these days.

  2. Simple by Anonymous Coward · · Score: 5, Insightful

    It means your code is wrong. Who knows what led up to that /0 error.

  3. Sounds like a plan! by Whip · · Score: 5, Insightful

    "Rather than failing when an unexpected condition arises, I want all software on my system to continue running with a possibly invalid or meaningless internal state."

    Sure, what could go wrong?

  4. And how do you know if there's an error then? by CSG_SurferDude · · Score: 5, Insightful

    So how do you know if you had an error if you return "0" for a divide by 0 error? Now you have a whole 'nother set of problems to code around.

  5. I want my division by zero errors to be errors by Lumpio- · · Score: 5, Insightful

    Because that usually means I'm trying to do something that's mathematically meaningless and I'd rather handle the special case than silently get a meaningless result.

  6. Infinity by Anonymous Coward · · Score: 5, Insightful

    I think infinity makes a bit more sense than zero. And max is the closest thing to infinity.

    1. Re:Infinity by lenart · · Score: 5, Informative

      Mathematically, the result is undefined. So neither is correct. Nor is anything inbetween.

    2. Re:Infinity by khallow · · Score: 5, Insightful

      Mathematicians don't know which rule has precedence for 0/0

      No, mathematicians known that there is no consistent number which would be an answer for 0/0. For example, take any number r and consider the fraction (rx)/x. For x not zero, it evaluates to r. Set x to zero and there's an argument that 0/0 should be r. But r wasn't special so you have an arbitrary argument with no special value of r indicated as the natural value of 0/0.

      Similarly, if you consider the fraction x/(x^2), you get an argument that 0/0 should be infinite (plus or negative depending on whether you approach zero from the positive side or negative). In other words, 0/0 is indeterminate with the value, if any, depending on how you approach 0/0.

    3. Re:Infinity by khallow · · Score: 5, Insightful

      How about evaluating 0/(0*0) versus (0/0)/0? Assuming 0/0=1 gives you inconsistent outcomes unless you're willing to sacrifice associativity of multiplication and division.

    4. Re:Infinity by vux984 · · Score: 5, Informative

      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.

    5. Re:Infinity by Jane+Q.+Public · · Score: 5, Insightful

      That is simply false. There are an infinite number of algorithms that might contain the (sub)expression X/X for which zero is a valid value of X.

      No, there aren't. There are zero algorithms were 0/0 is "valid". 0/0 is simply not a number. You don't get to make up mathematics as you go and make it a number, no matter how convenient you might find that. If your algorithm treats it as a valid number, your algorithm is wrong.

      In fact, most compilers and interpreters will barf if you try to feed them that. (Division by zero error, or "NaN", depending.) You'd have to do a bit of trickery to make it work at all.

      To assume it's a programming error is sheer unmitigated stupidity that I might expect from a mathematician that has never written a real program in his life.

      It isn't an "assumption". It *IS* a programming error. The fact that the result of division by zero is undefined is a fact of life, not just some made-up mathematical construct. You can prove that to yourself with a framing square and a couple of sticks, if you don't believe me.

      An algorithm that assumes pi is equal to 3.0 is no more stupid than assuming 0/0 is valid.

  7. I'm tired, too by linuxwrangler · · Score: 5, Funny

    Hi SlashDot. I'm a programmer who is tired of sanitizing inputs and checking for exceptions. Can you suggest a way to change the world so those things don't exist?

    --

    ~~~~~~~
    "You are not remembered for doing what is expected of you." - Atul Chitnis
    1. Re:I'm tired, too by 31415926535897 · · Score: 5, Funny

      Code in PHP

  8. Maybe by Anonymous Coward · · Score: 5, Funny

    Does anyone want their div by zero errors to result in anything other than zero?

    Yes.

    No.

    Maybe.

    1. Re:Maybe by Anonymous Coward · · Score: 5, Funny

      Does anyone want their div by zero errors to result in anything other than zero?

      Yes.

      No.

      Maybe.

      Maybe not.

      Wrong!

      Pineapple.

  9. I don't think you understand the definition by Thud457 · · Score: 5, Funny
    Division by zero - how many times does zero go into a number?

    100.0 / 0 = 3
    402350.32302 / 0 = 3
    pi / 0 = infinity
    1942 / 0 = 0
    194.3 / 0 = 0
    101 / 0 = 1
    1010 / 0 = 2
    200.02 / 0 = 3
    4004004 / 0 = 4

    Somebody please submit a RFP for this to C++17 standards committee.

    --

    the preceding comment is my own and in no way reflects the opinion of the Joint Chiefs of Staff

  10. Sighd by ledow · · Score: 5, Insightful

    You want to find out how many Euros in those Zimbabwean dollars you're keeping track of. The exchange rate fluctuates. The web-API you're using goes offline and returns zero, so you divide by zero. Whoops. How do you tell the difference between worthless numbers and just worthless currency?

    You want to draw an interlaced gif of some sort, so you do every nth line, then every n-1th line, as you get the interlaced lines and work down towards a full image with every row drawn. And then you cock up at the end, accidentally hit zero and you overwrite the first line thousands of times with garbage rather than spot the mistake.

    Zero is so completely the wrong answer, you don't even understand why. The actual real answer shouldn't even be the largest integer you can hold. And if it is, it could also be the smallest (i.e. largest negative). But actually it's none of them.

    Division by zero is NOT something that produces a number. It cannot happen. It cannot return zero (which is incredibly wrong), nor can it return any single other consistent constant. It should actually just error, which is why it does. It should produce something that's not a number (NaN). And it does exactly that.

    Divide by zero is like a null pointer. On the face of it is appears singularly useless. Why on earth would you want a pointer that you can't dereference? But it's there as an indicator. You cocked up. Majorly. If your maths is at all important at that point (a cell in a spreadsheet), then you're potentially losing billions of digits of accuracy.

    You can continue on blindly with your cockup quite easily. Any idiot can overload the divide operator to return zero when the denominator is zero. And you won't get any of those nasty errors. Errors which are indicative of an earlier error that you're just ignoring.

    There's a reason that, even back in the days of BASIC and very limited ROM space, you programmed in divide by zero as an error rather than just returning zero and documenting it. It's the same reason that you don't just "ignore" NULL pointer dereferences by saying "Oh, well, we won't call that function and just carry on from where we were then". Any idiot could make some kind of overload to allow that as well if they really wanted.

    The fact is that if you're dividing by zero you're doing something that's mathematically impossible. There is no amount of zeroes you can multiply to get anything other than zero. Not even if you multiply infinities of zeroes do you get anything other than zero. Hence division by zero of any non-zero integer is IMPOSSIBLE. It doesn't have an answer.

    And, like the square root of -1, if you just ignore it and pretend it exists you will run into all kinds of trouble. If you want to do something with it, in the same way that we use "i" to represent the square root of -1 to get lots of magical maths that actually works, use a language that recognises NaN and test against it.

    But I'll tell you now that it's quicker and easier to test if you're dividing by zero BEFORE you do the divide.

  11. Re:x/0 does not equal 0. by RackinFrackin · · Score: 5, Insightful

    Dividing any other number than zero by zero is well defined as infinity or minus infinity.

    We need a -1 Wrong mod for just this sort of post.

  12. Sounds like bugs by The+MAZZTer · · Score: 5, Funny

    caused by dividing by zero returning zero.

  13. Re:Bugs? by Anonymous Coward · · Score: 5, Insightful

    You think *you're* having problems. I'm seeing actual stories with titles like "Ask Slashdot: What's the Harm In a Default Setting For Div By Zero?"

  14. Re:Idiot by frovingslosh · · Score: 5, Interesting

    Absolute idiot. Reminds me of a time years ago when I was working at a University computer center. Despite my cultivated angry looks, a student came up to me with a printout and asked "Uh, what does this mean?" I said, "Oh, you want me to read it to you? It says: Error -divide by zero on line 50." I got a blank stare. So after a minute I further said" It means you are trying to divide by zero on line 50 of your code, which of course you can't do." That was responded to with another blank stare. So I said "look, here is line 50. See those two variables that you are multiplying together and then dividing into those other numbers? One of them must be reaching zero. Since you can't divide by zero the computer is trying to tell you that something has gone wrong. Go back, print out the variables inside the loop right before line 50 and see which one reaches zero. Then figure out why it is zero." The student said nothing and wandered away, apparently unhappy that I just didn't write the code for her.

    A few days later one of the student operators who worked for me there said to me "Remember that girl that you tried to help with the divide by zero problem? She's getting a B+ in her computer science class." Such is the state of the education system. This was a while ago, but as far as I can tell, and this post indicates, things have no gotten any better.

    No, you don't just ignore this problem and you absolutely don't put a system wide rule in effect to ignore the problem. If you get such an error it indicates a very fundamental problem wit the logic of the program. It is not trivial, and in real world situations could be deadly.

    And you don't just return the largest system number rather than zero, as some other idiots have suggested. That would be just as wrong and just as dangerous.

    And if you are really seeing this error often, I strongly suggest a change in profession to a short order chef.

    --
    I'm an American. I love this country and the freedoms that we used to have.