具有负操作数的C ++ size_t模运算 [英] C++ size_t modulus operation with negative operand

查看:115
本文介绍了具有负操作数的C ++ size_t模运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,模运算可以为您提供三个值:

然后:

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

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

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

另一个示例:

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

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

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

当左手操作数为正时,这三种方法之间的答案都是相同的.但是对于负值,它们似乎都有自己的方法.如何在C ++中计算无符号操作数上的模运算值?

解决方案

当您混合带符号和无符号值时会发生这种情况.混乱!

[C++14: 5.6/2]: */的操作数应具有算术或无作用域枚举类型. %的操作数应具有整数或无作用域的枚举类型.通常对操作数执行算术转换,并确定结果的类型.

现在,请参见下面的粗体部分(假设您的size_t与您的int具有相同的排名;这始终是正确的):

[C++14: 5/10]:许多期望算术或枚举类型的操作数的二进制运算符都以类似的方式导致转换和产生结果类型.目的是产生一个通用类型,它也是结果的类型. 这种模式称为常规算术转换,其定义如下:

  • 如果任何一个操作数都属于范围枚举类型(7.2),则不执行任何转换;如果另一个操作数的类型不同,则表达式格式错误.
  • 如果其中一个操作数的类型为long double,则另一个应转换为long double.
  • 否则,如果其中一个操作数为double,则另一个应转换为double.
  • 否则,如果其中一个操作数为float,则另一个应转换为float.
  • 否则,应在两个操作数上执行积分提升(4.5).61然后执行以下操作 规则应适用于提升后的操作数:
    • 如果两个操作数具有相同的类型,则无需进一步转换.
    • 否则,如果两个操作数都具有符号整数类型或都具有无符号整数类型,则将具有较小整数转换等级的操作数转换为具有较高等级的操作数的类型.
    • 否则,如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩,则应将具有符号整数类型的操作数转换为具有无符号整数的操作数的类型整数类型.
    • 否则,如果带符号整数类型的操作数的类型可以表示无符号整数类型的操作数的所有值,则应将无符号整数类型的操作数转换为带符号整数的操作数的类型类型.
    • 否则,两个操作数都应转换为与带符号整数类型的操作数类型相对应的无符号整数类型.

简而言之,您的-7正在变为std::numeric_limit<size_t>::max() + 1 - 7(无论平台上是什么),并且正在对该值执行计算.实际上,在 my 平台上的证实了1 的结果.

So there are three values that a modulus operation can give you:

Then:

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

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

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

Another example:

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

-7 % 4 = -3 (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++?

解决方案

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; 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):

[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. 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; 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.
  • Otherwise, if either operand is double, the other shall be converted to double.
  • Otherwise, if either operand is float, the other shall be converted to 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:
    • 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. Indeed, on my platform, that confirms the result of 1.

这篇关于具有负操作数的C ++ size_t模运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆