# Mathematical Functions

Navigation: PGMs ➔ Functions ➔ Mathematical Functions

Functions Subroutines Classes
Defining a Function Predefined Functions Tag Functions Mathematical Functions String Functions Defining a Subroutine Trigger Subroutines Defining a Class

# Maths Functions

The PGM Language supports the mathematical functions in the table below.

Notes:

1. All function names are NOT case sensitive.
2. To use the negative of a declared variable in any function (where allowed), must enter -1 * variable (NOT -variable).
For example y = exp(-x) is NOT allowed, must use y = exp(-1 * x)
3. Angles in trigonometric functions are specified in units of radians, not degrees.
$\displaystyle{ \mathbf{\mathit{radians = \dfrac {180^{\circ}}{\pi}}} }$ (Please see Example 4 for sample conversion functions, and Radians on Wikipedia)
Function Syntax Description
Sin Sin(variable1) The Sin function returns the sine of variable1. variable1 is an angle in radians.
aSin aSin(variable1) The aSin function is the inverse of the sine function (arcsin). Returns an angle in radians.
Note: Valid range: -1 <= variable1 <= 1. If out of range, variable1 will be limited by the range, and math error will be given in the GControl.
Cos Cos(variable1) The Cos function returns the cosine of variable1. variable1 is an angle in radians.
aCos aCos(variable1) The aCos function is the inverse of the cosine function (arccos). Returns an angle in radians.
Note: Valid range: -1 <= variable1 <= 1. If out of range, variable1 will be limited by the range, and math error will be given in the GControl.
Tan Tan(variable1) The Tan function returns the tangent of variable1. variable1 is an angle in radians
aTan aTan(variable1) The aTan function is the inverse of the tangent function (arctan). Returns an angle in radians.
aTan2 aTan2(y1, y2, x1, x2) The aTan2 function returns the angle in radians, given by the two points (x1,y1) and (x2,y2). $\displaystyle{ =aTan\left(\frac{y_2-y_1}{x_2-x_1}\right) }$
Degrees Degrees(variable1) The Degrees function is used to convert radians to degrees. Returns an angle in degrees from variable1, an angle in radians.
Can be used to interpret results from the trigonometric functions above, if accustomed to working in degrees.
Radians Radians(variable1) The Radians function is used to convert degrees to radians. Returns an angle in radians from variable1, an angle in degrees.
Can be used as input for the trigonometric functions above, if accustomed to working in degrees.
Abs Abs(variable1) The Abs function returns the absolute value of variable1.
Sqrt Sqrt(variable1) The Sqrt function returns the square root of variable1.
Note: If variable1 is negative an error occurs as the operation is undefined.
Cbrt
Implemented in Build139.30807
Cbrt(variable1) The Cbrt function returns the cube root of variable1.
Exp Exp(variable1) The Exp function returns the exponential of variable1. $\displaystyle{ =e^{variable1} }$
Note: If using the negative of variable1, the correct syntax is: Exp(-1 * variable1), NOT Exp(- variable1).
Ln Ln(variable1) The Ln function returns the natural logarithm of variable1. $\displaystyle{ =log_e(variable1) }$
Note: If variable1 is zero or negative an error occurs as the operation is undefined and the function returns 0.
Log Log(variable1) The Log function returns the logarithm of variable1. $\displaystyle{ =log_{10}(variable1) }$
Note: If variable1 is zero or negative an error occurs as the operation is undefined and the function returns 0.
Pow Pow(variable1, variable2)
OR
variable1^variable2
The Pow function, raises variable1 to the power of variable2. Alternatively, the ^ operator can be used.
Notes:
1. If variable1 is negative and variable2 is not an integer an error occurs as the operation is undefined.
2. If the user wishes to use the negative of variable2, the correct syntax is either Pow(variable1, -1 * variable2) OR variable1^(-1 * variable2) , NOT Pow(variable1, -variable2)
Max Max(variable1, variable2) The Max function returns the maximum of variable1 and variable2.
Min Min(variable1, variable2) The Min function returns the minimum of variable1 and variable2.
Range Range(low_limit, variable1, high_limit) The Range function ranges variable1 between the two limits.
• If variable1 is less than low_limit, the function returns the value of low_limit.
• If variable1 is greater than high_limit, the function returns the value of high_limit.
• Otherwise, variable1 is returned unmodified.
Floor
Implemented in Build139.30599
Floor(variable1) The Floor function returns the highest integer value which is <= variable1. i.e. Rounds to the nearest integer in the negative direction.
Note: This is not the same as the Excel Floor function.
Ceil
Implemented in Build139.30599
Ceil(variable1) The Ceil (ceiling) function returns the lowest integer value which is >= variable1. i.e. Rounds to the nearest integer in the positive direction.
Trunc Trunc(variable1) The Trunc (truncate) function returns the integer part of variable1, without rounding.
Round Round(variable1) The Round function rounds variable1 to the nearest integer value.
RoundTo
Implemented in Build139.30807
RoundTo(variable1, decimals) The RoundTo function rounds variable1 to the nearest value with decimals decimal places. (i.e. to the nearest $\displaystyle{ 10^{-decimals} }$)

Note: decimals must be an integer. Its value can be positive (e.g. 2 = round to the nearest 0.01), zero (i.e. round to the nearest integer), or negative (e.g. -2 = round to the nearest 100).

RoundUp RoundUp(variable1, decimals) The RoundUp function rounds variable1 up to the nearest value with decimals decimal places. (i.e. up to the nearest $\displaystyle{ 10^{-decimals} }$)
• This will increase the absolute value of variable1 if rounding is required, moving the value away from zero. i.e. Positives will round up to a larger positive, while negatives will round "up" to a larger negative.

Note: decimals must be an integer. Its value can be positive (e.g. 2 = round up to the nearest 0.01), zero (i.e. round up to the nearest integer), or negative (e.g. -2 = round up to the nearest 100).

Mod Mod(numerator, denominator) The Mod (modulus) function, returns the remainder of numerator divided by denominator. The result and the parameters are real/double data types. See also Div.
Note: Division by zero will result in an error as it is undefined.
WARNING: This is equivalent to the C/C++ fmod function, where the remainder is the same sign as the numerator. Take care with negative arguments.
Div Div(numerator, denominator) The Div (integer division) function, returns the positive integer quotient of numerator divided by denominator. The result is a integer/long data type and the parameters may be real/double variable types. See also Mod.
Note: Division by zero will result in an error as it is undefined.
Erf Erf(variable1) The Erf function returns the the error function integrated between 0 and variable1.
The form of the function is:

$\displaystyle{ \operatorname{erf}(z) = \frac{2}{\sqrt{\Pi}}\int\limits_{0}^{z}e^{-t^2} dt\, }$

IsNAN IsNAN(variable1) The IsNAN function returns true if variable1 is NAN (not a number) otherwise it returns false. variable1 should be of type Real/Double.
IsClose
Implemented in Build139.31388
IsClose(variable1, variable2) The IsClose function returns true if the comparison between variable1 and variable2 using AbsTol and RelTol values of 1e-9 are considered to be "equal" (close enough for the given tolerance).
See IsCloseTolError for further information.
IsCloseTol
Implemented in Build139.31388
IsCloseTol(variable1, variable2, AbsTol, RelTol) The IsCloseTol function returns true if the comparison between variable1 and variable2 the supplied absolute and relative tolerances are considered to be "equal" (close enough for the given tolerance).
See IsCloseTolError for further information.
IsCloseError
Implemented in Build139.31388
IsCloseError(variable1, variable2) The IsCloseError function returns the error value for the comparison between variable1 and variable2 using AbsTol and RelTol values of 1e-9. If the returned error value is less than 1.0 the values are considered to be "equal" (close enough for the given tolerance).
See IsCloseTolError for further information.
IsCloseTolError
Implemented in Build139.31388
IsCloseTolError(variable1, variable2, AbsTol, RelTol) The IsCloseTolError function returns the error value for the comparison between variable1 and variable2 using the supplied absolute and relative tolerances. If the returned error value is less than 1.0 the values are considered to be "equal" (close enough for the given tolerance).
The AbsTol is ranged between 1e-15 and 0.5. The RelTol is ranged between 1e-12 and 0.1.
The underlying calculation is: Error = Abs(V2 - V1) / (AbsTol + Max(Abs(V1), Abs(V2)) * RelTol)
iif Variable = iif(expression, truePart, falsePart) The iif function is a two part if() statement, based on the condition of the expression, the True or False value (or expression) is returned.
See IIf for more detail.

# Examples

## Example 1: Math Functions

REAL         Sin_a@, Cos_b@, Tan_c@, aSin_a@, aCos_b@, aTan_c@
REAL         f@
INTEGER      w@
BIT          i*
BYTE         z*
CONST DOUBLE a = 0.6
CONST DOUBLE b = 2
CONST DOUBLE c = -0.5
CONST DOUBLE d = 20.49
CONST DOUBLE e = 20.51
CONST DOUBLE g = 2.5
CONST DOUBLE h = -3.4

Sin_a  = Sin(a) 	     			;Sin_a= 0.56464
Cos_b  = Cos(b) 	     			;Cos_b=-0.41615
Tan_c  = Tan(c) 	     			;Tan_c=-0.54630
aSin_a = aSin(Sin_a)     			;aSin_a= 0.6
aCos_b = aCos(Cos_b)     			;aCos_b= 2.0
aTan_c = aTan(Tan_c)     			;aTan_c=-0.5
f = aTan2(1,2,1,2) 	     			;f=0.785
f = Abs(g) 	             			;f=2.5
f = Abs(h) 	             			;f=3.4
f = Sqrt(g) 	         			;f=1.581
f = Sqrt(h) 		     			;f=* (NAN)	(error)
f = Cbrt(g) 	         			;f=1.357
f = Cbrt(h) 		     			;f=-1.504
f = Exp(-1 * g)          			;f=0.0821
f = Exp(h) 	             			;f=0.0334
f = Ln(g) 	             			;f=0.916
f = Ln(h) 	             			;f=* (NAN)	(error)
f = Ln(0) 	             			;f=0	(error)
f = Log(g) 	             			;f=0.398
w = Log(h) 	             			;f=-2147483648 (error)
f = Pow(g,h) 	         			;f=0.044
f = Pow(h,g) 	         			;f=* (NAN)	(error)
w = Mod(7,3) 	         			;w=1
w = Div(7,3) 	         			;w=2
f = Mod(3.4,1.1) 	     			;f=0.1
f = Div(3.4,1.1) 	     			;f=3.0
f = Mod(g,h) 	         			;f=2.5
f = Mod(h,g) 	         			;f=-0.9
f = Mod(h,0) 	         			;f=* (NAN)	(error)
w = Div(g,h)	         			;w=0
w = Div(h,g)	         			;w=1
f = Erf(1)   	         			;f=0.84279
f = Min(g,h) 	         			;f=-3.4
f = Max(g,h) 	         			;f=2.5
f = Range(0,h,4) 	     			;f=0.0
f = Range(0,g,4) 	     			;f=2.5
f = Range(h,-4.2,g) 	     		;f=-3.4
f = Range(g,-4.2,h) 	     		;f=2.5
f = Round(2.5)               		;rounds to 3
f = Round(0.1)               		;rounds to 0
f = Round(-3.7)              		;rounds to -4
f = Round(-3.4)              		;rounds to -3
f = RoundTo(3.2,0)           		;Rounds 3.2 to zero decimal places (3)
f = RoundUp(3.2,0)           		;Rounds 3.2 up to zero decimal places (4)
f = RoundTo(76.9,0)          		;Rounds 76.9 to zero decimal places (77)
f = RoundUp(76.9,0)          		;Rounds 76.9 up to zero decimal places (77)
f = RoundTo(3.14159, 3)      		;Rounds 3.14159 to three decimal places (3.142)
f = RoundUp(3.14159, 3)      		;Rounds 3.14159 up to three decimal places (3.142)
f = RoundTo(-3.14159, 1)     		;Rounds -3.14159 to one decimal place (-3.1)
f = RoundUp(-3.14159, 1)     		;Rounds -3.14159 up to one decimal place (-3.2)
f = RoundTo(31415.92654, -2) 		;Rounds 31415.92654 to 2 decimal places to the left of the decimal (31400)
f = RoundUp(31415.92654, -2) 		;Rounds 31415.92654 up to 2 decimal places to the left of the decimal (31500)
f = RoundTo(3.2,-3)          		;Rounds 3.2 to 3 decimal places to the left of the decimal (0)
f = RoundUp(3.2,-3)          		;Rounds 3.2 up to 3 decimal places to the left of the decimal (1000)
f = Floor(d)  	             		;f=20
f = Floor(e) 	             		;f=20
f = Floor(h) 	             		;f=-4
f = Ceil(d) 	             		;f=21
f = Ceil(e) 	             		;f=21
f = Ceil(h) 	             		;f=-3
f = Trunc(d) 	             		;f=20
f = Trunc(e) 	             		;f=20
f = Trunc(h) 	             		;f=-3
f = Round(d) 	             		;f=20
f = Round(e) 	             		;f=21
f = Round(h) 	             		;f=-3
f = IsCloseError(d,e) 	     		;f=929800.0929801546
f = IsCloseTolError(d,e,1e-3,1e-3) 	;f=0.9298000929801545 (within tolerance)


## Example 2: Calculating pH

Real Tank1_pH@, H_Concentration@("ConcMl", "mol/L")

H_Concentration = ["P_001.Qo.CMlC:LPh.HCl (mol/L)"] * 1
Tank1_pH        = -1 * Log(H_Concentration)


## Example 3: Inverse Trigonometric Functions

Inverse trigonometric functions were implemented in Build 137, see functions aSin, aCos and aTan.

For users using older versions of SysCAD, where Inverse trigonometric functions were not available, the aTan2 function can be used:

Real Z<0,1>*, ArcSinZ("Ang","rad")@, ArcCosZ("Ang","rad")@
ArcSinZ = aTan2(0, Z, 0, sqrt(1.0 - Z^2))
ArcCosZ = aTan2(0,sqrt(1.0 - Z^2) , 0, Z)


 ;--- variable declarations --- TextLabel(, "Convert Radians to Degrees") Real Angle_Radians_Input*<<1>>{Comment("Radians")} Real Angle_Degrees_Result@{Comment("Degrees")} TextLabel(, "Convert Degrees to Radians") Real Angle_Degrees_Input*<<180>>{Comment("Degress")} Real Angle_Radians_Result@{Comment("Radians")} ;--- Define Functions --- Function RadToDeg(Real UserDefined_Radians) Return UserDefined_Radians * 180 / PI EndFunct Function DegToRad(Real UserDefined_Degrees) Return UserDefined_Degrees / 180 * PI EndFunct ;--- Convert angles --- Angle_Radians_Result = DegToRad(Angle_Degrees_Input) Angle_Degrees_Result = RadToDeg(Angle_Radians_Input) \$ ; --- end of file ---