Page 32 - DCAP404 _Object Oriented Programming
P. 32

Unit 2: Beginning of OOP Language




          If x had been assigned a value of –12 and y had been assigned 2, then the value of x/y would  Notes
          still be  –6 but the value of a x%y would be 0. Similarly, if x and  y had both been assigned
          negative values (–12 and –2, respectively), then the value of x/y would be 3 and the value of x%y
          would be –2.
          x = ((x/y)*y) + (x%y)

          will be satisfied in each of the above cases. Most versions of C++ will determine the sign of the
          remainder in this manner, though this feature is unspecified in the formal definition of the
          language.
          Here  is an illustration of the results that are obtained with floating-point operands  having
          different signs. Let y1 and y2 be floating-point variables whose assigned values are 0.70 and
          3.50. Several arithmetic expressions involving these variables are shown below, together with
          their resulting values.

          Expression                               Value
           y1+y2                                    2.72
           y1–y2                                   –4.28
            y/y2                                  –0.2728

          Operands that differ in type may undergo type conversion before the expression takes on its
          final  value.  In general, the final  result will be expressed in the  highest precision  possible,
          consistent with the data type of the operands. The following rules apply when neither operand
          is unsigned.
          1.   If both operands are floating-point types whose precisions differ (e.g., a float and a double),
               the lower-precision operand will be converted to the precision of the other operand, and
               the result will be expressed in this higher precision. Thus, an operation between a float
               and double will result in a double; a float and a long double will result in a long double;
               and a double and a long double will result in a long double. (Note: In some versions of
               C++, all operands of type float are automatically converted to double.)
          2.   If one operand is a floating-point type (e.g., float, double or long double) and the other is
               a char or an int (including short int or long int), the char or int will be converted to the
               floating-point type and the result will be expressed as such. Hence, an operation between
               an int and a double will result in a double.
          3.   If neither operand is a floating-point type but one is long int, the other will be converted
               to long int and the result will be long int. Thus, an operation between a long int and an int
               will result in a long int.
          4.   If  neither  operand  is a floating-point type or  a  long int,  then both operands will  be
               converted to int (if necessary) and the result will be int. Thus, an operation between a short
               into and an int will result in an int.





              Task  What does the following expression evaluate to?
             6 + 5 * 4 % 3










                                           LOVELY PROFESSIONAL UNIVERSITY                                   25
   27   28   29   30   31   32   33   34   35   36   37