I have seen many programmers who write code with ((a + b) % d + d) % din C ++. Why don't they just use it (a + b) % d? What is + din parentheses? Is this due to negative numbers?
((a + b) % d + d) % d
(a + b) % d
+ d
thank
Yes you are right. Prior to C ++ 11, the behavior of the residual operator %for negative arguments was left to implement, subject to some restrictions. And adding dto the left argument can help in that as long as the other members of this argument are summed more or equal -d, which, in general, is not so. ( -a / dmultiples of dnegative awould be the best additive constant in your particular case.)
%
d
-d
-a / d
a
, - . . , b , b % d . d, d .
b
b % d
Java, :
int a = 13; int b = -23; int d = 31; int result1 = (a + b % d + d) % d; int result2 = (a + b % d) % d; System.out.println(result1); System.out.println(result2);
:
21 -10
, . Compettitve (a+b)%b a%b, a - .
(a+b)%b
a%b
a%b = (a+b)%b (a+b)%b = a%b + b%b = a%b + 0 = a%b
a=-2 b=5, a%b = 3
a=-2
b=5
a%b = 3
Source: https://habr.com/ru/post/1681280/More articles:How to move back and forth in one word in the built-in local terminal - webstormIs there a way to assign multiple xlabels at once in matplotlib? - pythonMapKit 'style' parameter in the request - parametersHow can I trim my code? - javaTextView autoSizeTextType not working in application - androidDynamic Dropdown Country Laravel - databaseIn what situations does creating a new instance of a component respond? - reactjsHow can I give today a date as the default value for a parameter in powershell - parametersPowerShell Split string into variables - powershellOperation name must be unique in eclipse - javaAll Articles