Valid Expressions and Data type usage.

I been having trouble on valid expressions and data type usages.
If intger deals with numbers ; than when do I know when to use double?
For example: 18 - (5*2) would that be a integer?
Integer deals with integers like 4 or 1, doubles deal with numbers like 10.441 or 7.0.

If you are just using integers only (like you are), then the result will be an integer. If there are doubles involved, then the result will be a double (although parts of it might be integers before they become doubles).
So than, 1-99 is like integers. ( real numbers with no decimals) and doubles are more like with decimals? Your example : 10.441 is a double.

Than what about negatives? Would they consider as same?
Yes. The same rules apply to negative numbers.
I'm sorry that I'm asking alot of questions.
But when you put it as a operations, ( math operations) When you only see a single number with no decimals for the problem , is that consider as integer?

Example : 28/4


And if there was a problem with decimals? Would that be doubles?

Example: 10.5 +14 /3

1.) 28/4 = 7 making it an integer

2.) 10.5 + 14/3
10.5 + 4.666667
10.5 + 4 (decimal truncated because of integer division)
= 14.5 (double because of double addition)
Is there a tip or a way to identify in or vailid expressions?

If you need double, always put at least one decimal place. If you need integer, don't put the decimal. If you need float, put the decimal place and add f: 1.0f/2.0f
Ah , thats a great way for me to memorize this .
Thank you guys!
@firedraco:

I'm pretty sure your example is wrong because 14/3 will be evaluated using integer division, not floating point.


EDIT: NVM, sorry, my bad.

@parkjulie07:

The simple way to remember: if you have A + B or A * B or A - B or A / B, then the compiler uses integer division
if A and B are both integers, otherwise it uses floating point. Also, note that when you put hard coded numbers
in your program, for example:

cout << ( 1 + 2 );
cout << ( 1.0 + 2 );

numbers without decimal points are treated as integers (int) and numbers with decimal points are treated as
floating point (double).

So here are some examples to illustrate:

1 + 2; // Evaluated using integer addition, since 1 and 2 are both integer constants
1.0 + 2.0; // Evaluated using floating point addition, since 1.0 and 2.0 are both floating point
1.0 + 2; // Evaluated using floating point addition, since 1.0 is floating point

1.0 + 3/2;

Compiler first evaluates 3/2 according to precedence rules (division before addition). 3 and 2 are
both integers, so integer division is used. The result of integer division is also an integer. Next,
the addition is performed using floating point since 1.0 is floating point.

Summary: if both sides of the operator are integer, then integer arithmetic is used, otherwise floating
point arithmetic is used.
Last edited on
I'm pretty sure your example is wrong because 14/3 will be evaluated using integer division, not floating point.
That was the point he was trying to make.
Yes, sorry, missed the parenthetical statement.
Topic archived. No new replies allowed.