# Thread: Mathematics of intergers, doubles, floats, etc.

1. ## Mathematics of intergers, doubles, floats, etc.

Hello. I'm a beginning C++ programming student coming up on my first test. I'm sure this is a simple enough question. Basically i'm not sure for this particular question how the products of the formulas would appear. If someone could explain and answer these formulas, any help would be so greatly appreicated.

What are the values of the following expressions? In each line, assume
that
double x = 2.5;
double y = -1.5;
int m = 18;
int n = 4;
i. x + n * y − (x + n) * y
ii. m/n + m%n
iii. 5 * x − n/5

2. It is a matter of substituting the values of those variables, and then evaluating the resulting expressions. You can do it on paper like how you would simplify math expressions.

3. so it behaves just like regular mathematics? such as 5 *2.0= 10 and 2.5*2=5.0?

4. Somewhat, but there is also the issue of type, e.g., 5 * 2.0 evaluates to 10.0, not 10.

5. oh ok, so does behave just like common mathematics with a few tweaks. Thank you

6. Not quite:

I don't think Laserlights example quite gets across the importance of the type as well as this does...
Operations on ints give int results. Operations containing float or double operands have float or double results. E.g.
8/5 is 1
whereas
8.0/5.0 is 1.6

7. Originally Posted by iMalc
Not quite:

I don't think Laserlights example quite gets across the importance of the type as well as this does...
Operations on ints give int results. Operations containing float or double operands have float or double results. E.g.
8/5 is 1
whereas
8.0/5.0 is 1.6
To give one more example in that vein, 8.0/5 is also 1.6. That occurs in this expression because 5 is converted to a value of type double (the same type as 8.0) before performing the division.

8. The rules are a little tricky, but here is a few guidelines:
- In an expression, the expression will be promoted to the largest type present in the expression. So, if you have, say, a short and an int, the expression will be an int.
- If, in an expression, there is a floating point type (float, double) and a non-floating point type, the expression will be promoted to a floating point type.

Thus, 8/5 is (int/int), and therefore is 1.
8.0/5 is (double/int), thus, the int is promoted to a double and the expressions becomes 8.0/5.0 (double/double), thus the result is a double and becomes 1.6.

Beware that expressions are performed on a operation basis.
Therefore, 8/5 + 1.0 is 2.0, because 8/5 is evaluated to an int, promoted to a double and added to the 2.0.