如何在C ++中使用模数?

时间:2022-09-01 14:27:45

How do I perform a mod operation between two integers in C++?

如何在C ++中的两个整数之间执行mod操作?

6 个解决方案

#1


10  

Like this: x=y%z

像这样:x = y%z

#2


15  

In c++, use % operator

在c ++中,使用%operator

More Help

#3


10  

As the other answers have stated, you can use the C++ % operator. But be aware that there's a wrinkle no one has mentioned yet: in the expression a % b, what if a is negative? Should the result of this operation be positive or negative? The C++ standard leaves this up to the implementation. So if you want to handle negative inputs portably, you should probably do something like r = abs(a) % b, then fix up the sign of r to match your requirements.

正如其他答案所述,您可以使用C ++%运算符。但请注意,还没有人提到过的皱纹:在表达式a%b中,如果a是负面的怎么办?这项行动的结果应该是积极的还是消极的? C ++标准将此归结为实现。因此,如果您想要便携地处理负输入,您应该执行类似r = abs(a)%b的操作,然后修复r的符号以符合您的要求。

#4


9  

C++ has the % operator, occasionally and misleadingly named "the modulus" operator. In particular the STL has the modulus<> functor in the <functional> header. That's not the mathematical modulus operator, mind you, because in modulus arithmetics a mod b by definition evaluates to a non-negative value for any value of a and any positive value of b. In C++ the sign of the result of a % b is implementation-defined if either of the arguments is negative. So, we would more appropriately name the % operator the remainder operator.

C ++有%运算符,偶尔会误导性地命名为“模数”运算符。特别是STL在 头中具有模数<>仿函数。请注意,这不是数学模运算符,因为在模数算术中,根据定义,mod b对任何a值和b的任何正值都评估为非负值。在C ++中,如果任一参数为负,则%b的结果符号是实现定义的。因此,我们更适当地将%运算符命名为余数运算符。

That said, if you truly want the mathematical modulus operator then you can define a function to do just that:

也就是说,如果你真的想要数学模数运算符,那么你可以定义一个函数来做到这一点:

template<typename V>
V mod(const V& a, const V& b)
{
    return (a % b + b) % b;
}

So long as b is a positive value a call to the above function will yield a non-negative result.

只要b是正值,对上述函数的调用将产生非负结果。

#5


2  

Using the modulus % operator :

使用模数%运算符:

int modulus_a_b = a % b;

#6


0  

if you use double variable, you should use;

如果你使用双变量,你应该使用;

double x;
double y;
double result = fmod(x, y);

#1


10  

Like this: x=y%z

像这样:x = y%z

#2


15  

In c++, use % operator

在c ++中,使用%operator

More Help

#3


10  

As the other answers have stated, you can use the C++ % operator. But be aware that there's a wrinkle no one has mentioned yet: in the expression a % b, what if a is negative? Should the result of this operation be positive or negative? The C++ standard leaves this up to the implementation. So if you want to handle negative inputs portably, you should probably do something like r = abs(a) % b, then fix up the sign of r to match your requirements.

正如其他答案所述,您可以使用C ++%运算符。但请注意,还没有人提到过的皱纹:在表达式a%b中,如果a是负面的怎么办?这项行动的结果应该是积极的还是消极的? C ++标准将此归结为实现。因此,如果您想要便携地处理负输入,您应该执行类似r = abs(a)%b的操作,然后修复r的符号以符合您的要求。

#4


9  

C++ has the % operator, occasionally and misleadingly named "the modulus" operator. In particular the STL has the modulus<> functor in the <functional> header. That's not the mathematical modulus operator, mind you, because in modulus arithmetics a mod b by definition evaluates to a non-negative value for any value of a and any positive value of b. In C++ the sign of the result of a % b is implementation-defined if either of the arguments is negative. So, we would more appropriately name the % operator the remainder operator.

C ++有%运算符,偶尔会误导性地命名为“模数”运算符。特别是STL在 头中具有模数<>仿函数。请注意,这不是数学模运算符,因为在模数算术中,根据定义,mod b对任何a值和b的任何正值都评估为非负值。在C ++中,如果任一参数为负,则%b的结果符号是实现定义的。因此,我们更适当地将%运算符命名为余数运算符。

That said, if you truly want the mathematical modulus operator then you can define a function to do just that:

也就是说,如果你真的想要数学模数运算符,那么你可以定义一个函数来做到这一点:

template<typename V>
V mod(const V& a, const V& b)
{
    return (a % b + b) % b;
}

So long as b is a positive value a call to the above function will yield a non-negative result.

只要b是正值,对上述函数的调用将产生非负结果。

#5


2  

Using the modulus % operator :

使用模数%运算符:

int modulus_a_b = a % b;

#6


0  

if you use double variable, you should use;

如果你使用双变量,你应该使用;

double x;
double y;
double result = fmod(x, y);