一个 double 是否将方程中的每个 int 都提升为 double?

是否存在一种浮点数据类型(例如 double)确保所有 +、-、*、/、% 等数学运算都采用双操作数?

Does the presence of one floating-point data type (e.g. double) ensure that all +, -, *, /, %, etc math operations assume double operands?

如果故事比这更复杂,是否有资源可以描述这些规则?当等式的结果是 double 时,我是否应该不问这样的问题并始终明确地将 int 转换为 double.这是我正在考虑的一些方程式.我故意没有在我的系统上编译和运行,因为这可能是编译器依赖的类型.

If the story is more complicated than that, is there a resource that describes these rules? Should I not ask such questions and always explicitly cast int to double when the result of the equation is double. Here are some equations I'm thinking about. I purposefully did not compile and run then on my system, since this is the type of thing that could be compiler dependent.

int a(1), b(2), c(3);
double d(4.);
double result1 = a + b/d + c; // equal to 4 or to 4.5?
double result2 = (a + b)/d + c; // equal to 3 or to 3.75?    
double result3 = a/b + d; // equal to 4 or to 4.5?

推荐答案

我故意不在我的系统上编译和运行,因为这可能是编译器依赖的类型.

I purposefully did not compile and run then on my system, since this is the type of thing that could be compiler dependent.

这不依赖于编译器.C++ 明确定义了这些操作的顺序以及它们的转换方式.

This is not compiler dependent. C++ clearly defines the order of these operations and how they are converted.

转换如何发生取决于操作的顺序.

How the conversion happens is dependent on the order of operations.

double result1 = a + b / d + c; // equal to 4 or to 4.5?

在此示例中,首先进行除法.因为这是一个 int 除以 double,编译器通过将 int 转换为 double 来处理这个问题.因此,b/d 的结果是双精度数.

In this example, the division happens first. Because this is an int divided by a double, the compiler handles this by converting the int into a double. Thus, the result of b / d is a double.

C++ 做的下一件事是将 a 添加到 b/d 的结果中.这是一个加到 double 的 int,因此它将 int 转换为 double 并相加,得到一个 double.c 也会发生同样的事情.

The next thing that C++ does is add a to the result of b / d. This is an int added to a double, so it converts the int to a double and adds, resulting in a double. The same thing happens with c.

double result3 = a / b + d; // equal to 4 or to 4.5?

在本例中,首先处理除法.ab 都是整数,因此不进行转换.a/b 的结果是 int 类型,为 0.

In this example, division is handled first. a and b are both ints, so no conversion is done. The result of a / b is of type int and is 0.

然后,这个结果被添加到d.这是一个 int 加一个 double,所以 C++ 将 int 转换为一个 double,结果是一个 double.

Then, the result of this is added to d. This is an int plus a double, so C++ converts the int to a double, and the result is a double.

即使在这个表达式中存在双精度值,a/b 也会首先被计算,并且双精度值在执行到达双精度值之前没有任何意义.因此,会发生整数除法.

Even though a double is present in this expression, a / b is evaluated first, and the double means nothing until execution reaches the double. Therefore, integer division occurs.

我发现促销和转换规则非常复杂.通常,类似整数的数字(short、int、long)被提升为等效的浮点数(float、double).但是由于大小差异和符号,事情变得复杂了.

I find promotion and conversion rules pretty complex. Usually integer-like numbers (short, int, long) are promoted to floating-point equivalents (float, double). But things are complicated by size differences and sign.

有关转换的详细信息,请参阅此问题.

See this question for specifics about conversion.

相关文章