5.3.2. Numeric Types and Expressions


Numeric types

Numeric types are similar to those in Java.

The definition of Java is machine independent, but the exact meaning of C++ programs depends to some extent on the machine where the program runs. A particular operating system defines a word size: a 64-bit operating system uses 64 bits per word, and a 32-bit operating system uses 32 bits per word. Machine xlogin.cs.ecu.edu is a 64-bit machine.

The following are numeric types in C++.

long

Values of type long are integers that are 64 bits long in a 64-bit program and 32 bits long in a 32-bit program. Use them for integers that might be fairly large. The largest integer that can be represented in 32 bits is roughly 2 billion, and the largest integer that can be represented in 64 bits is roughly 8 billion billion.

int

Values of type int are typically 32-bit integers.

short

Values of type short are typically 16-bit integers. The largest integer that you can store in a 16-bit integer is roughly 32,000.

unsigned int, unsigned long, unsigned short

Normally, integers have a sign bit that is used to tell whether the integer is positive or negative. An unsigned integer is always treated as nonnegative, and does not devote one of the bits to the sign. So you can store a larger integer in an unsigned variable. For example, the largest unsigned 32-bit integer is roughly 4 billion.

double

A value of type double is an approximation to a real number that typically uses 64 bits. That gives you about 15 (decimal) digits of precision. Numbers are stored in base 2, not base 10, and you need to watch out for roundoff error. For example, 0.1 (one-tenth) cannot be represented exactly in base 2 with a finite number of digits, so it is rounded, similarly to the way 1/3 (0.33333...) is rounded in based 10.

float

A value of type float is an approximation to a real number that typically uses 32 bits. That gives you about 7 digits of precision. Because computers can perform many millions of operations in a single computation, roundoff error becomes a serious problem, and most programmers prefer to use type double for most real numbers.


Numeric constants

234

Write an integer constant in the usual way. Do not write commas in a number.

123456789L

Normally, an integer constant is assumed to have type int. Add L after the number to indicate that it has type long. Other suffixes are U (unsigned int) and UL (unsigned long).

234.5

Write real numbers in the usual way. Note that 3 has type int but 3.0 has type double.

234.5E12

An E indicates a power of 10. Constant 234.5E12 is 234.5×1012 and 1.0E−30 is 1.0×10−30.

0xA2

Precede an integer constant by 0x to indicate that it is written in base 16 (hexadecimal). The digits are 0,1, ...,9,A,B,C,D,E,F, where A stands for 10, B stands for 11, etc. You can use either upper case A-F or lower case a-f as digits.

011

An integer constant that starts with 0 is assumed to be written in base 8 (octal). For example, 011 is the same as 9.


Coercion and conversion

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. A value of type long is converted to type int automatically by selecting only the rightmost 32 bits. We will see examples of conversions below.

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.

You can request conversions explicitly. If T is a numeric type, then write (T), with the parentheses, 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.


Numeric operators

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.

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.


Some numeric functions

If you #include <cmath> then you can use the following functions. In all cases, x and y have type double, n has type int and L has type long. Variables u and v can be of any numeric type.

sqrt(x)

Approximately the square root of x.

pow(x, y)

Approximately xy, where x and y are real numbers.

abs(n)

The absolute value of int n.

labs(L)

The absolute value of long integer L.

fabs(x)

The absolute value of real number x.

If you #include <algorithm> then you can use the following functions.

max(u, v)

The larger of u and v. For example, max(3,7) = 7. This works for any numeric type.

min(u, v)

The smaller of u and v. For example, min(3,7) = 3. This works for any numeric type.


Integer overflow (optional)

Each size of integer (given by a number of bits used to represent it) has a largest allowed positive value. If you add 1 to that number, you will not get an error. Instead, the result will be a negative number. For example, the largest value that can be stored in a variable of type short is 32,767. Statement

  short n = 32767;
  short m = n + 1;
makes m = −32768, the smallest negative number that can be stored in a variable of type short.


Exercises

  1. Why do most programmers prefer to use type double instead of type float for most uses? Answer

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

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

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

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

  6. What is the value of expression sqrt(25.0) Answer

  7. What is the value of expression max(3,8) Answer

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