HP 3000 Manuals

Arithmetic Expressions [ SPL to HP C/XL Migration Guide ] MPE/iX 5.0 Documentation


SPL to HP C/XL Migration Guide

Arithmetic Expressions 

          Table 5-19.  Arithmetic Expressions 

-------------------------------------------------------------------------------------------------
|                                                 |                                             |
|                       SPL                       |             HP C/XL Equivalent              |
|                                                 |                                             |
-------------------------------------------------------------------------------------------------
|                                                 |                                             |
| arithmetic-expression:                          | Same as SPL, except as noted below.         |
|                                                 |                                             |
|      [sign] primary [operator primary][,...]    |                                             |
|                                                 |                                             |
-------------------------------------------------------------------------------------------------
|                                                 |                                             |
| sign:                                           | Same as SPL, except:                        |
|                                                 |                                             |
|      +                                          |      (+ is not permitted as a sign)         |
|      -                                          |                                             |
|                                                 |                                             |
-------------------------------------------------------------------------------------------------
|                                                 |                                             |
| operator:                                       | Same as SPL, except:                        |
|                                                 |                                             |
|      +    (addition)                            |                                             |
|      -    (subtraction)                         |                                             |
|      *    (multiplication)                      |                                             |
|      /    (division)                            |                                             |
|      ^    (exponentiation; allows real and long |      Convert ^ to pow(x,y) function.        |
|      values to integer power)                   |                                             |
|      MOD   (modulus)                            |      %   (modulus)                          |
|                                                 |                                             |
| Note:  The SPL exponentiation operator,         |                                             |
| "^", is the HP C/XL exclusive OR operator.      |                                             |
|                                                 |                                             |
-------------------------------------------------------------------------------------------------
|                                                 |                                             |
| primary:                                        | Same as SPL, except:                        |
|                                                 |                                             |
|      variable                                   |                                             |
|      constant                                   |                                             |
|      bit-operation                              |                                             |
|      ( arithmetic-expression )                  |                                             |
|      \ arithmetic-expression \                  |      Convert \...\ to abs(x) function.      |
|      function-designator                        |                                             |
|      ( assignment-statement )                   |                                             |
|                                                 |                                             |
-------------------------------------------------------------------------------------------------

The most significant difference between SPL and HP C/XL arithmetic
expressions is that SPL allows no type mixing, whereas HP C/XL performs
automatic type conversions during the evaluation of an expression.
Normally, this is very convenient and produces the desired result.
Occasionally, type "cast" operators may be required to force HP C/XL to
adhere to SPL-like operations.  Particular caution must be observed with
any bit manipulations, as an automatic type conversion may result in an
unexpected change in word size.

Sequence of Operations (Arithmetic) 

          Table 5-20.  Order of Evaluation of Arithmetic Operators 

---------------------------------------------------------------------------------------------
|                                             |                                             |
|                     SPL                     |             HP C/XL Equivalent              |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| Order of evaluation:                        | Same as SPL, except for the following:      |
|                                             |                                             |
|                                             |                                             |
|    1.  bit operations                       | bit operations   Implemented as function    |
|        expressions in parentheses           |                  calls; same sequence       |
|        expressions in backslashes           |                  level.                     |
|        function designators                 |                                             |
|        assignment statements in parentheses | absolute value   (expressions in            |
|                                             |                  backslashes) Implemented   |
|                                             |                  as function call; same     |
|    2.  exponentiation                       |                  sequence level.            |
|                                             |                                             |
|    3.  multiply                             | exponentiation   Implemented as function    |
|        divide                               |                  call; collapses into first |
|        modulus                              |                  level; care needed in      |
|                                             |                  converting operands.       |
|    4.  addition                             |                                             |
|        subtraction                          |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------

In general, well-formed expressions, with parentheses used to avoid
possible confusion, will always yield the same sequence of operations.

Care may be necessary to maintain the same precision, because of implicit
data conversion.  (See "Type Mixing (Arithmetic)" in this chapter).

Type Mixing (Arithmetic) 

          Table 5-21.  Arithmetic Type Mixing 

---------------------------------------------------------------------------------------------
|                                             |                                             |
|                     SPL                     |             HP C/XL Equivalent              |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| The mixing of data types across operands is | Arithmetic data types may be mixed.  HP     |
| not allowed in SPL, except that real and    | C/XL performs automatic type conversions as |
| long values may be exponentiated to integer | needed, generally proceeding toward long    |
| powers.                                     | int and double values.  Many type transfer  |
|                                             | functions can be eliminated or simplified.  |
| Type transfer functions (see "Expression    |                                             |
| Types" above) are used to convert data      |                                             |
| types.                                      | Where data types need forcing, HP C/XL      |
|                                             | provides the "cast" operators--data type    |
|                                             | names in parentheses preceding the value to |
|                                             | be converted.  See "Expression Types" above |
|                                             | for more detail.                            |
|                                             |                                             |
---------------------------------------------------------------------------------------------

As an example, if you need to force a floating point divide of two
integers, the cast operator is (float):

     X = (float)M/(float)N;

Cast operators are essential for converting exponentiation involving
integers into the pow function.  The SPL statement:

     I := J^K;
     all integer variables 

becomes the HP C/XL statement:

     I = pow ( (double) J , (double) K ) ;



MPE/iX 5.0 Documentation