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?

33 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 Venerable+Vegetable · · Score: 3, Insightful

      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? Ok don't answer that last question.

      Maybe the submitter would care to submit an example of where he thinks it would be appropriate to equal divide by zero to zero, because I honestly don't know where to start.

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

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

  3. WTF is this shit? by Anonymous Coward · · Score: 3, Insightful

    Umm, wtf is this shit?

  4. 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?

    1. Re:Sounds like a plan! by Dunbal · · Score: 3, Insightful

      Yeah but for some people "if(investors >= 1)" is hard.

      --
      Seven puppies were harmed during the making of this post.
  5. 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.

  6. 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.

  7. 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 Samantha+Wright · · Score: 4, Insightful

      More importantly is what happens when you graph it: the limit of 1/x as x approaches zero is discontiguous. It's positive infinity when descending on the positive numbers, but negative infinity when ascending from the negatives. No one value can represent both!

      --
      Bio questions? Ask me to start a Q&A journal. Computer analogies available for most topics!
    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 ShanghaiBill · · Score: 4, Insightful

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

      In many situations, you can use L'Hopital's Rule to resolve 0/0. But a properly written program should never get in a situation of dividing by zero, and this is one of the dumbest "Ask Slashdot" questions in a while. Masking the interrupt makes about as much sense as driving blindfolded so you don't see the people you are running over.

    4. 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.

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

      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 your code ever has the expression X / X and X is capable of being 0, you have a bug in your algorithm. It really is as simple as that.

      --
      -- All that is necessary for the triumph of evil is that good men do nothing. -- Edmund Burke
    6. 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.

  8. When a man is tired of checking for divide-by-zero by Anonymous Coward · · Score: 3, Insightful

    ... he is tired of life.

  9. Don't pretend everything's OK by Anonymous Coward · · Score: 4, Insightful

    There's a reason you're calculating a division. That number is supposed to be used for something. If your program is dividing by zero, the data it's working with is wrong. The consequences of just pretending to have a valid answer could vary from totally harmless to nuclear winter. But what's the upside?

  10. Infinity (max Float64) seems reasonable by tomxor · · Score: 3, Insightful

    Zero doesn't make a lot of sense if for instance you are dividing something by a dynamically changing denominator that hits zero at some point... the result would change from a very large number suddenly to 0.

    Divide by zero is infinity so using the largest supported number type seems reasonable for the calculation of real numbers.

  11. Re:x/0 does not equal 0. by Anonymous Coward · · Score: 3, Insightful

    No, mathematically, division by zero is undefined.

    Only engineers think it's infinity!

  12. Re:Why have the post counts moved? by amicusNYCL · · Score: 2, Insightful

    Apparently so that everyone can hit some stupid Share button by mistake instead.

    --
    "Our two-party system is like a bowl of shit looking at itself in a mirror." - Lewis Black
  13. Re:x/0 does not equal 0. by Verdatum · · Score: 4, Insightful
    No, it does not equal infinity.

    The Limit as x aproaches 0+ of a/x = infinity.

    But the Limit as x approaches 0- of a/x = negative infinity.

    because this represents a jump-discontinuity, the value of a/0 is just plain undefined.

    This is like week-1 of high school precalc shit. Come on.

  14. 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.

  15. Re:The Simplest Answer... by __aaclcg7560 · · Score: 4, Insightful

    Perhaps the Big Bang was a divide by zero error? That would explain a lot.

  16. Re:Identities by Fwipp · · Score: 3, Insightful

    No, 0/0 is still undefined.

  17. Was slashdot hacked today? by vivaoporto · · Score: 4, Insightful
    Because it is not, and it would cause code like to fail for a = 1, b = 2 and c = 0:

    if ((a/c) == (b/c)) {
    // It is safe to assume that a == b
    }

    In a code as simple as that it is easy to spot but in more complex code this simple verification may be done in more steps or split in many different operations like:

    speed1 = distance1 / time;
    speed2 = distance2 / time;
    some_function(speed1, speed2);

    where some_function(speed1, speed2), built by other team has:

    if (speed1 == speed2) {
    // distance was the same, do something accordingly
    }

    Now, how this question was accepted as legitimate in an advanced forum like this it is amazing. Rock bottom.

  18. Eugh by Dunbal · · Score: 4, Insightful

    After 20 years of programming, I've decided I'm tired of checking for div by zero.

    I am amazed that someone can persist in a career for 20 years without a clue as to what they are doing. If you are getting divide by zero errors there is something wrong with your logic. Don't blame the computer and certainly don't try to outsmart the computer which is trying to help you by pointing this out. Div by zero errors aren't something you should gloss over, they're something that should make you sit down and come up with an algorithm that actually does what you thought it was supposed to do.

    --
    Seven puppies were harmed during the making of this post.
  19. 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.

  20. Re:x/0 does not equal 0. by mark-t · · Score: 3, Insightful

    For definitions of "well-defined" that selectively ignore the definitions that mathematicians use, perhaps.

  21. 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?"

  22. Re:x/0 does not equal 0. by Xtifr · · Score: 3, Insightful

    You only think this makes sense in the real world because you phrased the answer improperly."No one gets the apple" does not answer "how much of the apple does each person get?" The answer to "how much of the apple does each person get?"is "the question makes no sense because there are no people, so there is no 'each person'." Which is the real-world equivalent of what the mathematics says ("No answer/not-a-number").

  23. Re:The Simplest Answer... by Moof123 · · Score: 3, Insightful

    Usually it just lets the magic smoke out. A divide by zero is usually called a short circuit and makes a lot of heat for a short period of time, after that you get the smell of the magic smoke.

  24. Depends on the application I suppose. by hey! · · Score: 4, Insightful

    For certain kinds of abstract algebras division by zero is even defined, although typically as a special element like infinty, but not 0 (the additive identity element) which would lead to all kinds of peculiar situations: like 0 * 1/0 = 0, so 1/0 has to be regarded as both 1 and 0 at the same time.

    BUT if you're dealing with regular numbers or anything that obeys the axioms of an algebraic field, division by zero always represents a failure of the assumptions under which you undertake the calculation. Since it is a failure of assumptions it should always be treated as an exception to normal logic flow. If the correct -- or more accurately speaking the safest -- course of action to take is to assign a value of 0 to a calculation then of course you can do that, but that's still a case of exception handling. Building that as default behavior FORCES a certain response to an exception which of course the language designer can't possibly know is the safest response.

    In fact, even implicitly allowing division by zero in a sequence of algebraic manipulations can lead to faulty results even without actually performing the arithmetic operation in question. That's behind several algebraic "paradoxes" that have made the rounds of the Internet over the years, such as the following algebraic "proof" that "2 = 1":

    Let a = b
    [1] a^2 = a*b // multiply both sides by a
    [2] a^2 - b^2 = ab - b^2 // subtract b^2 from both sides
    [3] (a-b)*(a+b) = b * (a - b) // factor both sides
    [4] a + b = b // divide both sides by (a-b)
    [5] b + b = b // substitute b for a on the left side
    [6] 2b = b // collect terms
    [7] 2 = 1 // factor out b

    It all looks kosher, but it's not because there's a division by zero in the *algebra*. I've actually seen programs that give faulty errors because the programmer simplified expressions in ways that commit this exactly blunder. The language and compiler can't catch this because the division by zero occurred in the programmer's head.

    --
    Post may contain irony: discontinue use if experiencing mood swings, nausea or elevated blood pressure.
  25. Re:Idiot by blue+trane · · Score: 1, Insightful

    The problem lies in the inexpressivity of math, not in the student. Division by zero happens naturally and it is math's problem if it can't deal with it. Natural language can deal with it, nature deals with it. Math fails to express nature adequately.

    Example: water flows over an area of land and divides itself evenly: so 1 cubic meter of water / 1 square meter of flat land. Now the land erodes and becomes 0 square meters. The water doesn't have to resort to error-handling code, it knows what to do, it flows on. But math gets hung up at the point when the land disappears, your code throws an error, and you have to handle that. But nature doesn't throw any error, it handles division by zero naturally and seamlessly. Again, math fails to describe nature very well.