|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Mathematical and algorithmic operations > Mathematical and algorithmic operations |
CAX. Command
Extended Scientific Calculations
Intention
The CAX. command allows you to assign a variable the result of a powerful mathematical and logical calculation. It functions as a full-featured scientific calculator with many advanced features. It also includes Link to the MCP-Server means it can interact with KI Agents.
Key Features:
• Uses extended precision (up to 18 digits) for high accuracy.
• Supports decimal, scientific (E), hexadecimal (&H), octal (&O), and binary (&B) number formats.
• Comprehensive library of functions covering trigonometry, statistics, number theory, and more.
• Advanced bitwise logic functions (AND, OR, XOR, NOT, ROL, ROR) for arbitrary-size integers.
• Extensive set of postfix unit conversions (e.g., Meter_TO_foot, Celsius_TO_Fahrenheit).
• Handles complex, nested expressions with correct operator precedence.
• Calculates factorials up to 1754! without overflow.
• Fastpath for BIT-Calculation with a String-Result. Due to the seaparate Logic that is separate from the rest of the Evaluator,
this is not limited to 64 Bit but can calculate on near to unlimited length. You can use it to calculate lengths of 256 Bits.
The limitation is here that it must be the most outer operation.
• Special Function for use with the MCP-Server: MEM(key)
Syntax
CAX.P1=[expression][|P2]
P1: Variable to store the result.
[expression]: The mathematical formula, including numbers, variables, operators, and functions.
P2: Optional flags. Use |# to enable degree mode for trigonometric functions.
Operator Precedence
Operators are evaluated from highest precedence (1) to lowest (11).
Level |
Operators |
1 (Highest) |
( ) (Grouping) |
2 |
! (Postfix Factorial), Unit Conversions |
3 |
^, ** (Exponentiation) |
4 |
+, - (Unary Sign), NOT |
5 |
*, /, \ (Multiplication, Division) |
6 |
MOD (Modulo) |
7 |
+, - (Addition, Subtraction) |
8 |
=, <>, >, <, >=, <= (Relational) |
9 |
AND |
10 |
OR, XOR |
11 (Lowest) |
EQV, IMP |
Detailed Operator Reference
Arithmetic Operators
+ (Addition): Adds two numbers. Sample: CAX.R=10+5.5
- (Subtraction): Subtracts the right number from the left. Sample: CAX.R=20-8
* (Multiplication): Multiplies two numbers. Sample: CAX.R=7*6
/ (Division): Performs floating-point division. Sample: CAX.R=15/2 (Result: 7.5)
\ (Integer Division): Performs division and discards the remainder. Sample: CAX.R=15\2 (Result: 7)
^ or ** (Exponentiation): Raises a number to a power. Sample: CAX.R=2^10
MOD (Modulo): Returns the integer remainder of a division. Sample: CAX.R=10 MOD 3 (Result: 1)
! (Factorial): Calculates the factorial of a non-negative integer. Must appear after the number. Sample: CAX.R=5! (Result: 120)
Relational Operators
These operators compare two values and return -1 for TRUE or 0 for FALSE.
= (Equal): Checks if two values are equal. Sample: CAX.R=(5*2)=10 (Result: -1)
<> (Not Equal): Checks if two values are not equal. Aliases: !=, ><. Sample: CAX.R=10<>9 (Result: -1)
> (Greater Than): Sample: CAX.R=10>9 (Result: -1)
< (Less Than): Sample: CAX.R=10<9 (Result: 0)
>= (Greater Than or Equal): Alias: =>. Sample: CAX.R=10>=10 (Result: -1)
<= (Less Than or Equal): Alias: =<. Sample: CAX.R=9<=10 (Result: -1)
Logical (Boolean) Operators
These operators work with TRUE (-1) and FALSE (0) values, often from relational comparisons.
NOT: Inverts a boolean value. Sample: CAX.R=NOT (5>10) (Result: -1, as 5>10 is FALSE)
AND: Returns TRUE if both operands are TRUE. Sample: CAX.R=(5>1) AND (10<20) (Result: -1)
OR: Returns TRUE if at least one operand is TRUE. Sample: CAX.R=(5>1) OR (10>20) (Result: -1)
XOR: Returns TRUE if operands are different. Sample: CAX.R=(-1) XOR (-1) (Result: 0)
EQV: Returns TRUE if operands are the same. Sample: CAX.R=(-1) EQV (5>1) (Result: -1)
IMP: Logical implication. A IMP B is equivalent to (NOT A) OR B. Sample: CAX.R=(-1) IMP (0) (Result: 0)
Detailed Function Reference
Trigonometric & Hyperbolic Functions
Standard trigonometric functions operate based on the angle mode (Radians by default, or Degrees if the |# flag is used). Inverse functions return a value in radians, which can be converted with the DEGREES() function.
SIN(angle): Calculates the sine of an angle.
CAX.$$RES=SIN(30)|#
COS(angle): Calculates the cosine of an angle.
CAX.$$RES=COS(RADIANS(60))
TAN(angle): Calculates the tangent of an angle.
CAX.$$RES=TAN(45)|#
ATN(number): Calculates the arctangent (inverse tangent). Alias: ARCTAN.
CAX.$$RES=DEGREES(ATN(1))
ATAN2(y, x): A two-argument arctangent that returns the correct angle based on the signs of y and x. Alias: ATN2.
CAX.$$RES=DEGREES(ATAN2(-5, -5))
ARCSIN(n), ARCCOS(n): Inverse sine and cosine.
CAX.$$RES=DEGREES(ARCCOS(-1))
SEC(a), CSC(a), COT(a): Secant, cosecant, and cotangent.
CAX.$$RES=SEC(60)|#
ARCSEC(n), ARCCSC(n), ARCCOT(n): Inverse secant, cosecant, and cotangent.
CAX.$$RES=DEGREES(ARCCOT(1))
SINH(n), COSH(n), TANH(n): Hyperbolic sine, cosine, and tangent.
CAX.$$RES=COSH(0)
ARCSINH(n), ARCCOSH(n), ...: All inverse hyperbolic functions are supported.
CAX.$$RES=ARCSINH(1)
Logarithmic & Exponential Functions
EXP(number): Returns e (approx. 2.718) raised to the specified power.
CAX.$$RES=EXP(1)
LOG(number): Calculates the natural logarithm (base e). Alias: LN.
CAX.$$RES=LOG(E#)
LOG(number, base): Calculates the logarithm of a number for a specified base.
CAX.$$RES=LOG(1000, 10)
CLG(number): Calculates the common logarithm (base 10). Alias: LOG10.
CAX.$$RES=CLG(100)
TLG(number): Calculates the binary logarithm (base 2). Alias: LOG2.
CAX.$$RES=TLG(256)
POW(base, exponent): Raises a base to a specified exponent (same as ^ operator).
CAX.$$RES=POW(5, 3)
SQR(number): Calculates the square root. Alias: SQRT.
CAX.$$RES=SQR(81)
Numeric, Rounding & Number Theory Functions
ABS(number): Returns the absolute (non-negative) value.
CAX.$$RES=ABS(-99.5)
SGN(number): Returns the sign of a number (-1, 0, or 1).
CAX.$$RES=SGN(-0.005)
CEIL(number): Rounds up to the nearest integer.
CAX.$$RES=CEIL(9.01)
FLOOR(number): Rounds down to the nearest integer.
CAX.$$RES=FLOOR(9.99)
FIX(number): Truncates the decimal part.
CAX.$$RES=FIX(-2.8)
FRAC(number): Returns the fractional part of a number.
CAX.$$RES=FRAC(123.456)
ROUND(number, places): Rounds a number to a specified number of decimal places.
CAX.$$RES=ROUND(PI#, 4)
RND() or RND(max): Generates a random number between 0 and 1, or between 0 and max.
CAX.$$RES=RND(100)
GCD(a, b): Calculates the Greatest Common Divisor of two integers.
CAX.$$RES=GCD(54, 24)
LCM(a, b): Calculates the Least Common Multiple of two integers.
CAX.$$RES=LCM(21, 6)
ISPRIME(number): Returns -1 (TRUE) if the integer is a prime number, otherwise 0 (FALSE).
CAX.$$RES=ISPRIME(29)
Statistical & Variadic Functions
These functions can accept one or more comma-separated arguments.
MAX(n1, n2, ...): Returns the largest value from a list.
CAX.$$RES=MAX(10, 50, -2, 12)
MIN(n1, n2, ...): Returns the smallest value from a list.
CAX.$$RES=MIN(10, 50, -2, 12)
SUM(n1, n2, ...): Calculates the sum of all values in a list.
CAX.$$RES=SUM(1, 2, 3, 4, 5)
AVERAGE(n1, n2, ...): Calculates the arithmetic mean of the values.
CAX.$$RES=AVERAGE(10, 15, 20)
COUNT(n1, n2, ...): Counts the number of arguments provided.
CAX.$$RES=COUNT(5, 10, 15, 20)
STDEV(n1, n2, ...): Calculates the sample standard deviation (uses n-1). Alias: VAR.
CAX.$$RES=STDEV(10, 12, 23, 23, 16, 23, 21, 16)
STDEVP(n1, n2, ...): Calculates the population standard deviation (uses n). Alias: VARP.
CAX.$$RES=STDEVP(10, 12, 23, 23, 16, 23, 21, 16)
Combinatorics Functions
NCR(n, k): Calculates combinations ("n choose k"). Aliases: COMB, C.
CAX.$$RES=NCR(10, 3)
NPR(n, k): Calculates permutations. Aliases: PERM, P.
CAX.$$RES=NPR(10, 3)
Advanced Functions
DEGREES(radians): Converts an angle from radians to degrees.
CAX.$$RES=DEGREES(PI#)
RADIANS(degrees): Converts an angle from degrees to radians.
CAX.$$RES=RADIANS(180)
HEX(number): Converts an integer to a hexadecimal string. Returns a string result.
CAX.$$S=HEX(255)
OCT(number): Converts an integer to an octal string. Returns a string result.
CAX.$$S=OCT(64)
BIN(number): Converts an integer to a binary string. Returns a string result.
CAX.$$S=BIN(10)
Special Function for use with the MCP-Server:
MEM(key): Retrieves a numeric value from an external key-value store using a numeric key.
CAX.$$RES=MEM(101)
Bitwise Operations
The calculator has two systems for bitwise operations. Arguments can be decimal, hexadecimal (&H), octal (&O), or binary (&B).
Numeric Bitwise Shifts (Return a Number)
SHL(value, bits): Logically shifts bits to the left.
CAX.$$RES=SHL(&B1010, 2) (Result: 40, which is &B101000)
SHR(value, bits): Logically shifts bits to the right (fills with zeros).
CAX.$$RES=SHR(40, 2) (Result: 10)
SAR(value, bits): Arithmetically shifts bits to the right (preserves sign bit).
CAX.$$RES=SAR(-16, 2) (Result: -4)
String-Based Bitwise Logic (Return a Hex String)
These functions perform bitwise operations and always return a string containing the result in hexadecimal format (e.g., "RES:FF"). They can be used as top-level commands for bitwise calculations.
BITAND(x, y): Performs a bitwise AND.
CAX.$$S=BITAND(&HF0, &H3C) (Result: "RES:30")
BITOR(x, y): Performs a bitwise OR.
CAX.$$S=BITOR(&HF0, &H0F) (Result: "RES:FF")
BITXOR(x, y): Performs a bitwise XOR.
CAX.$$S=BITXOR(&B1100, &B1010) (Result: "RES:6")
BITNOT(x): Performs a bitwise NOT (one's complement). Default is 64-bit.
CAX.$$S=BITNOT(0) (Result: "RES:FFFFFFFFFFFFFFFF")
ROL(value, bits): Rotates bits to the left.
CAX.$$S=ROL(&H1234, 4) (Result: "RES:2341")
ROR(value, bits): Rotates bits to the right.
CAX.$$S=ROR(&H1234, 4) (Result: "RES:4123")
Constants and Unit Conversions
Commonly used constants include PI# (or PI) and E# (or E). An extensive library of scientific constants and unit conversions is also available.
Unit conversions are applied after a value, like a postfix operator. An optional asterisk (*) can be used for clarity.
CAX.$$RES=100 Celsius_TO_Fahrenheit
CAX.$$RES=5 * Mile_TO_kilometer
Length |
Area / Mass |
Volume / Power |
Other |
Foot_TO_meter Inch_TO_centimeter Kilometer_TO_mile Inch_TO_foot Yard_TO_meter Fathom_TO_meter Mile_TO_light-year Parsec_TO_light-year (and inverses) |
Square_ft_TO_square_m Square_in_TO_square_cm Hectare_TO_acre Pound_TO_kilogram Ton_(metric)_TO_Kilogram Ton_(US)_TO_Kilogram Ounce_(avoirdupois)_TO_gram Ounce_(troy)_TO_gram (and inverses) |
Gallon_(US_dry)_TO_liter Gallon_(US_liquid)_TO_liter Pint_TO_liter_(US_liquid) Cubic_ft_TO_cubic_m Horsepower_(elec.)_TO_watt Horsepower_(metric)_TO_watt BTU/hour_TO_watt Kilowatt_TO_watt (and inverses) |
Fahrenheit_TO_Celsius Celsius_TO_Kelvin Kph_TO_mph Knot_TO_mph Minute_TO_second Hour_TO_minute Day_TO_hour Psi_TO_pascal (and inverses) |
Constant |
Value |
Units |
|---|---|---|
Acceleration_of_gravity |
9.80665 |
m/sec² |
Light_speed_in_a_vacuum |
299792458 |
m/sec |
Sound_speed_at_STP |
331.4 |
m/sec |
Standard_atmosphere |
101300 |
nt/m² |
Earth_mass |
5.983E24 |
kg |
Gravitational_constant |
6.6726E-11 |
nt-m²/kg² |
Electron_rest_mass |
9.10938188E-31 |
kg |
Proton_rest_mass |
1.67262158E-27 |
kg |
Avogadro |
6.02214076E23 |
1/mole |
Boltzmann |
1.380649E-23 |
joule/K |
Planck_constant |
6.62606876E-34 |
joule-sec |
Error Handling
The result of a CAX. operation is stored in the special variable $$CAX, which is a prefixed string indicating the outcome:
• CVE:<data> - Successful calculation of a numeric value.
• RES:<string> - Successful operation returning a string (e.g., from HEX() or BITAND()).
• ERR:<message> - An error occurred. The assigned variable P1 will be 0.
Examples
Basic Arithmetic:
CAX.$$RES = ((2+3)*5)^2 - (7-4)^3 + 10/5
Trigonometry (in Degrees):
CAX.$$RES = SIN(90) + COS(0)|#
Statistical Functions:
CAX.$$RES = AVERAGE(100, 50*3, 200, SQR(25000))
Unit Conversion:
CAX.$$RES = 100 * Mile_TO_kilometer
Bitwise Operations:
CAX.$$STR = BITAND(&HF0F0, &H0FF0)
Complex Nested Formula:
CAX.$$RES = ROUND( (5! + 2.5) * Meter_TO_foot / 10 , 2)
Example Script
' ================================================================
' == Evaluator Regression Test Script
' == VERSION: 5.2 (Definitive Syntax and Output)
' == PURPOSE:
' == A definitive diagnostic for the CAX. command that includes
' == complex, nested formulas and all known historical failure points.
' ==
' == RETURNS:
' == Prints a report to the Robot output and displays a message box.
' ================================================================
' --- 1. Initialization ---
VAR.$$RPT=Test# | Formula | Expected | Actual | Status
VAR.$$IDX=0
'CAX.$$RES=SIN(90)|#
'MBX.$$RES
' --- 2. Test Execution ---
' --- Basic Arithmetic & Precedence ---
GSB.TestSimple|2+3*(4-1)|11
GSB.TestSimple|((10-2)*5)/4|10
' --- Negative Numbers & Unary Operators ---
GSB.TestSimple|-5+3|-2
GSB.TestSimple|10*-5|-50
GSB.TestSimple|10*(-5)|-50
' --- Scientific Notation ---
GSB.TestSimple|1e6+2|1000002
GSB.TestSimple|1e-3*2|0.002
GSB.TestSimple|1.23E-5|0.0000123
' --- Constants ---
GSB.TestSimple|pi|3.14159265359
GSB.TestSimple|e#|2.718281828459
GSB.TestSimple|5*e#|13.591409142295
' --- Core Functions (sqrt, logs) ---
GSB.TestSimple|sqrt(16)|4
GSB.TestSimple|log10(1000)|3
GSB.TestSimple|ln(e#)|1
GSB.TestSimple|pow(2,10)|1024
GSB.TestSimple|log(243,3)|5
GSB.TestSimple|max(1,10,-5,20,0)|20
' --- Trigonometry (with Degree option) ---
GSB.TestWithOpts|sin(90)|#|1
GSB.TestWithOpts|cos(180)|#|-1
GSB.TestWithOpts|tan(45)|#|1
' --- Combinatorics ---
GSB.TestSimple|ncr(5,2)|10
GSB.TestSimple|perm(5,2)|20
GSB.TestSimple|C(10,3)|120
' --- Expected Errors (Domain & Syntax) ---
GSB.TestForError|1/0|Division by zero
GSB.TestForError|sqrt(-1)|cannot be negative
GSB.TestForError|log(-10)|must be > 0
GSB.TestForError|BadFunc(5)|Unknown function name
' --- Finalization ---
GTO.Finish
' ================================================================
' == Subroutine: TestSimple
' ================================================================
:TestSimple
VAR.$$FRM=§§_01
VAR.$$EXP=§§_02
VAR.$$ACT=
VAR.$$STA=FAIL
CAX.$$ACT=$$FRM
$$TXT=Formula: $$FRM Result: $$ACT Expected: $$EXP
PRT.$$TXT
IVV.$$ACT=$$EXP
VAR.$$STA=PASS
EIF.
VAR.$$LNE=$$IDX|$$FRM|$$EXP|$$ACT|$$STA
VAR.$$RPT=$$RPT$crlf$$$LNE
VIC.$$IDX
RET.
' ================================================================
' == Subroutine: TestWithOpts
' ================================================================
:TestWithOpts
VAR.$$FRM=§§_01
VAR.$$OPT=§§_02
VAR.$$EXP=§§_03
VAR.$$ACT=
VAR.$$STA=FAIL
CAX.$$ACT=$$FRM|$$OPT
$$TXT=Formula: $$FRM->$$OPT Result: $$ACT Expected: $$EXP
PRT.$$TXT
IVV.$$ACT=$$EXP
VAR.$$STA=PASS
EIF.
VAR.$$LNE=$$IDX|$$FRM (opts: $$OPT)|$$EXP|$$ACT|$$STA
VAR.$$RPT=$$RPT$crlf$$$LNE
VIC.$$IDX
RET.
' ================================================================
' == Subroutine: TestForError
' ================================================================
:TestForError
VAR.$$FRM=§§_01
VAR.$$EER=§§_02
VAR.$$ACT=
VAR.$$STA=FAIL
CAX.$$ACT=$$FRM
IVC.$$ACT=$$EER
VAR.$$STA=PASS
EIF.
VAR.$$LNE=$$IDX|$$FRM|Expected: '$$EER'|$$ACT|$$STA
VAR.$$RPT=$$RPT$crlf$$$LNE
VIC.$$IDX
RET.
' ================================================================
' == Finish: Print the report and end the script.
' ================================================================
:Finish
PRT.$$RPT
MBX.!
ENR.