Does one double promote every int in the equation to double?
Solution 1
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 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?
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.
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?
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.
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.
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.
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.
Solution 2
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.
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.
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.
Solution 3
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.
C++ is complicated, of course, and user-defined operators may have other behaviors.
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 answer for links to the standards. Any good book on C or C++ should describe the default type conversions and expression evaluation.
![Alan Turing](https://i.stack.imgur.com/zipop.jpg?s=256&g=1)
Alan Turing
Updated on July 20, 2022Comments
-
Alan Turing almost 2 years
Does the presence of one floating-point data type (e.g.
double
) ensure that all +, -, *, /, %, etc math operations assume double operands?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
todouble
when the result of the equation isdouble
. 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?