1.13.2. Arithmetic

[ fromfile: arithmetic.xml id: arithmetic ]

Every programming language provides support for basic arithmetic operations and expressions. For each of its native numerical types, C++ provides these four basic arithmetic operators:

These operator symbols are used to form expressions in the standard infix syntax that you learned in math class.

C++ provides shortcut operators that combine each of the basic operators with the assignment operator (=) so that, for example, it is possible to write

 x += y; 

instead of

 x = x + y; 

C++ also provides unary increment (++) and decrement (--) operators that can be used with integral types. If one of these operators is applied to a variable on its left side (prefix), then the operation is performed before the rest of the expression is evaluated. If it is applied to a variable on its right side (postfix), then the operation is performed after the rest of the expression is evaluated. Modern compilers usually produce a shorter block of machine code when compiling the prefix operator than when compiling the postfix operator, so prefix operators are recommended over postfix operators when it doesn't matter whether the operation takes place before or after the expression is evaluated. Example 1.21 through Example 1.26 demonstrate the use of the C++ arithmetic operators.

Example 1.21. src/arithmetic/arithmetic.cpp

[ . . . . ]

#include <QTextStream>

int main() {
    QTextStream cout(stdout);
    double x(1.23), y(4.56), z(7.89) ;
    int i(2), j(5), k(7);
    x += y ;
    z *= x ;
    cout << "x = " << x << "\tz = " << z
            << "\nx - z = " << x - z << endl ;
    

Integer division is handled as a special case. The result of dividing one int by another produces an int quotient and an int remainder. The operator / is used to obtain the quotient. The operator % (called the modulus operator) is used to obtain the remainder. Example 1.22 demonstrates the use of these integer arithmetic operators.

Example 1.22. src/arithmetic/arithmetic.cpp

[ . . . . ]

    cout << "k / i = " << k / i 
            << "\tk % j = " << k % j << endl ;
    cout << "i = " << i << "\tj = " << j << "\tk = " << k << endl;
    cout << "++k / i = " << ++k / i << endl;
    cout << "i = " << i << "\tj = " << j << "\tk = " << k << endl;
    cout << "i * j-- = " << i * j-- << endl;
    cout << "i = " << i << "\tj = " << j << "\tk = " << k << endl;
    

Mixed expressions, if valid, generally produce results of the widest of the argument types.[19] In Example 1.23 you can see that the result of a double divided by an int is a double.

Example 1.23. src/arithmetic/arithmetic.cpp

[ . . . . ]

    cout << "z / j = " << z / j << endl ;
    

Conversions are discussed further in Chapter 19, "Types and Expressions."

C++ also provides a full set of boolean operators to compare numeric expressions. Each of these operators returns a bool value of either false or true.

A bool expression can be negated with the unary not (!) operator. Expressions can be formed with two or more bool expressions using the conjunction and disjunction operators

Example 1.24. src/arithmetic/arithmetic.cpp

[ . . . . ]

    /*   if () ... else   approach */
    if (x * j <= z)
        cout << x * j << " <= " << z << endl ;
    else
        cout << x * j << " > " << z << endl;

In addition to the binary boolean operators, Example 1.24 makes use of the conditional-expression.

(boolExpr) ? expr1 : expr2

returns expr1 if boolExpr is true, and otherwise returns expr2.

Example 1.25. src/arithmetic/arithmetic.cpp

[ . . . . ]

    /* conditional operator approach */
    cout << x * k
            <<( (x * k < y * j) ? " < " : " >= ")
            << y * j << endl;
    return 0;
}

Example 1.26 shows the output of this program.

Example 1.26. src/arithmetic/arithmetic.cpp

[ . . . . ]

Output:

x = 5.79        z = 45.6831
x - z = -39.8931
k / i = 3       k % j = 2
i = 2   j = 5   k = 7
++k / i = 4
i = 2   j = 5   k = 8
i * j-- = 10
i = 2   j = 4   k = 8
z / j = 11.4208
23.16 <= 45.6831
46.32 >= 18.24





[19] See Table 1.2 for relative type widths.