Basically, I need (-3) % 5
to be "2" instead of "-3". Python produces "2", but C++ produces "-3". Not sure how to produce "2" in C++. Thanks!
基本上,我需要(-3)%5为“2”而不是“-3”。 Python产生“2”,但C ++产生“-3”。不知道如何在C ++中生成“2”。谢谢!
4 个解决方案
#1
17
((x % 5) + 5) % 5
but I'm sure there's a more elegant way.
((x%5)+ 5)%5但我确信这是一种更优雅的方式。
#2
6
Add the base if the input number X
is negative:
如果输入数字X为负,则添加基数:
X % Y + (X % Y < 0 ? Y : 0);
#3
1
You can add some multiple of 5
to the negative number first, to convert it to a positive number with the same value mod 5.
您可以先将负数加5的倍数,然后将其转换为具有相同值mod 5的正数。
You can do that by taking the absolute of the negative number, adding whatever is needed to round it up to the next multiple of 5, and then add it to your negative number, and that should already be a number between 0 and 4.
您可以通过取负数的绝对值,添加所需的任何内容将其四舍五入到下一个5的倍数,然后将其添加到负数中,并且该数字应该已经是0到4之间的数字。
Alternatively, simply do something like:
或者,只需执行以下操作:
num = -2;
mod = 5;
if ( num < 0 ) {
result = mod - (abs(num) % mod);
}
and it'll work (explanation: mathemagic)
它会工作(解释:mathemagic)
#4
1
The quick & dirty way is to write
快速而肮脏的方式是写
((x % divisor) + divisor) % divisor
For example, ((-3 % 5) + 5) % 5
. However this performs two separate divisions, and since divisions are one of the slowest arithmetic operations you might like one of these alternatives:
例如,(( - 3%5)+ 5)%5。但是,这会执行两个单独的分区,并且由于分区是最慢的算术运算之一,您可能会喜欢以下备选方案之一:
(1) General mod
for integer or floating point
(1)整数或浮点的通用mod
int mod(int x, int divisor)
{
int m = x % divisor;
return m + (m < 0 ? divisor : 0);
}
template<class Num> Num mod(Num x, Num divisor)
{
Num m = x % divisor;
return m + (m < 0 ? divisor : 0);
}
(2) Non-branching mod
for 32-bit integers
(2)32位整数的非分支mod
int mod(int x, int divisor)
{
int m = x % divisor;
return m + ((m >> 31) & divisor);
}
All this assumes that the divisor
is always positive.
所有这些都假定除数总是正的。
#1
17
((x % 5) + 5) % 5
but I'm sure there's a more elegant way.
((x%5)+ 5)%5但我确信这是一种更优雅的方式。
#2
6
Add the base if the input number X
is negative:
如果输入数字X为负,则添加基数:
X % Y + (X % Y < 0 ? Y : 0);
#3
1
You can add some multiple of 5
to the negative number first, to convert it to a positive number with the same value mod 5.
您可以先将负数加5的倍数,然后将其转换为具有相同值mod 5的正数。
You can do that by taking the absolute of the negative number, adding whatever is needed to round it up to the next multiple of 5, and then add it to your negative number, and that should already be a number between 0 and 4.
您可以通过取负数的绝对值,添加所需的任何内容将其四舍五入到下一个5的倍数,然后将其添加到负数中,并且该数字应该已经是0到4之间的数字。
Alternatively, simply do something like:
或者,只需执行以下操作:
num = -2;
mod = 5;
if ( num < 0 ) {
result = mod - (abs(num) % mod);
}
and it'll work (explanation: mathemagic)
它会工作(解释:mathemagic)
#4
1
The quick & dirty way is to write
快速而肮脏的方式是写
((x % divisor) + divisor) % divisor
For example, ((-3 % 5) + 5) % 5
. However this performs two separate divisions, and since divisions are one of the slowest arithmetic operations you might like one of these alternatives:
例如,(( - 3%5)+ 5)%5。但是,这会执行两个单独的分区,并且由于分区是最慢的算术运算之一,您可能会喜欢以下备选方案之一:
(1) General mod
for integer or floating point
(1)整数或浮点的通用mod
int mod(int x, int divisor)
{
int m = x % divisor;
return m + (m < 0 ? divisor : 0);
}
template<class Num> Num mod(Num x, Num divisor)
{
Num m = x % divisor;
return m + (m < 0 ? divisor : 0);
}
(2) Non-branching mod
for 32-bit integers
(2)32位整数的非分支mod
int mod(int x, int divisor)
{
int m = x % divisor;
return m + ((m >> 31) & divisor);
}
All this assumes that the divisor
is always positive.
所有这些都假定除数总是正的。