一个双重提升每个int在方程中双重? [英] Does one double promote every int in the equation to double?

查看:128
本文介绍了一个双重提升每个int在方程中双重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一个浮点数据类型(例如 double )确保所有+, - ,*,/,%等数学运算假定双操作数?

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

如果故事比这更复杂,是否有描述这些规则的资源?我不应该问这样的问题,并且当公式的结果<$ c $时总是显式地将 int 转换为 double c> 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 ++清楚地定义了这些操作的顺序及其转换方式。

转换的发生方式取决于操作的顺序。

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 的结果。这是一个int添加到double,所以它将int转换为双精度和添加,导致双。同样的事情发生在 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?

在此示例中,先处理除法。 a b 都是整数,因此不进行转换。 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.

即使此表达式中存在double, code> a / b 首先计算,double表示任何操作,直到执行达到double。因此,发生整数除法。

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.

这篇关于一个双重提升每个int在方程中双重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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