我想知道什么时候该投。在c++中,当进行加法、乘法等操作时,隐式类型转换规则是什么?例如,

int + float = ?
int * float = ?
float * int = ?
int / float = ?
float / int = ?
int / int = ?
int ^ float = ?

等等...

表达式是否总是计算为更精确的类型?Java的规则不同吗? 如果我在这个问题上用词不准确,请指正。


当前回答

涉及浮点数的算术运算结果为浮点数。

int + float = float
int * float = float
float * int = float
int / float = float
float / int = float
int / int = int

更多细节请回答。看看c++标准的§5/9节是怎么说的

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 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.54) — Then, if either operand is unsigned long the other shall be converted to unsigned long. — Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int. — Otherwise, if either operand is long, the other shall be converted to long. — Otherwise, if either operand is unsigned, the other shall be converted to unsigned. [Note: otherwise, the only remaining case is that both operands are int ]

其他回答

在c++中,操作符(用于POD类型)总是作用于相同类型的对象。 因此,如果它们不相同,其中一个将被提升到与另一个匹配。 操作结果的类型与操作数(转换后)相同。

if:
either is      long double       other is promoted >      long double
either is           double       other is promoted >           double
either is           float        other is promoted >           float
either is long long unsigned int other is promoted > long long unsigned int
either is long long          int other is promoted > long long          int
either is long      unsigned int other is promoted > long      unsigned int
either is long               int other is promoted > long               int
either is           unsigned int other is promoted >           unsigned int
either is                    int other is promoted >                    int

Otherwise:
both operands are promoted to int

请注意。操作的最小大小是int。因此,short/char在操作完成之前被提升为int。

在所有表达式中,int在执行操作之前被提升为浮点数。操作的结果是一个浮点数。

int + float =>  float + float = float
int * float =>  float * float = float
float * int =>  float * float = float
int / float =>  float / float = float
float / int =>  float / float = float
int / int                     = int
int ^ float =>  <compiler error>

整个第4章都在讲转换,但我认为你应该对这些最感兴趣:

4.5 Integral promotions [conv.prom] An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int if int can represent all the values of the source type; other- wise, the source rvalue can be converted to an rvalue of type unsigned int. An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) can be converted to an rvalue of the first of the following types that can represent all the values of its underlying type: int, unsigned int, long, or unsigned long. An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can rep- resent all the values of the bit-field. If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes. An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one. These conversions are called integral promotions.

4.6浮点提升 (conv.fpprom) float类型的右值可以转换为double类型的右值。该值不变。 这种转换称为浮点提升。

因此,所有涉及浮点数的转换结果都是浮点数。

只有一个包含两个int -结果是int: Int / Int = Int

表达式的类型,当不是两个部分是相同类型时,将转换为两者中最大的。这里的问题是理解哪一个比另一个大(它与字节大小无关)。

在包含实数和整数的表达式中,整数将升格为实数。例如,在int + float中,表达式的类型是float。

另一个区别与类型的能力有关。例如,包含int型和long int型的表达式的结果为long int型。

这个答案在很大程度上是针对@ rafajdowgird的评论:

操作的最小大小是int。-这太奇怪了 (那么有效支持char/short的架构呢 操作?)这真的在c++规范中吗?

请记住,c++标准有非常重要的“as-if”规则。参见第1.8节:程序执行:

3)这一规定有时被称为“假设”规则,因为a 执行可以自由地忽略标准的任何要求 只要结果是满足了要求,就可以 这可以从程序的可观察行为中确定。

编译器不能将int值设置为8位,即使它是最快的,因为标准要求最小int值为16位。

因此,在具有超快8位操作的理论计算机的情况下,将算术隐式提升为int可能很重要。然而,对于许多操作,您无法判断编译器是否确实以int精度执行了操作,然后将其转换为char类型存储在您的变量中,或者这些操作是否始终以char类型完成。

For example, consider unsigned char = unsigned char + unsigned char + unsigned char, where addition would overflow (let's assume a value of 200 for each). If you promoted to int, you would get 600, which would then be implicitly down cast into an unsigned char, which would wrap modulo 256, thus giving a final result of 88. If you did no such promotions,you'd have to wrap between the first two additions, which would reduce the problem from 200 + 200 + 200 to 144 + 200, which is 344, which reduces to 88. In other words, the program does not know the difference, so the compiler is free to ignore the mandate to perform intermediate operations in int if the operands have a lower ranking than int.

这在一般的加法、减法和乘法中都是成立的。对于除法和模量来说,一般不是这样的。

警告!

转换从左到右进行。

试试这个:

int i = 3, j = 2;
double k = 33;
cout << k * j / i << endl; // prints 22
cout << j / i * k << endl; // prints 0