c++ – Does one double promote every int in the equation to double?

c++ – Does one double promote every int in the equation to double?

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.

Does one double promote every int in the equation to 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.

double result2 = (a + b)/d + c; // equal to 3 or to 3.75?

3.75.

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

4.

c++ – Does one double promote every int in the equation to double?

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?

Thats like a + (b/d) +c because the / operator has the biggest precedence.Then it doesnt 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 its 4.5.

double result2 = (a + b)/d + c; // equal to 3 or to 3.75?  

Same here, its 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 its 0.75+3, that is 3.75.

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

Its like (a/b)+d, so a/b is zero and d is 4, so its 4.
A parser makes all the operations in order of precedence, so you can exactly know what will be the result of the expression.

Leave a Reply

Your email address will not be published.