2011-09-10 06:58:09 8 Comments

Why is `X % 0`

an invalid expression?

I always thought `X % 0`

should equal X. Since you can't divide by zero, shouldn't the answer naturally be the remainder, X (everything left over)?

### Related Questions

#### Sponsored Content

#### 23 Answered Questions

### [SOLVED] Why can't variables be declared in a switch statement?

**2008-09-18 13:11:55****Rob****278893**View**949**Score**23**Answer- Tags: c++ switch-statement

#### 18 Answered Questions

### [SOLVED] Why should C++ programmers minimize use of 'new'?

**2011-06-28 00:08:14****bitgarden****130869**View**877**Score**18**Answer- Tags: c++ memory-management heap new-operator c++-faq

#### 5 Answered Questions

### [SOLVED] Undefined behavior and sequence points

**2010-11-14 05:37:46****Prasoon Saurav****104996**View**991**Score**5**Answer- Tags: c++ undefined-behavior c++-faq sequence-points

#### 17 Answered Questions

#### 8 Answered Questions

### [SOLVED] C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

**2011-06-11 23:30:14****Nawaz****226354**View**1910**Score**8**Answer- Tags: c++ multithreading c++11 language-lawyer memory-model

#### 10 Answered Questions

### [SOLVED] Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations with _mm_popcnt_u64 on Intel CPUs

**2014-08-01 10:33:29****gexicide****160071**View**1432**Score**10**Answer- Tags: c++ performance assembly x86 compiler-optimization

#### 24 Answered Questions

### [SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

**2012-04-16 04:23:16****Charles Menguy****191405**View**1665**Score**24**Answer- Tags: c++ algorithm image-processing opencv

#### 4 Answered Questions

### [SOLVED] Why doesn't this windowed expression result in a divide by zero error?

**2019-03-11 09:00:29****HoneyBadger****171**View**9**Score**4**Answer- Tags: sql sql-server window-functions divide-by-zero

## 7 comments

## @Mateen Ulhaq 2011-09-10 07:06:48

This answer is not for the mathematician. This answer attempts to give motivation (at the cost of mathematical precision).

Mathematicians:See here.Programmers:Remember that division by`0`

is`undefined`

. Therefore,`mod`

, which relies on division, is also`undefined`

.This represents division for positive

`X`

and`D`

; it's made up of the integral part and fractional part:Rearranging, you get:

Substituting

`0`

for`D`

:Since division by

`0`

is`undefined`

:## @Yatharth Agarwal 2013-11-29 05:52:00

Why can't you just substitute 0 for the outer D in the second equation making it

`(X % 0) = 0 * (w/e)`

and just call it zero?## @ollj 2017-05-29 14:01:30

you can evade the "divivion by 0" case of (A%B) for its type float identity mod(a,b) for float(B)=b=0.0 , that is undefined, or defined differently between any 2 implementations, to avoid logic errors (hard crashes) in favor of arithmetic errors...

by computing

`mod([a*b],[b])==b*(a-floor(a))`

INSTREAD OF

computing

`mod([a],[b])`

where [a*b]==your x-axis, over time [b] == the maximum of the seesaw curve (that will never be reached) == the first derivative of the seesaw function

https://www.shadertoy.com/view/MslfW8

## @Mac 2011-09-10 09:04:45

Another way that might be conceptually easy to understand the issue:

Ignoring for the moment the issue of argument sign,

`a % b`

could easily be re-written as`a - ((a / b) * b)`

. The expression`a / b`

is undefined if`b`

is zero, so in that case the overall expression must be too.In the end, modulus is effectively a divisive operation, so if

`a / b`

is undefined, it's not unreasonable to expect`a % b`

to be as well.## @Nawaz 2011-09-10 07:10:24

The C++ Standard(2003) says in §5.6/4,

That is, following expressions invoke undefined-behavior(UB):

Note also that

`-5 % 2`

is NOT equal to`-(5 % 2)`

(as Petar seems to suggest in his comment to his answer). It's implementation-defined. The spec says (§5.6/4),## @mu is too short 2011-09-10 07:51:45

I'd probably lean towards "

partlyimplementation defined", the sign is implementation defined but wouldn't the value be fixed once the sign is chosen? But that's just nit picking.## @Zaffy 2012-10-02 11:29:55

Is it possible to crash program using mod zero or just result is unknown ?

## @Nawaz 2012-10-02 11:46:19

@Zaffy: Since mod zero invokes undefined behavior (UB), so yes, it is

possibleto crash your program using mod zero, but it isnotguranteedto crash your program. Program crash is just one out of the million possibilities of UB.## @mafso 2014-08-16 17:33:45

This was linked as a duplicate, and I think this post should be made up-to-date: It's no longer implementation-defined, but

`a/b + a%b`

is`a`

for all`a`

and`b`

where the quotient is defined (the behaviour is undefined otherwise). The change happened in C99 and C++11 (maybe already in C++03 with TR1, don't know). Would be nice, if you could also tag the question as C, as they are the same in this respect (it was a C question which duplicated this).## @mafso 2014-08-16 17:39:15

And in case you wonder why C99 and C11 differ (not sure about C++), I remember this to be a defect in C99:

`INT_MIN % -1`

was defined, although it throws an exception on many platforms. In C11,`x%y`

is defined only if`x/y`

is, and it was never safe to assume`INT_MIN % -1`

to evaluate.## @Masood Lapeh 2019-02-24 14:12:50

My program crashed silently without any error message, and that was the problem.

## @Billy Moon 2011-09-10 07:21:05

I think because to get the remainder of

`X % 0`

you need to first calculate`X / 0`

which yields infinity, and trying to calculate the remainder of infinity is not really possible.However, the best solution in line with your thinking would be to do something like this

## @Petar Ivanov 2011-09-10 07:00:45

`X % D`

is bydefinitiona number`0 <= R < D`

, such that there exists`Q`

so thatSo if

`D = 0`

, no such number can exists (because`0 <= R < 0`

)## @mu is too short 2011-09-10 07:08:14

That's not true, AFAIK the sign of

`x % y`

is implementation defined if`x < 0`

.`-5 % 2`

happens to be -1 on my system.## @hammar 2011-09-10 07:11:16

`X = D*Q + R`

works forany`Q`

when`D = 0`

, with`X = R`

as the OP wanted. It's the`0 <= R < 0`

that's impossible to satisfy. Your answer seems to imply that it's the other way round, though I might just be reading it wrong.## @Nawaz 2011-09-10 07:38:24

@Petar: No.

`-5 % 2`

is NOT`-(5 % 2)`

in fact. It's implementation-defined. The spec says,If both operands are nonnegative then the remainder is nonnegative;if not, the sign of the remainder is implementation-defined## @phuclv 2013-10-29 01:11:21

The above is true for mathematics modulus. But CPUs and C compiler implementations often return R that have the same sign as X, so -5 % 2 = -(5 % 2) is true. OTOH Python will return the "true" mathematics modulus so the above wouldn't be true anymore stackoverflow.com/questions/1907565/…

## @K-ballo 2011-09-10 07:00:04

X % Y gives a result in the integer [ 0, Y ) range. X % 0 would have to give a result greater or equal to zero, and less than zero.

## @mu is too short 2011-09-10 07:07:33

That's not true, AFAIK the sign of

`x % y`

is implementation defined if`x < 0`

.`-5 % 2`

happens to be -1 on my system.## @K-ballo 2011-09-10 07:18:48

True you, the quirks of mod. But alas it is enough to illustrate why one cannot mod by zero.