Arithmetic operation rules: basic operators
The basic operators (addition, subtraction, multiplication, and division) operate on numbers as follows.
Addition and subtraction
If either number
is 0, the other number, rounded to NUMERIC DIGITS
digits, if necessary, is used as the result (with sign adjustment
as appropriate). Otherwise, the two numbers are extended on the right
and left as necessary, up to a total maximum of DIGITS + 1 digits
(the number with the smaller absolute value may, therefore, lose some
or all of its digits on the right) and are then added or subtracted
as appropriate.
- Example
-
xxx.xxx + yy.yyyyybecomes:
The result is then rounded to the current setting of NUMERIC DIGITS if necessary (taking into account any extraxxx.xxx00 + 0yy.yyyyy ------------- zzz.zzzzzcarry digit
on the left after addition, but otherwise counting from the position corresponding to the most significant digit of the terms being added or subtracted). Finally, any insignificant leading zeros are removed.
The prefix operators are evaluated
using the same rules; the operations +number and -number are
calculated as 0+number and 0-number,
respectively.
Multiplication
The numbers are multiplied together (long multiplication
) resulting in a number that can
be as long as the sum of the lengths of the two operands.
- Example
-
xxx.xxx * yy.yyyyybecomes:zzzzz.zzzzzzzz
The result is then rounded, counting from the first significant digit of the result, to the current setting of NUMERIC DIGITS.
Division
yyy / xxxxx
First the number yyy is extended with zeros on the right until it is larger than
the number xxxxx (with note being taken of the change in the power of ten that this
implies). Thus, in this example, yyy might become yyy00.
Traditional long division then takes place. This might be written:
zzzz
+---------
xxxxx | yyy00 The length of the
result (zzzz) is such that the rightmost z is at least as far
right as the rightmost digit of the (extended) y number in the example. During the
division, the y number is extended further as necessary. The z
number may increase up to NUMERIC DIGITS+1 digits, at which point the division stops and the result
is rounded. Following completion of the division (and rounding if necessary), insignificant trailing
zeros are removed.Examples of basic operators
/* With: Numeric digits 5 */
12+7.00 -> 19.00
1.3-1.07 -> 0.23
1.3-2.07 -> -0.77
1.20*3 -> 3.60
7*3 -> 21
0.9*0.8 -> 0.72
1/3 -> 0.33333
2/3 -> 0.66667
5/2 -> 2.5
1/10 -> 0.1
12/12 -> 1
8.0/2 -> 4