我想知道什么时候该投。在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 ]

其他回答

我对这个问题的解决方案得到了WA(错误的答案),然后我将int中的一个改为long long int,它给出了AC(接受)。之前,我试图做long long int += int * int,在我将其纠正为long long int += long long int * int后。我在谷歌上搜了一下,

1. 算术转换

类型转换条件:

条件满足—>转换

Either operand is of type long double. ---> Other operand is converted to type long double. Preceding condition not met and either operand is of type double. ---> Other operand is converted to type double. Preceding conditions not met and either operand is of type float. ---> Other operand is converted to type float. Preceding conditions not met (none of the operands are of floating types). ---> Integral promotions are performed on the operands as follows: If either operand is of type unsigned long, the other operand is converted to type unsigned long. If preceding condition not met, and if either operand is of type long and the other of type unsigned int, both operands are converted to type unsigned long. If the preceding two conditions are not met, and if either operand is of type long, t he other operand is converted to type long. If the preceding three conditions are not met, and if either operand is of type unsigned int, the other operand is converted to type unsigned int. If none of the preceding conditions are met, both operands are converted to type int.

2。整数转换规则

整数的促销活动:

小于int的整数类型在对其执行操作时被提升。如果原始类型的所有值都可以表示为int型,则较小类型的值转换为int型;否则,它将被转换为无符号整型。整数提升作为对某些参数表达式的常规算术转换的一部分应用;一元+、-和~操作符的操作数;和移位操作符的操作数。

Integer Conversion Rank: No two signed integer types shall have the same rank, even if they have the same representation. The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision. The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char. The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any. The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width. The rank of char shall equal the rank of signed char and unsigned char. The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined but still subject to the other rules for determining the integer conversion rank. For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3. Usual Arithmetic Conversions: If both operands have the same type, no further conversion is needed. If both operands are of the same integer type (signed or unsigned), the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank. 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 is converted to the type of the operand with unsigned integer type. 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 is converted to the type of the operand with signed integer type. Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type. Specific operations can add to or modify the semantics of the usual arithmetic operations.

由于其他答案都没有谈到c++ 11中的规则,这里有一个。来自c++ 11标准(草案n3337)§5/9(强调了差异):

This pattern is called the usual arithmetic conversions, which are defined as follows: — If either operand is of scoped enumeration type, 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 shall be performed on both operands. 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.

这里有一个经常更新的列表。

警告!

转换从左到右进行。

试试这个:

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

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

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>