Mathematical and algorithmic operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Mathematical and algorithmic operations >

Mathematical and algorithmic operations

CAX. - extended Calculator

Previous Top Next


MiniRobotLanguage (MRL)

 

CAX. Command

extended scientific CALculations

 

 

Intention

 

Solve scientific mathematical calculations. With the CAX. Command, you can assign a Variable the result of a mathematical Calculation. Unlike CAL., CAX. is a full featured scientific calculator.

 

Here are some of the features:

    Using extended precision, 16 to 18 digits of precision is obtained.

    get extended precision in all calculation

    custom rounding of results

    fast (~500 calculations per second)

    in case of errors, Error-Messages on TOS

    Large set of trigonometric functions

    Large set of scientific constants and conversions already built in (see below).

    Input of angles can be in degrees or  radians.

    Trigonometric output using the ARC functions will always be in radians.

    You can transform angles to radians and vice versa using the built-in conversions.

    You can use as complicated formulas as you want.

    you can use as many nested brackets as you like.

    finally: Factorials can be calculated up to 1754! without overflow!

 

 

Supported Operators and expressions:

  +   -   *   /   \   ^   !   (   )   <   =   >   <>   >=   <=

 

 

Operator precedence:

The Operator Precedence follows as closely as possible the common standards.

The only exception is the unary negation operator which does not have a special priority

following exponentiation.

Operations inside parentheses always have the highest priority and are always evaluated first. Thus by using parentheses in the right places you can always obtain the operator precedence you need for your calculation.

 

The following operator precedence is used in the program:

 

1. parentheses ( )

2. unary operators (Factorials (!) )

3. one argument functions

4. exponentiation (^) (power expressions)

5. multiplication (*) and floating-point division (/)

6. integer division (\)

7. modulo (MOD)

8. addition (+), subtraction (-)

9. relational operators (<, <=, =, >=, >, <> )

10. NOT, ISFALSE and ISTRUE

11. AND

12. OR and XOR (exclusive OR)

13. EQV (equivalence)

14. IMP (implication)

 

Trigonometric functions:

 

 "ARCSINH() - Arc (Inverse) Hyperbolic Sine"

 "ARCCOSH() - Arc (Inverse) Hyperbolic Cosine"

 "ARCTANH() - Arc (Inverse) Hyperbolic Tangent"

 "ARCSECH() - Arc (Inverse) Hyperbolic Secant"

 "ARCCSCH() - Arc (Inverse) Hyperbolic Cosecant"

 "ARCCOTH() - Arc (Inverse) Hyperbolic Cotangent"

 "ARCSIN()  - Arc (Inverse) Sine"

 "ARCCOS()  - Arc (Inverse) Cosine"

 "ARCSEC()  - Arc (Inverse) Secant"

 "ARCCSC()  - Arc (Inverse) Cosecant"

 "ARCCOT()  - Arc (Inverse) Cotangent"

 "SINH()    - Hyperbolic Sine","TANH() - Hyperbolic Tangent"

 "SECH()    - Hyperbolic Secant"

 "CSCH()    - Hyperbolic Cosecant"

 "COTH()    - Hyperbolic Cotangent"

 "SEC()     - Secant"

 "CSC()     - Cosecant"

 "COT()     - Cotangent"

 "SIN()     - Sine","COS() - Cosine"

 "TAN()     - Tangent"

 "ATN()     - Arc (Inverse) Tangent"

 "LN()      - Natural Log (base E)"

 "EXP2()    - Exponentiation of 2 or Antilog 2"

 "EXP10()   - Exponentiation of 10 or Antilog 10"

 "EXP()     - Exponentiation of E or Antilog E"

 "SQR()     - Square Root","LOG10() - Common Log (base 10)"

 "ABS()     - Absolute Value"

 "LOG2()    - Log (base 2)"

 "LOG()     - Natural Log (base E)"

 

 

Logical Operations:

 

 "AND"       - Logical AND

 "XOR"       - Logical XOR (Exclusive OR)

 "OR"        - Logical OR

 "MOD"       - MOD (Modulo)

 "IMP"       - Logical IMP (Implication)

 "EQV"       - Logical EQV (Equivalence)

 "NOT"       - NOT

 "ISTRUE"    - Logical Truth

 "ISFALSE"   - Logical Falsity

 "CEIL()"    - Ceiling

 "FRAC()"    - Fractional Part

 "FIX()"     - Truncate To Integer

 "SGN()"     - Sign

 

Important:

While FALSE is always zero, Logical TRUTH is been flagged with -1.

 

 

Usage example:

 

: $$NUA=36

CAX.$$NUM=SIN($$NUA)+5

DBP.Result is $$NUM in Radians

 

: $$NUA=36

CAX.$$NUM=SIN($$NUA)+5|#

DBP.Result is $$NUM in Degree

 

 

 

 

 

 

Know Constants:

A number of constants and conversions are available, making this math evaluator an advanced scientific calculator.

 

Let us start with PI and Euler:

"PI#"                                                  3.14159..

"E#"                                                   2.71828..

 

Known Conversion Constants:

"Foot_TO_meter"                                    = "*1/ 3.28084"

"Meter_TO_foot"                                    , "* 3.28084"

"Inch_TO_centimeter"                               , "* 2.54"

"Centimeter_TO_inch"                               , "*1 / 2.54"

"Kilometer_TO_mile"                            , "*1 / 1.609344"

"Mile_TO_kilometer"                                , "* 1.609344"

"Inch_TO_foot"                                     , "*1 / 12"

"Foot_TO_inch"                                     , "* 12"

"Yard_TO_meter"                                , "*1 / 1.093613"

"Meter_TO_yard"                                    , "* 1.093613"

"Fathom_TO_meter"                                  , "* 1.8288"

"Meter_TO_fathom"                                  , "*1 / 1.8288"

"Mile_TO_light-year"                        , "*1 / 5880000000000"

"Light-year_TO_mile"                        , "* 5880000000000"

"Parsec_TO_light-year"                             , "* 3.261643"

"Light-year_TO_parsec"                      , "*1 / 3.261643"

"Square_ft_TO_square_m"                     , "*1 / 10.76391"

"Square_m_TO_square_ft"                            , "* 10.76391"

"Square_in_TO_square_cm"                           , "* 6.4516"

"Square_cm_TO_square_in"                           , "*1 / 6.4516"

"Hectare_TO_acre"                                  , "* 2.471054"

"Acre_TO_hectare"                           , "*1 / 2.471054"

"Pound_TO_kilogram"                         , "*1 / 2.204623"

"Kilogram_TO_pound"                                , "* 2.204623"

"Ton_(metric)_TO_Kilogram"                         , "* 1000"

"Kilogram_TO_ton_(metric)"                         , "*1 / 1000"

"Ton_(US)_TO_Kilogram"                             , "* 907.18474"

"Kilogram_TO_ton_(US)"                      , "*1 / 907.18474"

"Ton_(UK)_TO_Kilogram"                      , "* 1016.046909"

"Kilogram_TO_ton_(UK)"                      , "*1 / 1016.046909"

"Ounce_(avoirdupois)_TO_gram"                      , "* 28.349551"

"Gram_TO_ounce_(avoirdupois)"               , "*1 / 28.349551"

"Ounce_(troy)_TO_gram"                             , "* 31.103508"

"Gram_TO_ounce_(troy)"                      , "*1 / 31.103508"

"Fahrenheit_TO_Celsius"                    , "*1 / 1.8 - 32 / 1.8"

"Celsius_TO_Fahrenheit"                            , "* 1.8 + 32"

"Celsius_TO_Kelvin"                                , "*1 + 273.16"

"Kelvin_TO_Celsius"                                , "*1 - 273.16"

"Gallon_(US_dry)_TO_liter"                         , "* 4.404884"

"Liter_TO_gallon_(US_dry)"                 , "*1 / 4.404884"

"Gallon_(US_liquid)_TO_liter"                      , "* 3.785412"

"Liter_TO_gallon_(US_liquid)"              , "*1 / 3.785412"

"Quart_(US_dry)_TO_gallon_(US_dry)"                , "*1 / 4"

"Gallon_(US_dry)_TO_quart_(US_dry)"                , "* 4"

"Pint_TO_liter_(US_dry)"                           , "* 0.55061"

"Liter_TO_pint_(US_dry)"                        , "*1 / 0.55061"

"Pint_TO_liter_(US_liquid)"                        , "* 0.473176"

"Liter_TO_pint_(US_liquid)"                     , "*1 / 0.473176"

"Cubic_ft_TO_cubic_m"                           , "*1 / 35.314667"

"Cubic_m_TO_cubic_ft"                              , "* 35.314667"

"Horsepower_(elec.)_TO_watt"                       , "* 745.7"

"Watt_TO_horsepower_(elec.)"                       , "*1 / 745.7"

"Horsepower_(metric)_TO_watt"                      , "* 735.499"

"Watt_TO_horsepower_(metric)"                     , "*1 / 735.499"

"BTU/hour_TO_watt"                                 , "* 0.293071"

"Watt_TO_BTU/hour"                               , "*1 / 0.293071"

"Kilowatt_TO_watt"                                 , "* 1000"

"Watt_TO_kilowatt"                                 , "*1 / 1000"

"Kph_TO_mph"                                     , "*1 / 1.609344"

"Mph_TO_kph"                                       , "* 1.609344"

"Knot_TO_mph"                                      , "* 1.150778"

"Mph_TO_knot"                                    , "*1 / 1.150778"

"Meter/sec_TO_ft/sec"                              , "* 3.28084"

"Ft/sec_TO_meter/sec"                             , "*1 / 3.28084"

"Second_TO_minute"                                 , "*1 / 60"

"Minute_TO_second"                                 , "* 60"

"Minute_TO_hour"                                   , "*1 / 60"

"Hour_TO_minute"                                   , "* 60"

"Minute_TO_day"                                    , "*1 / 1440"

"Day_TO_minute"                                    , "* 1440"

"Day_TO_hour"                                      , "* 24"

"Hour_TO_day"                                      , "*1 / 24"

"Psi_TO_pascal"                                    , "* 6894.757"

"Pascal_TO_psi"                                 , "*1 / 6894.757"

"Psi_TO_atmosphere"                                , "* 0.068046"

"Atmosphere_TO_psi"                             , "*1 / 0.068046"

"Psi_TO_kg/sqcm"                                   , "* 0.070307"

"Kg/sqcm_TO_psi"                                , "*1 / 0.070307"

"Degrees_TO_radians"                  , "* 1.74532925199432958E-2"

"Radians_TO_degrees"               , "*1 / 1.74532925199432958E-2"      

 

Usage example:

 

GUT.How many hours?|||$$TXT

CAX.$$NUM=$$TXT Hour_TO_minute

DBP.These are $$NUM Minutes.

 

 

More known Conversions:

 

"Acceleration_of_gravity_(m/sec²)"                 , "9.80665"

"Dry_air_density_at_STP_(kg/m³)"                   , "1.293"

"Light_speed_in_a_vacuum_(m/sec)"                  , "299792458"

"Solar_constant_(watts/m²)"                        , "1340"

"Sound_speed_at_STP_(m/sec)"                       , "331.4"

"Standard_atmosphere_(nt/m²)"                      , "101300"

"Earth_equatorial_radius_(m)"                      , "6378000"

"Earth_escape_velcity_(m/sec)"                     , "11186"

"Earth_magnetic_dipole_moment_(amp-m²)"            , "6.400E21"

"Earth_mass_(kg)"                                  , "5.983E24"

"Earth_mean_angular_rotational_speed_(rads/sec)"   , ".0000729"

"Earth_mean_density_(kg/m³)"                       , "5522"

"Earth_mean_orbital_speed_(m/sec)"                 , "29770"

"Earth_polar_radius_(m)"                           , "6357000"

"Earth_volume_(m³)"                                , "1.087E21"

"Alpha_particle_mass_(kg)"                      , "6.64465598E-27"

"Electron_charge-mass_ratio_(coul/kg)"          , "175881961000"

"Electron_rest_mass_(kg)"                       , "9.10938188E-31"

"Muon_rest_mass_(kg)"                           , "1.88353109E-28"

"Neutron_rest_mass_(kg)"                        , "1.67492716E-27"

"Proton_rest_mass_(kg)"                         , "1.67262158E-27"

"Atomic_mass_constant_(kg)"                     , "1.66053873E-27"

"Avogadro_constant_(1/mole)"                    , "6.02214199E23"

"Boltzmann_constant_(joule/K)"                  , "1.3806503E-23"

"Elementary_charge_(coul)"                    , "1.602176462E-19"

"Faraday_constant_(coul/mole)"                , "96485.3415"

"Gravitational_constant_(nt-m²/kg²)"          , ".000000000066726"

"Permeability_constant_(henry/m)"             , "1.25663706143E-6"

"Permittivity_constant_(farad/m)"            , "8.85418781762E-12"

"Planck_constant_(joule-sec)"                , "6.62606876E-34"

"Rydberg_constant_(1/m)"                     , "10973731.568549"

"Stefan-Boltzmann_constant_(watt/m²K4)"      , ".000000056704"

"Universal_gas_constant_(joule/K-mole)"      , "8.314472"

"Wien_displacement_constant_(m-K)"           , ".00290"      

 

 

Input of angles can be in degrees or radians, depending if you specify the "#" as an additional flag. However, trigonometric output using the ARC functions will always be in radians. You can transform angles to radians and vice versa.

 

Here is a simple example:

 

CAX.$$RES=3+9

MBX. The result of 3+9 is: $$RES

 

 

In case of errors, the result will be zero and you will find an error-message on the TOS. Also the Timeout-Flag will be set to show you there was an error. Here is an example:

 

CAX.$$NUM=SIN((9)

DBP.Result is $$NUM

 

ITO.

 DBP.There was an Error:$$000

EIF.

ENR.

 

graphic

 

Important:

Expressions and functions should be entered strictly according to standard algebraic syntax rules.

 

 

 

Syntax

 

 

CAX.P1=[N/V][Operator][...][|P2]

 

 

Parameter Explanation

 

 

[N/V] = Numerical Value or variable containing a numerical value

[Operator] = + - * / \ ^ %

 

+ Addition

- Subtraction

* Multiplication

/ Division

\ is the "Modulo Operator"

^ power

! Factorial

 

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

<=  smaller then

>=  greater then

 

 

The Result of these logical operation is always 0 or -1.

 

Logical Operations:

 

 "AND"       - Logical AND

 "XOR"       - Logical XOR (Exclusive OR)

 "OR"        - Logical OR

 "MOD"       - MOD (Modulo)

 "IMP"       - Logical IMP (Implication)

 "EQV"       - Logical EQV (Equivalence)

 "NOT"       - NOT

 "ISTRUE"    - Logical Truth, anything else

               then 0 will result in -1

 "ISFALSE"   - Logical Falsity, only 0 will result in -1

 "CEIL()"    - Ceiling

 "FRAC()"    - Fractional Part

 "FIX()"     - Truncate To Integer

 "SGN()"     - Sign

 

Important:

While FALSE is always zero, Logical TRUTH is been flagged with -1. This is different to the CAL.-command which flags truth with 1. Example:

 

CAX.$$NUM=(5>1)

DBP.Result is $$NUM

' Result is -1

 

CAL.$$NUM=(5>1)

DBP.Result is $$NUM

' Result is 1

 

P2 - optional Rounding Options. Using these Options, you can round or truncate the result of the calculation to an integer in many way.

 

 

Custom rounding of results:

 

CAX. 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)

- # (Radian to Degree switch), if omitted Radian is default

 

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.

 

 

 

Example

 

 

'######################################

' CAX. - Examples

'######################################

'

CAX.$$NUM=PI#|c

DBP.Result is $$NUM

' Result is 4

'

CAX.$$NUM=PI#|i

DBP.Result is $$NUM

' Result is 3

'

CAX.$$NUM=SGN(-0.43)

DBP.Result is $$NUM

' Result is -1

'

CAX.$$NUM=ISFALSE(-0.43)

DBP.Result is $$NUM

' Result is 0

 

END.

 

 

 

 

Remarks

 

Depending on complexity of the formula, the CAX.-Command can calculate several hundreds of calculations each seconds. The typical speed is between 100 to 500 formulas per second on a 3.00 GHz machine.

 

Note that the MOD Operator (\,%) is different, and the result of logical operations are different  (-1/1) in CAX. and CAL. Therefore be careful when switching formulas over.

 

 

 

Limitations:

 

CAX 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

    CAL. - mathematical CALculation

    GEC. - Get Encryption

    GMD. - Get Message Digest

    RND. - RandDom Number

    VIC. - Variable Increment

    VDC. - Variable Decrement