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?

9 of 1,067 comments (clear)

  1. Well... by TaleSpinner · · Score: 4, Interesting

    ...aside from the fact that it's completely wrong I can't see a problem with it.

  2. Math doesn't approve by spiritplumber · · Score: 4, Interesting

    Division by zero if anything would be +infinity or -infinity depending on signs, not zero. A while ago I wrote an autopilot that handled division by zero by looking at the signs and setting the result to (maxpos) or (maxneg), the zero's sign being derived from the variable's last value scavenged from the PID function.

    --
    Liberty - Security - Laziness - Pick any two.
    1. Re:Math doesn't approve by spiritplumber · · Score: 4, Interesting

      Well, division by zero should never happen, but you want it to be handled gracefully in case it does. Nobody wants the autopilot in charge of a barge train to segfault. Basically, every variable in the system was stored three times, past value - current value - predicted future value. If I saw a zero, I could use the past value to get the sign of the zero, and work from there.

      --
      Liberty - Security - Laziness - Pick any two.
  3. Bury Head in Sand by NickAragua · · Score: 4, Interesting

    This idea reminds me of "On Error Resume Next". The reason you don't do that is because a divide by zero indicates that you've got a logic failure somewhere else in your code. It's frequently easier to find an error when it's flashing big and red and throwing exceptions, rather than failing silently.

  4. Re:I want my division by zero errors to be errors by TopherC · · Score: 4, Interesting

    I agree here. One easy example is computing an average: add up the numbers and divide by N. What if you have no numbers to average and N == 0? That doesn't mean the average is zero, it means you don't have an average. You always have to check for /0 errors, not because you want to keep the program from crashing but because you need to handle all the special cases. It's usually (not always) better to crash to alert you to an un-handled condition than to pretend nothing is wrong.

    Should all null pointer exceptions or segfaults be handled quietly in some arbitrary way, in order to make software more "robust?"

  5. Re:Infinity by dissy · · Score: 4, Interesting

    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

    But divide by zero isn't covered by either of those two rules of algebra.

    Asking what is X divided by zero is no different than asking what is Y plus red, or what is Z times pineapple.

    I say focus on a proper mathematical answer for multiplying by blue first, and then apply it to the equally nonsensical divide by zero question.

  6. Re:I'm tired, too by Anonymous Coward · · Score: 4, Interesting

    Yes. I wrote a 3D modeling program a very long time ago. It involved a lot of fractions and subtracting. Lines that were perfectly horizontal or vertical would result in divide-by-zero errors, since X1-X2 in the denominator was disastrous.

    So I rotated the entire universe by an irrational number of degrees on each axis. The problem went away.

  7. 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.
  8. Re:Floating point by tristes_tigres · · Score: 4, Interesting

    Oh my god, this whole discussion is so misguided it hurts my eyes to look at. Why don't you people go educate yourself about floating-point arithmetics? IEEE754 standard was designed by top-notch numerical expert and YOU IGNORE IT AT YOUR AND YOUR USERS' PERIL.

    And yeah, division of nonzero by +zero must give Inf, and there are actual useful numerical algorithms that make use of that.