Arithmetic Operations and Expressions

  1. In an arithmetic expression, the type of the value produced and the value of the result can depend on the type of the data involved.

  2. In any assignment statement

    Variable = Expression

    involving an expression, the expression is first evaluated to obtain a value and the value is then assigned to the variable on the left.

  3. Example:

    int integer1(11), integer2(4), integer3;
    double double1(11.0), double2(4.0), double3;
    integer3 = integer1 / integer2;    // integer3 is 2 since in integer division only the quotient is returned!
    integer4 = integer1 % integer2;    // integer4 is 3 (integer remainder)
    double3 = integer1 / integer2;    // double3 is 2.00 (expression is evaluated first to give 2,                                                                            which is then converted to 2.0 before assigning to double3)
    double3 = double1 / double2;    // double3 has the value of 2.75
    double3 = integer1 / double2;    // double3 also has the value of 2.75
    double3 = double1 / integer2;    // double3 also has the value of 2.75

  4. When one integer is divided by another integer, the division is carried out using "integer division" where only the quotient is returned. Any remainder is dropped (not rounded).

    The remainder of such an "integer division" if any, can be retrieved using the % operator, which is defined for integer operation only.

  5. Notice that when a and n are positive integers, then the result a % n can only be an integer, 0, 1, ..., or n-1. For example, 7 % 4 gives 3, but 8 % 4 gives 0.

    You need to be very careful using the integer remainder operator, %, on negative integers. The result may depend on the particular compiler used.

    On .NET, if a is negative, then the result of a % n is given by -( (-a) % n ), which is a negative integer and does not lie between 0 and n-1.

    For example, -7 % 4 gives -3.

    This is different from the usual mathematical definition, where the remainder is the integer that you reach by starting with a and adding or subtracting enough number of n until you reach an integer between 0 and n-1.

    For example, 11 % 4 is given by 11 - 4 - 4 = 3, and 7 % 4 is given by 7 - 4 = 3. Note that according to this mathematical definition, we have -7 % 4 = -7 + 4 + 4 = 1, and not the -3 that you get from .NET.

    You can force it to lie within that range by replacing it with

    n - 1 - ((-a - 1) % n)

    when a is a negative integer.
  6. In an arithmetic operation involving an integer and a double, the integer is first converted to a double, and the operation is carried out as doubles and the result is a double.

  7. Results of operations using / and % involving negative integers can differ depending on the compiler, and therefore such operations should be avoided.

  8. An expression that has more than one operator can have the order of operation changed by proper use of parentheses.
    For example, the following two expressions give different results
    (x + y) * z;
    x + (y * z);

  9. In the absence of parentheses, the computer follows the so-called precedence rules to determine the actual order of operations.
    • An operator of a higher precedence must be carried out first before the lower order operators.
    • Operators having the same level of precedence are evaluated from left to right.
    • A complete set C++ precedence rules are given in Appendix 2. Operators are listed from high to low precedence. Operator having the same level of precedence are grouped together.
    • For example, you will find the following operators list:

      !   not
      ------------------------------
      -   unary minus
      +   unary plus
      ------------------------------
      *   multiplication
      /   division
      %   integer remainder (modulo)
      ------------------------------
      +   addition
      -   subtraction
      ------------------------------
      =   assignment

    • Use parentheses if you have any doubt about the precedence of the operators

    • The use of parentheses also helps to improve the readability of the expression

  10. C++ has a shorthand notation of the general form:

    Variable Operator = Expression

    which is defined to be equivalent to

    Variable = Variable Operator (Expression)

    where operator represents any one of the operators in C++.

    Examples:
    count += 2;                                // count = count + 2;
    cost -= discount;                       // cost = cost - discount;
    position += velocity * time;     // position = position + velocity * time;
    change %= 100;                        // change = change % 100;

  11. One must be careful when writing expressions from mathematical formulas.

  12. Problem with parentheses:

    7 * ( 2 * ( 3 * x - 4 * y) + 2 ) + 3 ) + ( b / 2 * a + 1 )

    clearly has a problem since there are 3 left parentheses but 4 right parentheses. This unbalanced parentheses can be easily detected by counting the total number of left and right parentheses. On the other hand, expression

    7 * ( 2 * ( 3 * x - 4 * y) + 2 ) + 3 ) + ( b / 2 * ( a + 1 )

    has 4 left and right parentheses but is also incorrect. A simple way to detect the presence of an error is to keep track of a single counter by scanning the expression from left to right. Start with 1 at the first opening parenthesis, add 1 whenever you see an opening parenthesis and subtract 1 whenever you see a closing parenthesis. If the counter ever drops below 0, or is not 0 at the end, the parentheses are not balanced. For this example, you would get

    7 * ( 2 * ( 3 * x - 4 * y) + 2 ) + 3 ) + ( b / 2 * ( a + 1 )
         1      2                 1       0      -1

    which indicts an error.