Question

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 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?
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top