C++ 一个 double 是否会将等式中的每个 int 提升为两倍?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13663545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does one double promote every int in the equation to double?
提问by Alan Turing
Does the presence of one floating-point data type (e.g. double
) ensure that all +, -, *, /, %, etc math operations assume double operands?
一种浮点数据类型(例如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
转换double
为等式的结果是double
. 这是我正在考虑的一些方程式。我故意没有在我的系统上编译和运行,因为这是可能依赖于编译器的类型。
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?
回答by Slix
I purposefully did not compile and run then on my system, since this is the type of thing that could be compiler dependent.
我故意没有在我的系统上编译和运行,因为这是可能依赖于编译器的类型。
This is notcompiler dependent. C++ clearly defines the order of these operationsand how they are converted.
这与编译器无关。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?
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.
在这个例子中,除法首先发生。因为这是一个 int 除以 double,编译器通过将 int 转换为 double 来处理这个问题。因此,结果b / d
是双倍。
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
.
C++ 做的下一件事是添加a
到b / d
. 这是一个加到双精度的整数,因此它将整数转换为双精度并相加,从而产生双精度。同样的事情发生在c
.
double result3 = a / b + d; // equal to 4 or to 4.5?
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.
在本例中,首先处理除法。a
并且b
都是整数,所以没有进行转换。的结果a / b
是 int 类型且为 0。
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.
然后,将其结果添加到d
. 这是一个int加一个double,所以C++把int转换成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.
即使此表达式中存在双精度值,a / b
也会先求值,并且双精度值在执行达到双精度值之前没有任何意义。因此,发生整数除法。
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.
我发现促销和转换规则非常复杂。通常类似整数的数字(short、int、long)被提升为等价的浮点数(float、double)。但事情因尺寸差异和符号而变得复杂。
See this questionfor specifics about conversion.
有关转换的详细信息,请参阅此问题。
回答by Alan Turing
Does one
double
promote everyint
in the equation todouble
?
是否将等式中的
double
每一个都提升int
为double
?
No. Only the result of a single operation (with respect to precedence).
不。只有单个操作的结果(相对于优先级)。
double result1 = a + b/d + c; // equal to 4 or to 4.5?
4.5.
4.5.
double result2 = (a + b)/d + c; // equal to 3 or to 3.75?
3.75.
3.75。
double result3 = a/b + d; // equal to 4 or to 4.5?
4.
4.
回答by Ramy Al Zuhouri
You must consider the precedence of every operator, you must think like a parser:
您必须考虑每个运算符的优先级,您必须像解析器一样思考:
double result1 = a + b/d + c; // equal to 4 or to 4.5?
That's like a + (b/d) +c because the '/' operator has the biggest precedence.Then it doesn't matter what of these 2 operations is made for first, because the floating point operand is in the middle, and it "infects" other operands and make them be double.So it's 4.5.
那就像a + (b/d) +c 因为'/'运算符的优先级最高。那么这2个操作中的哪个先做就无所谓了,因为浮点操作数在中间,它“感染”其他操作数并使它们成为双倍。所以它是4.5。
double result2 = (a + b)/d + c; // equal to 3 or to 3.75?
Same here, it's like ((a+b)/d )+c, so a+b is 3, that 3 becomes a floating point number because gets promoted to double, because is the dividend of d, which is a double, so it's 0.75+3, that is 3.75.
这里也一样,就像 ((a+b)/d )+c,所以 a+b 是 3,那个 3 变成浮点数,因为被提升为 double,因为是 d 的被除数,它是一个 double,所以它是 0.75+3,即 3.75。
double result3 = a/b + d; // equal to 4 or to 4.5?
It's like (a/b)+d, so a/b is zero and d is 4, so it's 4. A parser makes all the operations in order of precedence, so you can exactly know what will be the result of the expression.
这就像 (a/b)+d,所以 a/b 是 0,d 是 4,所以它是 4。解析器按优先级顺序进行所有操作,因此您可以确切地知道表达式的结果是什么。
回答by Eric Postpischil
Generally, if one operand of a binary operator is floating point and the other is integer, the integer is converted to floating point, and the result is floating point.
通常,如果二元运算符的一个操作数是浮点数,另一个是整数,则将整数转换为浮点数,结果为浮点数。
In a compound expression, with multiple subexpressions, each operator is processed individually, using the precedence rules you probably know. Thus, in a*b + c*d
, a*b
is evaluated, and c*d
is evaluated, and the results are added together. Whatever is in c*d
has no effect in a*b
and vice-versa.
在具有多个子表达式的复合表达式中,使用您可能知道的优先级规则单独处理每个运算符。因此,在a*b + c*d
,a*b
被评估,并且c*d
被评估,且结果被加在一起。任何内容c*d
都没有影响,a*b
反之亦然。
C++ is complicated, of course, and user-defined operators may have other behaviors.
当然,C++ 很复杂,用户定义的运算符可能有其他行为。
The authoritative resource that defines the rules is the C++ standard. The standard is quite large and technical. You might prefer to examine the C standard first. See this answerfor links to the standards. Any good book on C or C++ should describe the default type conversions and expression evaluation.
定义规则的权威资源是 C++ 标准。该标准是相当大的和技术性的。您可能更愿意先检查 C 标准。有关标准的链接,请参阅此答案。任何关于 C 或 C++ 的好书都应该描述默认类型转换和表达式计算。