Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does one double promote every int in the equation to double?

People also ask

Is a double int a double?

Conclusion. The int and double are major primitive data types. The main difference between int and double is that int is used to store 32 bit two's complement integer while double is used to store 64 bit double precision floating point value. In brief, double takes twice memory space than int to store data.

Can you mix int and double?

When one operand is an int and the other is a double, Java creates a new temporary value that is the double version of the int operand. For example, suppose that we are adding dd + ii where ii is an int variable. Suppose the value of ii is 3. Java creates a temporary value 3.0 that is the corresponding double value.

How do you convert an int to a double in C++?

if i'm not wrong you can directly assign an integer to a double in c++, there's no need to do an explicit casting. It's called "typecasting". int a = 5; double b = (double) a; double c = 4.0; int d = (int) c; chandra.

What double means in C++?

Double: The C++ double is also a primitive data type that is used to store floating-point values up to 15 digits.


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.


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.