3B. Numeric Types and Expressions


Numeric types

C++ numeric types are similar to those in Java, with minor exceptions. We will mostly use only two numeric types: int for integers and double for real numbers.

See additional numeric types if you are interested. That page also discusses some differences between C++ and Java.


Arithmetic operators and expressions

x + y

x + y is the sum of x and y.

xy

xy is the difference, xy.

x

x is the negation of x.

x * y

x * y is the product x times y.

x / y

x / y is the quotient x divided by y.

Important: If x and y are both integers then x/y is also an integer. Anything after the decimal point is ignored. So 7/4 has value 1, not 1.75.


x % y

x % y is the remainder when you divide integer x by integer y. For example, when you divide 14 by 3 you get a quotient of 4 and a remainder of 2. So 14 % 3 = 2. You can only use the % operator on integers.


Precedence and associativity

Operator * has higher precedence than operator +. For example, expression 2 + 5 * 2 has value 12, since the multiplication is done first.

Operators *, / and % have the same precedence, and operators + and - have the same precedence.

All of the numeric operators are performed from left to right, except where precedence rules and parentheses force otherwise. For example, 10 − 2 − 3 has value 5, but 10 − (2 − 3) has value 11.

Notice that 16/2*2 has value 16 since * and / have the same precedence. The division 16/2 is done first, and then the result of that division is multiplied by 2. If you want 16/(2*2), be sure to use parentheses.


Conversions

Numbers are converted automatically from one type to another where necessary. For example, 2 is converted to 2.0 if it occurs where a value of type double is needed.

Converting an integer to a real number is usually harmless. Converting a real number to an integer throws away everything after the decimal point. So if 2.7 occurs in a place where an integer is required, it is converted to 2. Watch out for that. It can lead to errors that are difficult to detect, where you accidentally use type int but mean double.

You can request conversions explicitly. If T is a numeric type, then write (T) in front of an expression to convert the value of that expression to type T. For example, expression (int)(x + y) is the value of x + y converted to type int.


Optional material


Summary

We will mostly use types int and double.

Addition, subtraction and division use familiar operators. Multiplication is *. Writing two values next to one another does not mean to multiply.

Watch out for division. When x and y are integers, expression x/y is always an integer. Expression 2/3 has value 0 no matter where it occurs.

C++ obeys common precedence rules, but * and / have the same precedence. Numeric operations of the same precedence are done from left to right. Use parentheses to override precedence rules, or any time you find that they make an expression more clear.

Expression (double)(z) is the value of z, converted to type double.


Exercises

  1. What is the value of expression 9 − 6 + 3? Answer

  2. What is the value of expression 3/5? Answer

  3. What is the value of expression 9/4? Answer

  4. What is the value of expression 25 % 4? Answer

  5. What is the value of expression 0 % 5 Answer

  6. Is [3 + 2] * 8 an allowed C++ expression? Answer