<< Click to Display Table of Contents >> Navigation: 3. Script Language > Mathematical and algorithmic operations > Mathematical and algorithmic operations |
MiniRobotLanguage (MRL)
CAL. Command
mathematical CALculation
Intention
Solve mathematical calculations. With the CAL. Command, you can assign a Variable the result of a mathematical Calculation. And it can help you to prepare complicated decisions. Here is a simple example:
CAL.$$RES=3+9
MBX. The result of 3+9 is: $$RES
The CAL. command knows how to solve ( ..). You can also do this:
CAL.$$RES=3+(9*5)+(8+3/3)
MBX. The result is: $$RES
What is the result of this last calculation?
Is it 57 or is it 51.66666 ?
The correct answer is 57. Because calculations with * or / have precedence before + or -.
In fact this is calculated:
CAL.$$RES=3+(9*5)+(8+(3/3))
And that is the reason, why the correct result is 57. As you can see CAL. knows that.
It will calculate mathematically correct.Here are some more advantages of CAL.
• You can use as complicated formulas as you want.
• you can use as many nested brackets as you like.
• get extended precision in all calculation
• include decisions into your calculations
• custom rounding of results
• very fast (~10000 calculations per second)
CAL includes a mechanism to embed decisions into the formula. Here is an example:
: $$MAA=12
: $$MAB=9
' Calculate an unlimited complicated decision into just one result:
CAL.$$RES=($$MAA>9)+($$MAB<5)
' Here you get an result in $$RES that is 0,1 or 2
' Now you can make a decision:
IVV.$$RES=0
' Do something
ELS.
' Do something else
EIF.
CAL uses "Operator Precedence" and understands negative or positive signed numbers.
Syntax
CAL.P1=[N/V][Operator][...][|P2]
Parameter Explanation
[N/V] = Numerical Value or variable containing a numerical value
[Operator] = + - * / \ ^ %
+ Addition
- Subtraction
* Multiplication
/ Division
\ Integer-Division
% is the "Modulo Operator"
^ power
The result of these numeric operations is a always a Floating-Point Number.
The following Operators enable you to include decisions into the formula.
! unequal -Operator
= Equal-Operator
< smaller then
> greater then
The Result of these logical operation is always 0 or 1.
Unlike elsewhere, the Operators do NOT work binary, but on decision true (1) or false (0). See Examples.
P2 - optional Options. Using these Options, you can round or truncate the result of the calculation to an integer. Using this Options, you can use the CAL-Command to round one variable into another one.
CAL uses "Operator Precedence" as follows:
1. The logical Decisions: =!<> are calculated first with equal priority.
2. The ^ Operator is been calculated before any other numerical calculation.
3. The "*/\%" Operators are calculated with equal precedence each after the other next.
4. Finally the "+-" are calculated.
Custom rounding of results:
CAL. without any Option specified uses 80 bit Extended Precision for all Calculations.
Using the Options, you can manipulate the result of the operation:
- i (INTEGER)
- c (CEIL)
- x (FIXPOINT)
- f (FRACTIONAL)
- a (ABSOLUTE)
- r (ROUND) - makes a rounding to 2 decimal places (money ...)
- s (SIGN)
- t (SQR - Square Root)
- u (SIN)
- v (COS)
- w (LOG) - in this special case, the function checks internally, if the result is not zero. Only then the LOG will be calculated. Otherwise the result is zero.
In Detail:
- "r" Rounding is done according to the "banker's rounding" principle: if the fractional digit being rounded off is exactly five, with no trailing digits, the number is rounded to the nearest even number. This provides better results, on average, than the simple "round up at five" approach.
- "x" FIX strips off the fractional part of its argument, and returns the integer part. Unlike INT, FIX does not perform any form of rounding or scaling.
- "i" INT rounds numeric_expression to the largest integer-class value that is less than or equal to result.
- "c" The CEIL function rounds upward, returning the smallest integer value that is greater than or equal to result. For example, CEIL with 1.5 would places the value 2 into Result.
- "f" FRAC returns the number after the decimal point of a floating-point number or expression. FRAC rounds the result of fit the precision of the target h, as per IEEE specifications.
- "a" The absolute value of a number is its non-negative value. For example, the absolute value of -3 is 3, and the absolute value of +3 is also 3. The absolute value of 0 is 0.
- "s" If numeric_expression is positive, SGN returns 1. If numeric_expression is zero, SGN returns 0. If numeric_expression is negative, SGN returns -1.
Speed in Ticks:
This depends heavily on teh used Formula. A rather simple Formula with additional rounding to Integer uses around 260 Ticks.
Example
Eval-Example:
' The variable contains a formula now.
$$FOM=3*5+9
CAL.$$RES=$$FOM
VAR.$$BBB=0.5
CAL.$$AAA=123.5*$$BBB+0.9|iar
'Example 2:
' Please note that the second part ($$BBB+.5)*($$AAA=1)
' evaluates to zero, when $$AAA=0 because of the use of
' the "=" Operator.
VAR.$$BBB=-5
CAL.$$AAA=(3^-19)+($$BBB+.5)*($$AAA=1)
PRT.L-RESULT:$$AAA
CAL.(4^19)+($$BBB+.5)|i
POP.$$AAA
PRT.Q-RESULT:$$AAA
CAL.(4^19)+($$BBB+.5)
POP.$$AAA
PRT.RESULT:$$AAA
CAL.(4^19)+($$BBB+.5)|r
POP.$$AAA
PRT.RESULT:$$AAA
DMP.
END.
Remarks
Depending on complexity of the formula, the CAL.-Command can calculate several thousands of calculations each seconds. The typical speed is between 4000 to 10000 formulas per second on a 3.00 GHz machine.
Limitations:
Does it have limitations? Lets see ...
Doing this:
CAL.$$RES=3+(9*5)+((8+3)/3)
we get this result:
51.6666666666667
00.1234567890ABC
We have extended precision. As you can see, we get 12 significant numbers after the "." and a last one that is rounded. My other scientific calculator just gives me 7.
CAL. calculates Formulas of unlimited complexity, there is no Limitation in:
- total Length of the Formula
- How many () are used and nested.
other then the normal 2 GB Limit for total variable storage.
CAL works internally with Extended precision (FPU-Registers). They have a range of approximately +/- 3.4x10^-4932 to 1.2x10^4932, and offer between a total of 16-18 digits of precision.
See also:
• 3.5 Hexadezimal, Decimal and Binary and more