4 ms·
>Zero is a fine answer for integer division by zero. It's not. Division (edit: of a positive integer) by zero is better approximated by +infinity rather than
by nvy 2y ago
>Zero is a fine answer for integer division by zero.
It's not. Division (edit: of a positive integer) by zero is better approximated by +infinity rather than zero.
In real life to divide something into zero groups is nonsensical, thus the number system in programs should reflect this. In the exceptionally-rare case where you want to divide by zero and it makes sense (can't think of a scenario where that's true but let's stipulate), then you can use your language's equivalent of try/catch or (if (zerop x) ...) to get around it.
Don't fuck up normal mathematics for the rest of us just because some lazy programmer doesn't like to add error checking.
- foobar1962 2y ago> Division by zero is better approximated by +infinity rather than zero. No it's not. Division by zero is UNDEFINED. How does a calculation return +infinity anyway? On the positive side of the graph, 1/x approaches +infinity. However from the negative side of the graph 1/x approaches -infinity. So at zero, 1/x is simultaneously +infinity and -infinity, which is not possible. The answer to 1/0 is "there is no answer" which is UNDEFINED. A reasonable result for a calculation is to return "?" or possibly NULL or nothing, depending on what other parts of the system are expecting.
- vlovich123 2y ago> No it's not. Division by zero is UNDEFINED Yeah OP is suggesting that the answer to lim x-> 0 1/x = infinity and thus that’s an intuitive answer. Unfortunately that’s obviously not correct precisely because it depends which side from 0 you approach and why just 1/0 is undefined (you could argue about 0+ and 0- but I view that more as IEEE754 weirdness that is used in niches rather than something that would meaningfully change the situation) > How does a calculation return +infinity anyway? Not sure what your actually asking but floating point representations generally support a concept of +/- inf. > A reasonable result for a calculation is to return "?" or possibly NULL or nothing, depending on what other parts of the system are expecting. That’s what floating point NaN is
- nvy 2y ago>Division by zero is UNDEFINED. How does a calculation return +infinity anyway? I used the word "approximated" for a reason. I'll cop to the fact that I was only considering positive integers but your response is needlessly pedantic given the context of what I wrote.
- nullc 2y agoWhen performing integer "division" of x by y, you're finding a solution {q,r} to the equation y * q = x - r. When y=0 any choice of q works, and using 0,x is a perfectly reasonable and intuitive way of defining things. Computer integers aren't the real numbers you learned about in gradeschool. INT_MAX+1 is not greater than INT_MAX. :) x/0 = program explodes is also a justifiable choice. It is not however more or less fundamentally correct than making the result 0. Floating point division by zero doesn't (typically) crash programs the way integer division by zero does (it typically returns a NaN-- and the programmer is free to turn nans to zeros if they like :)).
- lmm 2y ago> When performing integer "division" of x by y, you're finding a solution {q,r} to the equation y * q = x - r. When y=0 any choice of q works, and using 0,x is a perfectly reasonable and intuitive way of defining things. Nope. You forgot that there's a requirement that r < q. Otherwise I could say e.g. 5/2 = 1, which is certainly no less valid than saying that 5/0 = 0, but not something that anyone sane wants.
- nullc 2y agoI think not, because in the y=0 case the only sensible r is x, because remove the mod 0 subset you have the unmodified set of integers itself-- the remainder can only sensibly be an identity in that case.
- lmm 2y ago> in the y=0 case the only sensible r is x No, x is an utterly non-sensible value of r. > remove the mod 0 subset you have the unmodified set of integers itself-- the remainder can only sensibly be an identity in that case. What are you talking about? There are no possible remainders mod 0 (just as there is one possible remainder mod 1 and there are 2 possible remainders mod 2) and there is no sensible definition of the remainder function; defining it to be identity is no less silly than defining it to be, IDK, 7.
- nvy 2y ago