6

Consider integer division

a = bq + r

where a, b, q, r are respectively: dividend, divisor, quotient, and remainder. Particularly when b = 0, there is no unique q that satisfies the equation for a given a, and hence it makes sense that the quotient q should be undefined in such case.

However, there is indeed a unique r in such case, namely, r = a. Under the premise that the quotient and the remainder are always defined together, it would follow that r is not defined whenever q is undefined, but in programming, we often want to use the remainder operation % irrespective of division /. I actually came across a situation where I want if b == 0 then a else a % b end.

Is there/Was there an operator in any programming language such that it is the same as % but returns the dividend instead of a zero division error when the divisor is 0?

Is there any reason that most (or all) programing languages return a zero division error for % 0?

sawa
  • 165,429
  • 45
  • 277
  • 381
  • 3
    You know, I was wondering earlier on today if Chuck Norris could take the logarithm of zero... – Andrew Grimm Apr 08 '11 at 15:52
  • Interesting question. Scheme (or guile, anyway) has both a `remainder` and a `modulo` function (they differ when the args are negative, btw). Both of them overflow on 0. – drysdam Apr 08 '11 at 15:53
  • "Divident" should be, "dividend". Also, I think you mean to have the divisor be 0, not the quotient. The quotient is the result of division, and is zero whenever the dividend is less than the divisior (for unsigned numbers). In this case, the remainder **will** be equal to the dividend. When the divisor is zero, the quotient is undefined, and it is not clear that multiplying an undefined number by zero **is** zero, so the remainder should also be undefined. – pat Apr 08 '11 at 15:59
  • Now you need to say that there's no unique 'q' that satisfies the equation. – pat Apr 08 '11 at 16:05
  • Well, I said it is not clear about zero multiplied by an undefined value. The real argument is that r has to be less than b according to the definition of division. – pat Apr 08 '11 at 16:07
  • @pat That is my question. Even when there is no unique 'q', there is unique 'r', and can't we define the latter irrespective of the uniqueness of the former? Okay, your later comment is more convincing. – sawa Apr 08 '11 at 16:09
  • @Grimm I understand that log0 should be undefined just as /0 is. But I though %0 could be independent of these. – sawa Apr 08 '11 at 16:15
  • I really don't think it can be claimed that 0 multiplied by anything is zero see [here](http://answers.yahoo.com/question/index?qid=20110214194804AA2tzEk). However, side-stepping that argument, a quotient and a remainder have to satisfy two conditions; a = bq + r **and** 0 <= r < b. Setting b = 0, r = a satisfies the first condition (again, ignoring the fact that q is undefined), but not the second. – pat Apr 08 '11 at 16:15
  • @pat Thanks. Your comment is just as convincing as user677480's. (I still think zero times any number is zero, but I wrote something wrong in my earlier comment, so I removed it.) – sawa Apr 08 '11 at 16:46
  • It is usually the case that the magnitude of the remainder of a division is always smaller than the magnitude of the divisor (see e.g. [this plot](http://www.wolframalpha.com/input/?i=1%25x)). Defining the remainder as the divident when the divisor is zero would break that rule. – HelloGoodbye Sep 09 '15 at 20:34
  • @AndrewGrimm I'm sending this message from 2017 back six years to let you know Chuck Norris isn't that cool anymore. Next year (2012) he'll claim that if Obama is reelected the US will plunge into 1,000 years of darkness (none of us are sure if he meant that as some kind of a euphemism). Obama did win, so we're currently in year six of that. (That's how this works, right? He'll see my reply back in 2011?) – Trixie Wolf Jan 04 '17 at 04:10
  • An implementation of the division algorithm that doesn't check for /0 actually returns what you write the dividend is the remainder in that case. – Calmarius Mar 02 '18 at 17:50

2 Answers2

3

Mathematically, the remainder is between 0 and b-1, where b is the divisor. Therefore, when b = 0, r is undefined since it has to be >= 0.

pjwilliams
  • 300
  • 2
  • 10
  • Ah yes, that's the crucial missing statement, 0 <= r < b. Without a constraint on r, there would be an infinite number of quotients. – pat Apr 08 '11 at 16:04
  • @user677480, @pat Thanks for the answer. I was my carelessness to have forgotten that condition. Now it's clear. – sawa Apr 08 '11 at 16:26
2

Is there any programming language that returns the dividend? Not sure. I've never come across any.

Is there a reason that most don't return the dividend? Yes. Modulus is a common operation in CS because it is a byproduct of integer division on a CPU. Most (if not all) assembly languages have a modulus operation, and this operation uses the exact same hardware as the division operation. Thus if you can't divide by zero in hardware, then you can't do modulus zero in hardware.

Does this mean that you can't have a language that supports this? Not really, but you would have to add an if-statement to an operation that is usually a single instruction. This would probably result in a pretty heavy performance hit, so few (if any) do it.

riwalk
  • 14,033
  • 6
  • 51
  • 68
  • Thanks for the answer. The problem of having it implemented became clear enough. – sawa Apr 08 '11 at 16:25
  • The modulus/remainder operation is most often used for checking if some number x is a multiple of some number y. What is of interest in such cases is not the exact value returned, but whether or not it is zero. Zero is a multiple of zero (the only multiple of zero) and applying a definition of division such that a/0 yields a quotient of zero and a remainder of a would let the normal method of testing "x is a multiple of y" work smoothly in all cases including zero. – supercat Nov 13 '13 at 22:10
  • @supercat, nobody is trying to smooth over division by zero. If you need to handle division by zero in a special way, use an if statement. Modulus operates the way it does because it is efficient. If you need it to do more, then there are plenty of programming constructs that will get you there. – riwalk Nov 14 '13 at 00:25
  • @Stargazer712: I understand that many hardware vendors have decided that it was better to have divide-by-zero trap than simply do nothing but set a flag, and as a consequence of this allowing any sort of predictable behavior in an integer-divide-by-zero scenario would be more expensive than leaving it UB (the relative cost would probably be less than that of requiring that even when y is constant, x/y must to truncate toward zero and x%y must match the sign of x, but there would be a cost). My point was simply that `x mod 0` is not a meaningless concept, though if I were designing... – supercat Nov 14 '13 at 16:08
  • ...a programming language I'd probably limit mod-zero testing to an "is multiple of" operator. When testing if x is a multiple of some arbitrary y, the cost if a zero check could be made up for by not having to adjust the value of x%y if x is negative. – supercat Nov 14 '13 at 16:11
  • I am designing a new microprocessor named ForwardCom. I want to avoid "undefined" and ambiguous results. I have decided that 1/0 gives MAX_INT. If you need to calculate r=a%b after calculating q=a/b, then you may save time by calculating r=a-q*b. This gives r=a. I am still in doubt whether a%0 should return 0 or a. Any suggestions? – A Fog Nov 27 '22 at 14:02