简体   繁体   English

具有负操作数的C ++ size_t模运算

[英]C++ size_t modulus operation with negative operand

So there are three values that a modulus operation can give you: 因此,模运算可以为您提供三个值:

Then: 然后:

-7 % 5 = 3 (math, remainder >= 0) -7%5 = 3(数学,余数> = 0)

-7 % 5 = -2 (C++) -7%5 = -2(C ++)

-7 % (size_t)5 = 4 (C++) -7%(size_t)5 = 4(C ++)

Another example: 另一个例子:

-7 % 4 = 1 (math, remainder >= 0) -7%4 = 1(数学,余数> = 0)

-7 % 4 = -3 (C++) -7%4 = -3(C ++)

-7 % (size_t)4 = 1 (C++) -7%(size_t)4 = 1(C ++)

When the left hand operand is positive, the answer between all three methods are the same. 当左操作数为正时,这三种方法之间的答案都是相同的。 But for negative values they all seem to have their own methods. 但是对于负值,它们似乎都有自己的方法。 How is the value of modulus operations on unsigned operands calculated in C++? 在C ++中如何计算无符号操作数上的模运算值?

This is what happens when you mix signed and unsigned values — confusion! 当您混合带符号和无符号的值时,会发生这种情况-混乱!

[C++14: 5.6/2]: The operands of * and / shall have arithmetic or unscoped enumeration type; [C++14: 5.6/2]: */的操作数应为算术或无作用域枚举类型; the operands of % shall have integral or unscoped enumeration type. %的操作数应具有整数或无作用域的枚举类型。 The usual arithmetic conversions are performed on the operands and determine the type of the result. 通常对操作数执行算术转换,并确定结果的类型。

Now, see the bolded passage below (which assumes your size_t has the same rank as your int ; this is always true): 现在,请参见下面的粗体部分(假定您的size_t与您的int具有相同的等级;这始终是正确的):

[C++14: 5/10]: Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. [C++14: 5/10]:许多期望算术或枚举类型的操作数的二进制运算符都以类似的方式导致转换并产生结果类型。 The purpose is to yield a common type, which is also the type of the result. 目的是产生一个通用类型,它也是结果的类型。 This pattern is called the usual arithmetic conversions , which are defined as follows: 这种模式称为通常的算术转换 ,其定义如下:

  • If either operand is of scoped enumeration type (7.2), no conversions are performed; 如果两个操作数都属于范围枚举类型(7.2),则不执行任何转换。 if the other operand does not have the same type, the expression is ill-formed. 如果另一个操作数不具有相同的类型,则表达式格式错误。
  • If either operand is of type long double, the other shall be converted to long double. 如果一个操作数的类型为long double,则另一个应转换为long double。
  • Otherwise, if either operand is double, the other shall be converted to double. 否则,如果其中一个操作数为double,则另一个应转换为double。
  • Otherwise, if either operand is float, the other shall be converted to float. 否则,如果其中一个操作数为float,则另一个应转换为float。
  • Otherwise, the integral promotions (4.5) shall be performed on both operands.61 Then the following rules shall be applied to the promoted operands: 否则,应在两个操作数上执行整数提升(4.5)。61然后,以下规则应应用于提升的操作数:
    • If both operands have the same type, no further conversion is needed. 如果两个操作数具有相同的类型,则无需进一步转换。
    • Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank. 否则,如果两个操作数都具有符号整数类型或都具有无符号整数类型,则整数转换等级较小的操作数应转换为等级较大的操作数的类型。
    • Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type. 否则, 如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩,则带符号整数类型的操作数应转换为无符号整数类型的操作数的类型。
    • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type. 否则,如果带符号整数类型的操作数的类型可以表示无符号整数类型的操作数的所有值,则应将无符号整数类型的操作数转换为带符号整数类型的操作数的类型。
    • Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type. 否则,两个操作数均应转换为与带符号整数类型的操作数类型相对应的无符号整数类型。

In short, your -7 is becoming std::numeric_limit<size_t>::max() + 1 - 7 (whatever that is on your platform), and the calculation is being performed on that value. 简而言之,您的-7变为std::numeric_limit<size_t>::max() + 1 - 7 (无论平台上是什么),并且正在对该值执行计算。 Indeed, on my platform, that confirms the result of 1 . 确实, 我的平台上,这证实了1的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM