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?

44 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. Bugs? by weilawei · · Score: 4, Informative

    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.

    1. Re:Bugs? by blue+trane · · Score: 4, Informative

      Titles are now partially obscured by the comment count and the classification icons. Screenshot: http://snag.gy/ciB02.jpg

    2. Re:Bugs? by aardvarkjoe · · Score: 4, Funny

      Didn't you hear? Slashdot is trying this cutting-edge "Agile Development" thing. The idea is that instead of testing your changes, you just make them directly on your production site and see what happens. It's the wave of the future!

      --

      How can we continue to believe in a just universe and freedom to eat crackers if we have no ale?
    3. 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?"

  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 Anonymous Coward · · Score: 4, Informative

      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.

  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.

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

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

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

  8. 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 joeblog · · Score: 4, 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

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

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

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

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

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

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

    8. Re:Infinity by rwa2 · · Score: 4, 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

      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.

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

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

  9. 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.
  10. 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.

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

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

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

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

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

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

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

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

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

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

  19. 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.
  20. 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.

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

    caused by dividing by zero returning zero.

  22. 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.
  23. 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.

  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.