Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The math library provides a number of useful math functions as well as special functions for plane-angle conversions, digests, trigonometry, and great-circle calculationsBase64, and hashing and digests. More are added as needed.  

General Functions

random

random(<num>) - generate a random number. The value is between 0 and the number given as an argument.

Code Block
themeConfluence
langjavascript
  r = math:random(999)

The given example will return random digits between 0 and 3 inclusive:

Code Block
themeConfluence
langjavascript
rule random_example {
  select when pageview ".*"
  pre{  
    r = math:random(3);
  }
  {
    notify("Random Number","r = #{r}") with sticky = true;
  }
}

exponent

exp(<num>) - return e (natural logrithm) raised to the <num>th power

Code Block
themeConfluence
langjavascript
 
     n = math:exp(2);

int

int(<num>) - the integer value of <num>

Code Block
themeConfluence
langjavascript
 
     n = math:int(2.999);

To make your own contributions, see the page Pico Engine welcoming your contributions for step-by-step guidance.

Table of Contents
maxLevel6

General Functions

absolute value

abs(<num>) - absolute value of <num>

Code Block
themeConfluence
langjavascript
 
     n = math:abs(-12);

logarithm

log(<num>) - natural log of <num>

Code Block
themeConfluence
langjavascript
     // n = math:log(2);12

...

ceiling

sqrtceiling(<num>) - square root of the smallest integer not less than <num>

Code Block
themeConfluence
langjavascript
n = math:sqrt(2);

power

power(<num1>,<num1>) - <num1> to the <num2>th power.

code
languagejavascript
themeConfluence
languagejavascript
 n = math:powerceiling(2,3.78);    //  n = 83

...

floor

ceilingfloor(<num>) - the smallest largest integer not less greater than <num>

Code Block
languagejavascript
themeConfluence
languagejavascript
n = math:ceilingfloor(2.78);    // n = 32

...

int

floorint(<num>) - the largest integer not greater than value of <num>

Code Block
themeConfluence
languagelangjavascript
 
     n = math:floorint(2.78999);     // n = 2

Only numbers are valid arguments to int. To coerce a string, use the Universal operator as()

random

To obtain a random number, please use a function from the random library

round

round(<num>) - convert <num> to the nearest integer.

javascript
Code Block
themeConfluence
language
n = math:round(2.1);    // n = 2
m = math:round(2.7);    // nm = 3

 

Plane Angle Conversions

deg2rad

deg2rad(<num>) - convert degrees to radians

Code Block
themeConfluence
langjavascript
 
     r = math:deg2rad(d);

grad2rad

grad2rad(<num>) - convert gradians to radians

Code Block
themeConfluence
langjavascript
 
     r = math:grad2rad(g);

rad2deg

rad2deg(<num>) - convert radians to degrees

Code Block
themeConfluence
langjavascript
 
     d = math:rad2deg(r);

grad2deg

grad2deg(<num>) - convert gradians to degrees

Code Block
themeConfluence
langjavascript
 
     d = math:grad2deg(g);

deg2grad

deg2grad(<num>) - convert degrees to gradians

Code Block
themeConfluence
langjavascript
 
     g = math:deg2grad(d);

rad2grad

rad2grad(<num>) - convert radians to gradians

Code Block
themeConfluence
langjavascript
 
     g = math:rad2grad(r);

Digest functions

md5

md5(<str>) - calculate the hex string representing the 16 byte md5 digest of the argument string.

Code Block
themeConfluence
langjavascript
  d = math:md5("Hello" + foo)

sha1

sha1(<str>) - calculate the hex string representing the 20 byte SHA1 digest of the argument string.

Code Block
themeConfluence
langjavascript
  d = math:sha1("Hello" + foo)

hmac_sha1

hmac_sha1(<str>, <key>) - calculate the hmac signature of a string with a key, encoded as a binary string.

Code Block
themeConfluence
langjavascript
  sig = math:hmac_sha1("Hello" + foo, "mykey")

hmac_sha1_hex

...

round can take an optional second argument, the number of decimal places, which extends to negative values in an intuitive way.

Code Block
math:round(4066.123,2)   // 4066.12
math:round(4066.123,0)   // 4066
math:round(4066.123,-2)  // 4100

Base64 Functions

math:base64encode(str, [encoding])

Given a string, str of type [encoding], return the base64 string. If the encoding is omitted, it defaults to utf8. 

Code Block
languagejs
themeConfluence
math:base64encode(payload,"hex")

math:base64decode(str,[encoding])

Decode the given base64 string, str returning a value with a type of [ encoding ] . If the encoding is omitted, it defaults to utf8. 

Code Block
languagejs
themeConfluence
math:base64decode(payload,"hex")

Hashing and Digest Functions

math:hashAlgorithms

An array of available hash algorithm names (strings)

Code Block
themeConfluence
langjavascript
  sig = math:hmac_sha1_hex("Hello" + foo, "mykey")

hmac_sha1_base64

hmac_sha1_base64(<str>, <key>) - calculate the hmac signature of a string with a key, encoded as base64.

Code Block
themeConfluence
langjavascript
  sig = math:hmac_sha1_base64("Hello" + foo, "mykey")

hmac_sha256

hmac_sha256(<str>, <key>) - calculate the hmac signature of a string with a key, encoded as a binary string.

Code Block
themeConfluence
langjavascript
  sig = math:hmac_sha256("Hello" + foo, "mykey")

hmac_sha256_hex

...

hashAlgorithms.klog() // ["sha256", "sha512", "DSA", ...]

math:hash(algorithm, str [, encoding])

Return the hash for the str using the given algorithm. Optionally specify the encoding "hex" (default) and "base64"

Code Block
themeConfluence
langjavascript
  sig = math:hmac_sha256_hexhash("Hellosha256" + foo, "mykeyhello") 

hmac_sha256_base64

hmac_sha256_base64(<str>, <key>) - calculate the hmac signature of a string with a key, encoded as base64.

Code Block
themeConfluence
langjavascript
  sig = math:hmac_sha256_base64("Hello" + foo, "mykey")

Trigonometry

Trig functions expect radians as arguments unless otherwise noted

Basic Trig

tan(<num>) - calculate the tangent of <num>

Code Block
themeConfluence
langjavascript
  d = math:tan(r)

sin(<num>) - calculate the sine of <num>

Code Block
themeConfluence
langjavascript
  d = math:sin(r)

cos(<num>) - calculate the cosine of <num>

Code Block
themeConfluence
langjavascript
  d = math:cos(r)

Trig Co-functions

cot(<num>) - calculate the co-tangent of <num>

Code Block
themeConfluence
langjavascript
  d = math:cot(r)

sec(<num>) - calculate the secant of <num>

Code Block
themeConfluence
langjavascript
  d = math:sec(r)

csc(<num>) - calculate the co-secant of <num>

// "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
 math:hash("sha256", "hello", "base64") // "LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ="

math:hmac(algorithm, key, message [, encoding])

Return an hmac string of message from the given hash algorithm and secret key. Optionally specify the encoding "hex" (default) and "base64"

Code Block
themeConfluence
langjavascript
  d = math:csc(r)

Arcus functions

atan(<num>) - calculate the arctangent of <num>

Code Block
themeConfluence
langjavascript
  d = math:atan(r)

asin(<num>) - calculate the arcsine of <num>

Code Block
themeConfluence
langjavascript
  d = math:asin(r)

acos(<num>) - calculate the arccosine of <num>

Code Block
themeConfluence
langjavascript
  d = math:acos(r)

Arcus Co-functions

acot(<num>) - calculate the arc co-tangent of <num>

Code Block
themeConfluence
langjavascript
  d = math:acot(r)

asec(<num>) - calculate the arc secant of <num>

Code Block
themeConfluence
langjavascript
  d = math:asec(r)

acsc(<num>) - calculate the arc co-secant of <num>

Code Block
themeConfluence
langjavascript
  d = math:acsc(r)

Hyperbolic functions

...

hmac("sha256", "a secret", "some message") // "86de43245b44531ac38b6a6a691996287d932a8dea03bc69b193b90caf48ff53"
 math:hmac("sha256", "a secret", "some message", "base64") // "ht5DJFtEUxrDi2pqaRmWKH2TKo3qA7xpsZO5DK9I/1M="

Planned but not yet implemented

Note
titleEngine Compatibility

Everything after this note is not supported at this time.

exponent

exp(<num>) - return e (natural logrithm) raised to the <num>th power

Code Block
themeConfluence
langjavascript
  d = math:tanh(r) 

sinh(<num>) - calculate the hyperbolic sine of <num>

Code Block
themeConfluence
langjavascript
  dn = math:sinhexp(r2);

cosh(<num>) - calculate the hyperbolic cosine of <num>

Code Block
themeConfluence
langjavascript
  d = math:cosh(r)

Hyperbolic Co-functions

...

logarithm

log(<num>) - calculate the hyperbolic co-tangent natural log of <num>

Code Block
themeConfluence
langjavascript
 n d = math:cothlog(r2);

sech(<num>) - calculate the hyperbolic secant of <num>

Code Block
themeConfluence
langjavascript
  d = math:sech(r)

...

square root

sqrt(<num>) - calculate the hyperbolic co-secant square root of <num>

Code Block
themeConfluence
langjavascript
  dn = math:cschsqrt(r2);

...

power

atanhpower(<num><num1>,<num1>) - calculate the hyperbolic arc tangent of <num><num1> to the <num2>th power.

Code Block
themeConfluence
langjavascript
  d = math:atanh(r)

asinh(<num>) - calculate the hyperbolic arc sine of <num>

code
languagejavascript
themeConfluencelangjavascript
  dn = math:asinh(r)

cosh(<num>) - calculate the hyperbolic arc cosine of <num>

Code Block
themeConfluence
langjavascript
  d = math:acosh(r)

acoth(<num>) - calculate the hyperbolic arc co-tangent of <num>

Code Block
themeConfluence
langjavascript
  d = math:acoth(r)

asech(<num>) - calculate the hyperbolic arc secant of <num>

Code Block
themeConfluence
langjavascript
  d = math:asech(r)

acsch(<num>) - calculate the hyperbolic arc co-secant of <num>

Code Block
themeConfluence
langjavascript
  d = math:acsch(r)

Great Circle Calculations

Great Circle Calculations are based upon a mathematical model, not geographical. Geographically, the zero axis for Latitude is located on the equator. The mathematical model places the zero axis for Latitude at the north pole.

The practical result of this model is that for geographical co-ordinates, the radian expression of the latitude must be subtracted from π/2. This will be demonstrated in the examples that follow the function definitions.

Distance

great_circle_distance(<long0>, π/2 - <lat0>, <long1>, π/2 - <lat1> |, r) - compute the distance along a great circle given two points on a spherical surface. r is an optional parameter for the radius of the sphere. If left unspecified, r defaults to 1-the unit sphere-and is expressed in radians.

Code Block
themeConfluence
langjavascript
r90   = math:pi()/2;      
rEk   = 6378;         // radius of the Earth in km

// point a
lata  = 40.4267290;
lnga  = -111.9025358;

// point b
latb  = 43.8310154;
lngb  = -111.7747790;

// convert co-ordinates to radians
rlata = math:deg2rad(lata);
rlnga = math:deg2rad(lnga);
rlatb = math:deg2rad(latb);
rlngb = math:deg2rad(lngb);

// distance between two co-ordinates in radians
dR = math:great_circle_distance(rlnga,r90 - rlata, rlngb,r90 - rlatb);

// distance between two co-ordinates in kilometers
dE = math:great_circle_distance(rlnga,r90 - rlata, rlngb,r90 - rlatb, rEk);

Direction

great_circle_direction(<long0>, π/2 - <lat0>, <long1>, π/2 - <lat1>) - compute the direction of the great circle that connects the two points note: the direction is not static. It changes for each position along the great circle. Thus, the great circle direction is also referred to as a bearing

Code Block
themeConfluence
langjavascript
// using the same co-ordinates as the great distance example
b = math:great_circle_direction(rlnga,r90 - rlata, rlngb,r90 - rlatb);

Mid Point

great_circle_midpoint(<long0>, π/2 - <lat0>, <long1>, π/2 - <lat1>) - compute the co-ordinates along the great circle that lay at the midpoint of two points

Code Block
themeConfluence
langjavascript
// using the same co-ordinates as the great distance example
b = math:great_circle_midpoint(rlnga,r90 - rlata, rlngb,r90 - rlatb);
lng = b[0];
lat = b[1];

Way Point

great_circle_waypoint(<long0>, π/2 - <lat0>, <long1>, π/2 - <lat1>,<f>) - compute the co-ordinates along the great circle that lay at fraction f of the distance between points

Code Block
themeConfluence
langjavascript
// using the same co-ordinates as the great distance example
// find waypoint 3/4ths of the distance along the great circle
b = math:great_circle_waypoint(rlnga,r90 - rlata, rlngb,r90 - rlatb,.75);
lng = b[0];
lat = b[1];

 

...

power(2,3);    //  n = 8

Other possibilities

See the page (Classic) math from the classic KRE.