Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Programming Math

Ask Slashdot: What's the Harm In a Default Setting For Div By Zero? 1067

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?
This discussion has been archived. No new comments can be posted.

Ask Slashdot: What's the Harm In a Default Setting For Div By Zero?

Comments Filter:
  • Yes (Score:5, Insightful)

    by Fwipp ( 1473271 ) on Thursday June 18, 2015 @05:05PM (#49939301)

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

    Yes.

    • Infinity (Score:5, Insightful)

      by Anonymous Coward on Thursday June 18, 2015 @05:09PM (#49939343)

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

      • Re:Infinity (Score:4, Informative)

        by joeblog ( 2655375 ) on Thursday June 18, 2015 @05:25PM (#49939551) Homepage

        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.

        • Re:Infinity (Score:5, Interesting)

          by dissy ( 172727 ) on Thursday June 18, 2015 @05:35PM (#49939691)

          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.

          • Re: (Score:3, Informative)

            by joeblog ( 2655375 )

            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.

            • Re:Infinity (Score:5, Insightful)

              by khallow ( 566160 ) on Thursday June 18, 2015 @06:04PM (#49940021)
              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.
            • Re:Infinity (Score:4, Insightful)

              by ranton ( 36917 ) on Thursday June 18, 2015 @08:23PM (#49941201)

              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.

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

                Some old code I had years and years ago needed to calculate some bits and bobs from parameters in a data file. Because reasons many parameters were often missing. So I initialised everything to 0/0, then read in what existed in the data file and did the computations.

                If for calculations where all the parameters were defines, I got out a number, otherwise, I got NaN :)

                Worked

        • Re:Infinity (Score:5, Insightful)

          by Samantha Wright ( 1324923 ) on Thursday June 18, 2015 @05:52PM (#49939883) Homepage Journal
          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!
        • Re:Infinity (Score:5, Insightful)

          by khallow ( 566160 ) on Thursday June 18, 2015 @05:57PM (#49939933)

          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.

        • Re:Infinity (Score:5, Insightful)

          by ShanghaiBill ( 739463 ) on Thursday June 18, 2015 @05:57PM (#49939937)

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

          In many situations, you can use L'Hopital's Rule [wikipedia.org] 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.

          • by AmiMoJo ( 196126 )

            This. If the OP is tired of checking for this condition, they are doing it wrong. It's not hard to write code where there is simply no possibly of a division by zero happening.

            Even when handling external input you need to validate it in almost every case, so adding a simple !=0 test is trivial and a minor part of the bigger problem.

            • adding a simple !=0 test is trivial and a minor part of the bigger problem.

              Wrong !
              Of course, !=0 is fine when you deal with integers.
              But when you deal with floating point values, !=0 does not work.
              This is because there are rounding errors, the zero that is displayed can be stored internally as 10^-9, and rounded to 0 because the printing function uses 8 decimals.

              You have to use:
              fabs(value) > delta
              where delta corresponds to the rounding error.
              If you work with single precision, you can probably use delta=10^-6
              For double-precision, you need to verify the accumulated rounding error

              • That's why I never use floating point. It's a mess. It's impossible to achieve a uniform random distribution of floats. The space isn't uniform.
                Fixed point is nice, as long as you have an idea of how big your numbers will be.

          • While I will respect the idea the you think this is the dumbest question in a while, It's does show what people are feeling and being frustrated by. And what I have found great about Slashdot is some of the more creative reply's. I am looking forward to this.

            Personally, I think, clean code with check's and filters makes for better programs, but today's behavior seems to indicate that product needs to be out the door ASAP or you are out the door ASAP. so sloppy code consistently comes forth.

        • Re:Infinity (Score:5, Informative)

          by rwa2 ( 4391 ) * on Thursday June 18, 2015 @06:09PM (#49940087) Homepage Journal

          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.

        • Re:Infinity (Score:5, Informative)

          by vux984 ( 928602 ) on Thursday June 18, 2015 @07:12PM (#49940679)

          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... [wolframalpha.com]

          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... [wolframalpha.com]

          Now consider the equation 2x/x.

          http://www.wolframalpha.com/in... [wolframalpha.com]

          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... [wolframalpha.com]

          Neither. Its not defined.

          Now consider the equation 1/x.

          http://www.wolframalpha.com/in... [wolframalpha.com]

          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.

      • it approaches infinity if you start at one and get closer and closer and closer to 0. N/.00000001 is pretty high.

        But it approaches negative infinity if you start at -1 and get closer and closer and closer to 0. N/-.00000000001 is pretty low.

        because of this discontinuity...the largest discontinuity possible in fact, the actual value of N/0 cannot be any of these compromises. It has to not exist.

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

        I think the biggest problem is that depending on the application, you might want different things to happen.
        I find in my code, that I tend to want one of 4 mutually exclusive things to happen:
        1) If denominator is zero, die.
        2) If denominator is zero, set denominator to 1 and continue.
        3) If denominator is zero, set denominator to 0.0001 (or some other similiar small number) and continue.
        4) If denominator is zero, set final result of x/0 to zero.

        Honestly, in my experience #4 (which is what the original pos

    • Re:Yes (Score:4, Insightful)

      by Venerable Vegetable ( 1003177 ) on Thursday June 18, 2015 @05:36PM (#49939707)

      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.

      • This sounds so much like a beginning programmer question. Sorts of things I used to hear when being a teaching assistant. Too much effort, can this be simpler?

        Now in some cases, people may not care. There are a lot of cases where one ignores errors in real life. But lots of programmers love to crash at a moment's notice: sprinkle the code liberally with asserts rather than try to recover, let main() catch all errors and then print an error and exit, etc. However if you're ignoring an error you should a

      • Re:Yes (Score:5, Funny)

        by AthanasiusKircher ( 1333179 ) on Thursday June 18, 2015 @08:03PM (#49941091)

        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.

    • by hey! ( 33014 ) on Thursday June 18, 2015 @07:03PM (#49940597) Homepage Journal

      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.

  • Simple (Score:5, Insightful)

    by Anonymous Coward on Thursday June 18, 2015 @05:06PM (#49939309)

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

  • Bugs? (Score:5, Informative)

    by weilawei ( 897823 ) on Thursday June 18, 2015 @05:06PM (#49939311)

    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.

  • WTF is this shit? (Score:3, Insightful)

    by Anonymous Coward on Thursday June 18, 2015 @05:06PM (#49939313)

    Umm, wtf is this shit?

  • by Whip ( 4737 ) on Thursday June 18, 2015 @05:08PM (#49939321)

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

    • by Anonymous Coward on Thursday June 18, 2015 @05:25PM (#49939547)

      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.

  • 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.
  • by Lumpio- ( 986581 ) on Thursday June 18, 2015 @05:08PM (#49939335)
    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.
    • by TopherC ( 412335 ) on Thursday June 18, 2015 @05:33PM (#49939667)

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

  • Well... (Score:5, Interesting)

    by TaleSpinner ( 96034 ) on Thursday June 18, 2015 @05:09PM (#49939341)

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

  • by Anonymous Coward on Thursday June 18, 2015 @05:10PM (#49939345)

    ... he is tired of life.

  • Math doesn't approve (Score:5, Interesting)

    by spiritplumber ( 1944222 ) on Thursday June 18, 2015 @05:10PM (#49939347) Homepage
    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.
  • Bury Head in Sand (Score:4, Interesting)

    by NickAragua ( 1976688 ) on Thursday June 18, 2015 @05:10PM (#49939353)
    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.
    • 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.

      Or a data input failure.

      If I'm streaming data in from a sensor, I expect there to be the odd data failure. Or lost packet. Or alpha particle hitting a memory core. Or something.

      My logic might be perfect; if the data is screwy and I pass it in to process it, well that was a silly move.

      I'd prefer 100 lines of validation code to throw out bad data before it hits the processing code than 10,000 lines of code to validate within the data processing.

      But that's just me.

  • by NEDHead ( 1651195 ) on Thursday June 18, 2015 @05:10PM (#49939357)

    no, because div by zero is not equal to zero

  • by mlts ( 1038732 ) on Thursday June 18, 2015 @05:11PM (#49939371)

    I'd rather either have an exception thrown or a "NaN" value used than a zero returned. A divide by zero error is nasty, but just "papering over" it by returning a zero is only going to introduce more subtle bugs in the code.

  • by linuxwrangler ( 582055 ) on Thursday June 18, 2015 @05:12PM (#49939379)

    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?

  • by Anonymous Coward on Thursday June 18, 2015 @05:17PM (#49939437)

    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?

  • by tomxor ( 2379126 ) on Thursday June 18, 2015 @05:17PM (#49939447)

    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.

  • by FranTaylor ( 164577 ) on Thursday June 18, 2015 @05:24PM (#49939537)

    Would there be any serious harm in allowing a system wide setting that said div by zero simply equals zero?

    What could possibly go wrong when you assert that infinity equals zero? Well, let's see, your logic errors will go undetected, for one. If you are dividing by zero YOU ARE DOING IT WRONG. You might as well just ignore bus errors as well. And while you are at it, just ignore if the disk is full and keep writing anyway. What could go wrong?

  • Sighd (Score:5, Insightful)

    by ledow ( 319597 ) on Thursday June 18, 2015 @05:28PM (#49939595) Homepage

    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.

  • by physicsphairy ( 720718 ) on Thursday June 18, 2015 @05:30PM (#49939617)

    2/0.1 = 20
    2/0.01 = 200
    2/0.00000001 = 200000000
    2/0.000000000000000000000000000000000000000000000001 = 0

    Exactly as you would expect!

  • by Kjella ( 173770 ) on Thursday June 18, 2015 @05:36PM (#49939711) Homepage

    Let's say I have have product sales like 4,6,0,5 and want % changes:

    6/4-1 = 0.5 = +50% ok
    0/6-1 = -1 = -100% ok
    5/0-1 = -1 = -100% epic fail

    Though I really wish you could get NULL when doing a divide by 0 in a database instead of an error.

  • by vivaoporto ( 1064484 ) on Thursday June 18, 2015 @05:37PM (#49939725)
    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.

  • Eugh (Score:5, Insightful)

    by Dunbal ( 464142 ) * on Thursday June 18, 2015 @05:38PM (#49939729)

    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.

  • And 2+2 = 5 (Score:4, Funny)

    by PPH ( 736903 ) on Thursday June 18, 2015 @05:40PM (#49939749)

    ... for extremely large values of 2.

    While you are making changes ....

  • by idji ( 984038 ) on Thursday June 18, 2015 @05:43PM (#49939775)
    You will have many horrible consequences.
    You have no idea what your code will produce.
    You will never know that your code actually works.
    As you spend a VAST amount of time debugging weird stuff, you will eventually realize it would be better if the program crashed when the error happened.

    Example from yesterday. A web-based graphing package had a field "Number of minor ticks per tick". I selected 0 and pressed ENTER. The webserver crashed with a division by zero error.
    If that software had FOOLISHLY decided to replace a division by zero with 0, then it would have gone into an infinite loop drawing the graph when it calculated the
    tickspacing to be zero...

    Example from today. I was configuring a call to a SOAP service, which was receiving an error from the Server "Division by Zero". It turned out that the error was that I had an HTTP header with no value.
    Division by Zero is an error TO BE IGNORED AT YOUR PERIL.
    Yes, both pieces of software had sloppy coding, but they both crashed, which helped me continue.
  • by sideslash ( 1865434 ) on Thursday June 18, 2015 @05:43PM (#49939777)
    "Can't I just be a programmer and not understand math?"
  • by g01d4 ( 888748 ) on Thursday June 18, 2015 @06:02PM (#49939999)
    If you're programming float then what's wrong w/the IEEE standard (assuming it's supported in your implementation)? From Wikipedia [wikipedia.org]

    The IEEE floating-point standard, supported by almost all modern floating-point units, specifies that every floating point arithmetic operation, including division by zero, has a well-defined result.

    Defining it to zero only makes sense where zero is also an error in the numerator, and/or zero is not a valid result in your problem domain.

  • by phantomfive ( 622387 ) on Thursday June 18, 2015 @06:11PM (#49940109) Journal
    Once a biologist, a mathematician, and a programmer were out riding in a Jeep on safari in Africa. Suddenly, there appeared, in a heard of zebras, a white zebra! This following conversation ensued:

    Biologist: "A white zebra! We discovered a new species! We'll be famous!"
    Mathematician: "Technically, we only know that it's white on one side."
    Programmer: "Oh no, a special case!"

    Programming is full of special cases. Part of being professional is learning to deal with it. There is no technology, no setting that will get rid of them for you.
  • by Hotawa Hawk-eye ( 976755 ) on Thursday June 18, 2015 @07:27PM (#49940801)

    There's a classic document appropriately titled What Every Computer Scientist Should Know About Floating-Point Arithmetic [oracle.com]. The "Special Quantities" section discusses plus and minus 0, denormal numbers, infinity, and NaN and offers some rationales for why those special values exist in IEEE floating-point arithmetic.

  • by jrumney ( 197329 ) on Thursday June 18, 2015 @08:04PM (#49941095)
    Indeed. And my programs would run a lot quicker if pi was 4. Does anyone really want pi to be anything other than 4?

Top Ten Things Overheard At The ANSI C Draft Committee Meetings: (5) All right, who's the wiseguy who stuck this trigraph stuff in here?

Working...