Can't install Zero?

Why is X % 0 invalid expression?

I always thought that X % 0 should be equal to X. Since you cannot divide by zero, should the answer naturally remain, X (all remains)?

+45
c ++ divide-by-zero modulo
Sep 10 2018-11-11T00:
source share
7 answers

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

[...] If the second operand / or% is zero, the behavior is undefined ; [...]

That is, the following expressions refer to undefined -behavior (UB):

 X / 0; //UB X % 0; //UB 



We also note that -5 % 2 NOT equal to -(5 % 2) (as Petar seems to suggest in his commentary on his answer). This is determined by the implementation. The spectrum says (Β§5.6 / 4)

[...] If both operands are non-negative, then the remainder is non-negative; if not, the remainder sign is determined by the implementation .

+29
Sep 10 '11 at 7:10
source share

I want to see this one .

Since division by 0 is undefined , the mod that relies on division is also undefined .




This is a division; It consists of an integral part and the remainder:

 (X / D) = floor(X / D) + (X % D) / D 

Regrouping, you will receive:

 (X % D) / D = (X / D) - floor(X / D) (X % D) = D * ((X / D) - floor(X / D)) 

Substituting 0 for D :

 (X % D) = D * ((X / 0) - floor(X / 0)) 

Since dividing by 0 is undefined :

 (X % D) = D * (undefined - floor(undefined)) (X % D) = D * (undefined) (X % D) = undefined 
+8
Sep 10 2018-11-11T00:
source share

X % D has the definition of the number 0 <= R < D , such that Q exists, so that

 X = D*Q + R 

So, if D = 0 , such a number does not exist (because 0 <= R < 0 )

+4
Sep 10 '11 at 7:00
source share

I think because to get the rest of X % 0 you first need to calculate X / 0 , which gives infinity, and trying to calculate the rest of infinity is actually impossible.

However, the best decision according to your thinking would be to do something like this

 REMAIN = Y ? X % Y : X 
+2
Sep 10 '11 at 7:21
source share

Another way that could conceptually easily understand the problem:

Ignoring the question of the sign of the argument at the moment, a % b can easily be rewritten as a - ((a / b) * b) . The expression a / b is undefined if b is zero, so in this case the general expression should be too much.

After all, a module is effectively a dividing operation, so if a / b is undefined, it is not safe to expect a % b too.

+2
Sep 10 2018-11-11T00:
source share

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

+1
Sep 10 '11 at 7:00
source share

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

by computing mod([a*b],[b])==b*(a-floor(a))
TOOL
calculation mod([a],[b])

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

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

0
May 29 '17 at 14:01
source share



All Articles