1.13.2. Arithmetic

[ fromfile: arithmetic.xml id: arithmetic ]

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 ;
    

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;
    

Example 1.23. src/arithmetic/arithmetic.cpp

[ . . . . ]

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

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;

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. 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





[10] See Table 1.2 for relative type widths.