Language Elements and Reference:xpplrm

Mathematical operators Foundation

The mathematical operators execute mathematical operations using numeric values. Except for the two operators + and -, they are always binary operators. The plus and minus operators are also unique because they can be used with values of the character and date data types.

Mathematical operators
Operator Type Data type Operation
+ unary N positive signed prefix
+ binary N, D numeric addition
+ binary C concatenation
- unary N negative signed prefix
- binary N numeric subtraction
- binary D subtraction of a date
- binary C concatenation without blank spaces between character strings
* binary N multiplication
/ binary N division
** binary N exponentiation
^ binary N exponentiation
% binary N modulus

The operations of the plus operator depends on the program context and the data type of the operands used.

When a unary plus operator appears in front of a numeric expression, it is the same as multiplying by the value +1. The plus operator actually performs no operation with the operand, but raises the precedence of the operation in relation to all other mathematical operators except the unary minus operator.

When the plus operator appears as a binary operator between two numeric expressions, it adds the value of the right expression to the value of the left expression. The result of the addition is a numeric value which is the sum of the two expressions.

If one operand for the addition is a date expression and the other operand is a numeric expression, the numeric is treated as the number of days and is added to the value of the date expression. The result is a date value.

When the binary plus operator appears between two character expressions, the string of the right expression is attached to the end of the string of the left expression. The result of this operation is a character string.

?   4 + 4                        // result: 8 
? + 4 - 4                        // result: 0 

? CtoD("12/31/94") + 6           // result: 01/06/95 
? 6 + CtoD("12/31/94")           // result: 01/06/95 

? "James " + "Bond"              // result: James Bond 

The minus operator is similar to the plus operator and can be used with operands having "numeric", "date" or "character" data types. The unary minus operator performs multiplication by -1 which changes the signed prefix of a numeric value. The result of the numeric binary minus operator is the numeric difference between the left and right operands.

When the minus operator appears between two date expressions, it subtracts the date value of the right expression from the date value of the left expression. The result of the subtraction is a numeric value indicating the difference between the two dates as the number of days.

When the left operand of the minus operator is a date expression and the right operand is a numeric expression, the numeric number of days is subtracted from the date. The result is a date value. When the left operand is numeric and the right operand is of the date data type, a runtime error occurs.

When the binary minus operator appears between two character expressions, the string of the right expression is attached to the string of the left expression after all blank spaces at the end of the left character expression are removed and attached to the end of the resulting character string.

nValue := 10 

? -nValue                        // result: -10 
? nValue := -nValue              // result: -10 
? nValue := -nValue              // result: 10 

?   4 - 4                        // result: 0 
? - 4 - 4                        // result: -8 

? CtoD("12/31/94") - 7           // result: 12/24/94 

? CtoD("12/31/94") - ; 
  CtoD("12/31/93")               // result: 365 

? "James " - "Bond"              // result: JamesBond 

The other mathematical operators can only be used with numeric values. They execute the operations of multiplication, division, exponentiation and modulus (the remainder of division). There are two kinds of exponentation operators: ^ and **.

Calculations using the various mathematical operators can be used within complex expressions. During calculations the operators have a specific precedence. Operators of higher precedence execute their operations within an expression prior to operators of lower precedence. Operators of equal precedence in a program line are processed from left to right. The precedence of the mathematical operators is as follows:

- +

Signed prefixes (unary minus and plus operators)

** ^

Exponentiation

* / %

Multiplication, division, modulus

- +

Subtraction, addition (binary minus and plus operators)

A detailed description of the precedence of all operators is found in the chapter "Precedence of Operators".

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.