Numeric Expressions
6.2 T YPES OF A RITHMETIC O PERATORS
There are six different arithmetic operators in C/AL.
The Plus (+) Operator
The plus operator is used for several purposes. As shown earlier, if the terms on either side of it are both string types, the result is a string, so this would not be an example of it being used as an arithmetic operator. However, it can be used as either a unary or binary arithmetic operator. When used as a unary operator, its function is to not change the sign of the term following it. It literally does nothing and is rarely used. If it is used, its purpose is to explicitly show that a value is positive. Here is an example of an expression using the plus unary operator and the same expression without it:
IntVariable * +11
·
· IntVariable * 11
When normally used as a binary operator, its function is to add the term following it to the term preceding it. Both terms can be numeric, or one term can be a Date or a Time and the other an integer. If either term is a decimal value, then the result is decimal. If both terms are char values and the sum is less than 256, the result is char; otherwise, the result is integer. If both terms are option or integer values and the sum is in the allowable values for integers, the result is integer; otherwise, the result is decimal.
If one term is a date and the other an integer, the result is the date that is the integer number of days away from the date term. Thus, the value of
03202001D + 7 is 03272001D. If the resulting value results in an
invalid date, a run-time error will occur.
Similarly, if one term is a time and the other an integer, the result is the time that is the integer number of milliseconds away from the time term. Thus, the value of 115815T + 350000 is 120405T. If the resulting value
results in an invalid time, a run-time error will occur.
Technically the plus operator used with a date or a time term is not an example of an arithmetic operator, since the result is not numeric. However, we have covered it here for convenience. The chart below reviews this information.
Note that the left column indicates the type of the term preceding the plus operator, while the top row indicates the type of the term following the plus operator.
Plus (+) Char Option Integer Decimal Date Time Char Char Integer Integer Decimal N/A N/A
Option Integer Integer Integer Decimal N/A N/A
Integer Integer Integer Integer Decimal N/A N/A
Decimal Decima l Decima l Decima l
Decimal N/A N/A
Date Date Date Date N/A N/A N/A
Time Time Time Time N/A N/A N/A
Remember that if a result would normally be a char but the value is not a valid char value, the result type changes to integer. If a result would normally be an integer but the value is not a valid integer value, the result type changes to decimal.
The Minus Operator (-)
Like the plus operator, the minus operator can be used as either a binary operator or a unary operator. When used as a unary operator, its function is to change the sign of the term following it.
When used as a binary operator, its function is to subtract the term
following it from the term preceding it. Both terms can be numeric, both can be a date or a time, or the preceding term can be a Date or a Time, while the following term is an integer.
If the first term is a date and the second is an integer, the result is the date that is the integer number of days before the date term. Thus, the value of
02252001D - 7 is 02182001D. If the resulting value results in an
invalid date, a run-time error will occur.
If the first term is a time and the second is an integer, the result is the time that is the integer number of milliseconds before the time term. Thus, the value of 115815T - 350000 is 115225T. If the resulting value results in
If one date is subtracted from another, the result is the integer number of days between the two dates. If one time is subtracted from another, the result is the integer number of milliseconds between the two times.
The following chart summarizes the result types when the minus operator is used on various term types. Note that the left column indicates the type of the term preceding the minus operator, while the top row indicates the type of the term following the minus operator:
Minus (-) Char Option Integer Decimal Date Time Char Char Integer Integer Decimal N/A N/A
Option Integer Integer Integer Decimal N/A N/A
Integer Integer Integer Integer Decimal N/A N/A
Decimal Decimal Decimal Decimal Decimal N/A N/A
Date Date Date Date N/A Integer N/A
Time Time Time Time N/A N/A Integer
Remember that if a result would normally be a char but the value is not a valid char value, the result type changes to integer. If a result would normally be an integer but the value is not a valid integer value, the result type changes to decimal.
The Times Operator (*)
The times operator (or the multiplication operator) is only used as a binary operator. Its function is to multiply the numeric term preceding it by the numeric term following it.
The following chart summarizes the result types when the times operator is used on various term types:
Times (*) Char Option Integer Decimal Char Char Integer Integer Decimal
Option Integer Integer Integer Decimal
Integer Integer Integer Integer Decimal
The normal automatic conversion rules, from char to integer to decimal, apply.
The Divide Operator (/)
The divide operator is only used as a binary operator. Its function is to divide the numeric term preceding it by the numeric term following it. The result type of this division is always decimal. If the second term is zero (0), the result is a run-time error.
The Integer Divide Operator (DIV)
The integer divide operator is also only used as a binary operator. Its function is to divide the numeric term preceding it by the numeric term following it. The result type of this division is always integer. If the second term is zero (0), the result is a run-time error. Any decimals that would have resulted from an ordinary division are dropped, not rounded. Thus, the result of 17 DIV 8 is 2, while the result of 17 DIV 9 is 1.
The Modulus Operator (MOD)
The modulus operator requires two numbers. The first number is the one that is converted using the modulus function and the second number. The second number represents the number system being used. By definition, the number system starts at zero and ends at the second number minus 1. For example, if the second number were 10, then the number system used would be from 0 to 9. So, the modulus represents what the first number would convert to if your numbering system only had the number of values indicated by the second number, and then was forced to restart at zero. Here are some examples:
15 modulus 10 is 5 (since 9 is the last number available, 10 is represented by going back to the start, or zero, 11 is 1, 12 is 2, etc.).
· · · · 6 Modulus 10 is 6 10 Modulus 10 is 0 127 Modulus 10 is 7
Notice that you would get the same result if you divided the first number by the second using integers only and returned the remainder as the value.
The modulus operator (or the remainder operator) is only used as a binary operator. Its function is to divide the numeric term preceding it by the numeric term following it using the integer division method outlined above and then return the remainder of that division. The result type of this operation is always an integer. If the second term is zero (0), the result is a run-time error.
17 MOD 8 = 1
·
· 17 MOD 9 = 8. Exercise 7 Arithmetic Operator
1 Go into the Object Designer, select codeunit 95100 and click Design. 2 Add the following global variables to the current variable list, by
selecting View, C/AL Globals on the Menu Bar:
Int1 Integer Int2 Integer Amt1 Decimal Amt2 Decimal
3 In the OnRun trigger, enter the following statements, after removing any code that was in that trigger from previous lessons.
Int1 := 25 DIV 3; Int2 := 25 MOD 3;
LoopNo := Int1 * 3 + Int2;
MESSAGE('The value of %1 is %2','LoopNo',LoopNo); Amt1 := 25 / 3;
Amt2 := 0.00000000000000001;
Amount := (Amt1 - Int1) * 3 + Amt2;
MESSAGE('The value of %1 is %2','Amount',Amount);
4 Now exit, save and Run the codeunit. The results you should get are that the LoopNo is 25 and Amount is 1.