Skip to content

OSCAT BASIC Mathematics

Thomas Storek edited this page Apr 11, 2018 · 2 revisions

5. Mathematics

5.1. ACOSH

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
ACOSH calculates the arcus hyperbolic cosine of the following formula:

5.2. ACOTH

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
Acoth calculates the arc cotangent hyperbolic with following formula:

5.3. AGDF.

Type Function: REAL
Input X: REAL (input)
Output REAL (Gundermann inverse function)
AGDF calculates the inverse Gundermann function.
The calculation is done using the formula:

5.4. ASINH

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
ASINH calculate the arc hyperbolic sine by the formula:

5.5. ATAN2

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
ATAN2 calculates the angle of coordinates (Y, X) in RAD. The result is bet-
ween - and +

5.6. ATANH

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
ATANH calculates the Arcus Hyperbolic tangent as follows:

5.7. BETA

Type Function: REAL
Input X: REAL (input)
Y: REAL (input value)
Output REAL (output value)
BETA computes the Euler Beta function.

5.8. BINOM

Type Function: DINT
Input N: INT (input value)
K: INT (input value)
Output DINT (output value)
BINOM calculates the binominal coefcient N over K for integer N and K.

5.9. CAUCHY

Type Function: REAL
Input X: REAL (input)
T: REAL (input)
U: REAL (input)
Output REAL (output value)
CAUCHY calculates the density function for Cauchy.

5.10. CAUCHYCD

Type Function: REAL
Input X: REAL (input)
T: REAL (input)
U: REAL (input)
Output REAL (output value)
CAUCHYCD calculated the distribution function after Cauchy.

5.11. CEIL

Type Function: INT
Input X: REAL (input)
Output INT (output value)
The CEIL function returns the smallest integer value greater or equal than
X.
Example: CEIL(3.14) = 4
CEIL(-3.14) = -3
CEIL(2) = 2

5.12. CEIL2

Type Function: DINT
Input X: REAL (input)
Output DINT (output value)
The function returns the smallest integer value CEIL2 greater or equal to
X.
Example: CEIL2(3.14) = 4
CEIL2(-3.14) = -3
CEIL2(2) = 2

5.13. CMP

Type Function: BOOL
Input X, Y: REAL (input)
N: INT (number of digits to be compared)
Output BOOL (result)
CMP compares two REAL values if the frst N points are equal.
Examples:
CMP(3.140,3.149,3) = TRUE CMP(3.140,3.149,4) = FALSE
CMP(0.015,0,016,1) = TRUE CMP(0.015,0,016,2) = FALSE
In the CMP function note that the dual coding of numbers a 0.1 in the deci-
mal system can not necessarily always displayed as 0.1 in the binary sys-
tem. Rather, it may happen that represented something less than 0.1 or
greater because the resolution is not the number in binary coding allows
exactly one 0.1. For this reason, the function can not detect for 100% the
diference of 1 in the last position. In addition, note that a data type REAL
with 32 bit has only a resolution of 7 - 8 decimal places.

5.14. COSH

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
COSH calculates the hyperbolic cosine using the formula:

5.15. COTH

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
COTH calculates the hyperbolic cotangent by the following formula:
For input values larger than 20 or less than -20 COTH provides the appro-
ximate value +1 or -1 corresponding to an accuracy better than 8 digits
and is thus below the resolution of type REAL.

5.16. D_TRUNC

Type Function: DINT
Input X: REAL (input)
Output DINT (output value)
D _TRUNC returns the integer value of a REAL value as DINT. The IEC
routine TRUNC() does not supports on all systems a TRUNC to DINT so that
we have rebuilt this routine for compatibility. Unfortunately, even
REAL_TO_DINT does not give on all systems the same result. D_TRUNC
reviewes what result the IEC functions provides, and uses the appropriate
function to deliver a useful result.
D_TRUNC(1.6) = 1
D_TRUNC(-1.6) = -1

5.17. DEC1

Type Function: INT
Input INT: X (number of values X can be)
N: INT (the variable which is incremented)
Output INT ( Return Value )
DEC1 counts the variable X from N-1 to 0 and then starts again at N-1, so
that exactly N diferent starting values are generated at N-1 through 0.

5.18. DEG

Type Function: REAL
Input Rad: REAL (angle in radians)
Output REAL (angle in degrees)
The function converts an angle value from radians to degrees. This takes
into account the input may be not larger than 2.. If RAD is greater than
2 , the equivalent to 2 is deducted until the input RAD is between 0 and
2 .
DEG(π) = 180 Grad,DEG(3π) = 180 Grad
DEG(0) = 0 Grad, DEG(2π) = 0 Grad

5.19. DIFFER

Type Function: BOOL
Input IN1: REAL (value 1)
IN2: REAL (value 2)
X: REAL (minimum diference in1 to in2)
Output BOOL (TRUE if in1 and in2 difer by more than x from each
other )
The function DIFFER is TRUE if in1 and in2 difer by more than X from each
other.
Example: Difer(100, 120, 10) returns TRUE
Difer(100,110,15) returns FALSE

5.20. ERF.

Type Function: REAL
Input X: REAL (input)
Output REAL (result)
The ERF function calculates the error function of X. The error function is
calculated using an approximation formula, the maximum relative error is
smaller than 1,3 * 10-4.

5.21. ERFC.

Type Function: REAL
Input X: REAL (input)
Output REAL (result)
The function ERFC calculates the inverse error function of X.

5.22. EVEN.

Type Function: BOOL
Input in: DINT (input)
Output BOOL (TRUE, if in straight)
The function EVEN = TRUE if the input IN is even and FALSE for odd IN.
Example: EVEN(2) is TRUE
EVEN(3) returns FALSE

5.23. EXP10

Type Function: REAL
Input X: REAL (input)
Output REAL (exponential base 10)
The function Exp10 returns the exponential base 10
Exp10 (2) = 100
EXP10(0) = 1
EXP10(3.14) = 1380.384265

5.24. EXPN

Type Function: REAL
Input X: REAL (input)
N: INT (exponential)
Output REAL (result X^N)
EXPN calculates the exponential value of X^N for integer N. EXPN is espe-
cifcally written for PLC without Floating Point Unit and is about 30 times
faster than the IEC standard function EXPT(). Note the special case of the
0^0 defned mathematically as a 1 and is not a 0.
EXPN(10,-2) = 0.01 EXPN(1.5,2) = 2.25
EXPN(0,0) = 1

5.25. FACT

Type Function: DINT
Input X: INT (input)
Output DINT (Faculty of X)
The function FACT calculates the factorial of X.
It is defned for input values from 0  12. For values less than zero and
greater than 12 is the result -1. For the factorial of larger numbers, the
GAMMA function is suitable.
For natural numbers X: X! = 1*2*3.*(X-1)*X, 0! = 1
Faculties of negative or non-whole numbers are not defned.
Example: 1! = 1
2! = 1*2 = 2
5! = 1*2*3*4*5 = 120

5.26. FIB

Type Function: DINT
Input X: INT (input)
Output DINT (Fibonacci numbers)
FIB calculate the Fibonacci number The Fibonacci number is defned as fol-
lows:
FIB(0) = 0, FIB(1) = 1, FIB(2) = 1, FIB(3) = 2, FIB(4) = 3, FIB(5) = 5 .
The Fibonacci number of X is equal to the sum of the Fibonacci numbers of
X-1 and X-2. The function can compute the Fibonacci numbers up to 46, if
X < 0 or greater than 46, the function returns -1.

5.27. FLOOR

Type Function: INT
Input X: REAL (input)
Output INT (output value)
The FLOOR function returns the greatest integer value less or equal to X.
Example: FLOOR(3.14) = 3
FLOOR(-3.14) = -4
FLOOR(2) = 2

5.28. Floor2

Type Function: DINT
Input X: REAL (input)
Output DINT (output value)
The function foor2 returns the largest integer value less or equal than X
back.
Example: FLOOR2(3.14) = 3
FLOOR2(-3.14) = -4
FLOOR2(2) = 2

5.29. FRACT.

Type Function: REAL
Input X : REAL (input)
Output REAL (fractional part of X)
The function Fract returns the fractional part of X.
Example: FRACT(3.14) results 0.14.
For X greater than or less than +/- 2.14 * 10^9 Fract always provides a
zero return. As the resolution of a 32bit REAL is a maximum of 8 digits,
from numbers larger or smaller than +/- 2.14 * 10^9 no fractional part can
be determined, because this part can also not be stored in a REAL varia-
ble.

5.30. GAMMA

Type Function: REAL
Input X: REAL (input)
Output REAL (Gamma function)
The function GAMMA calculates the gamma function after approximation
of NEMES.
The gamma function can be used for Integer X as replacement for the Fa-
culty.

5.31. GAUSS

Type Function: REAL
Input X: REAL (input)
U: REAL (locality of the function)
SI: REAL (Sigma, spreading the function)
Output REAL (Gaussian function)
The function calculates the Gaussian normal distribution using the followi-
ng formula:
The normal distribution is the density function normally distributed ran-
dom variables. With the parameters U = 0 and SI = 1, it follows the stan-
dard normal distribution.

5.32. GAUSSCD

Type Function: REAL
Input X: REAL (input)
U: REAL (locality of the function)
SI: REAL (Sigma, spreading the function)
Output REAL (Gaussian distribution function)
The function GAUSSCD calculated the distribution function for normal dis-
tribution using the following formula:
The normal distribution is the density function normally distributed ran-
dom variables. With the parameters U = 0 and SI = 1, it follows the stan-
dard normal distribution. The distribution function ( Cumulative Distributi-
on Function ).

5.33. GCD

Type Function
Input A: DINT (input value A)
B: DINT (input value B)
Output INT (Greatest common divisor)
The GCD function calculates the greatest common divisor (GCD) of A and
B.

5.34. GDF

Type Function: REAL
Input X: REAL (input)
Output REAL (Gundermann function)
GDF calculate the Gundermann function.
The calculation is done using the formula:
The result of GDF is between -π/2 and + π/2
GDF(0) = 0

5.35. GOLD

Type Function: REAL
Input X: REAL (input)
Output REAL (result of the Golden function)
GOLD calculates the result of the golden feature. GOLD (1) gives the gol-
den ratio, and GOLD (0) returns 1. GOLD (X) * GOLD (-X) is always 1. GOLD
(X) is the positive result of the quadratic equation and -GOLD(-X) is the ne-
gative result of the quadratic equation.
The calculation is done using the formula:

5.36. HYPOT

Type Function: REAL
Input X: REAL (X - value)
Y: REAL (Y - value)
Output REAL (length of the hypotenuse)
The Mortgage function calculates the hypotenuse of a right triangle, by
the theorem of Pythagoras.

5.37. INC

Type Function: INT
Input X: INT (input)
D: INT (value to be added to the input value)
M: INT (maximum value for the output)
Output INT (output value)
INC adds to the input X the Value D and ensures that the output INC is not
does not exceed the value of M. If the result from the addition of X and D
is greater than M, then it starts again at 0. The feature is especially useful
when addressing arrays and bufers. Even the positioning of absolute en-
coders it can be used. INC can be used to decrementieren with a negative
D, while INC will ensure that the result is not below zero. If subtract 1 from
zero INC starts again at M.
INC: = X + D, because D can take the maximum value M.
If INC > M so INC starts again at 0.
If INC < 0 so INC starts again at M
Example: INC(3, 2, 5) ergibt 5
INC(4, 2, 5) ergibt 0
INC(0,-1,7) ergibt 7

5.38. INC1

Type Function: INT
Input X: INT(number of values X can be)
N INT(the variable that is incremented)
Output INT ( Return Value )
INC1 count the variable X from 0 N-1 and then starts again from 0, so
that exactly N diferent values are produced starting from 0.

5.39. INC2

Type Function: INT
Input X: INT (input)
D: INT (value to be added to the input value)
L: INT (lower limit)
U: INT (upper limit)
Output INT (output value)
INC2 addes valued D to the input X and ensures that the output INC does
not exceed the value U (upper limit) or under-run the value L (low limit). If
the result from the addition of X and D is larger than U so it begins again
with L. It is ensured that at negative D when reaching L counted again at
U on. The feature is especially useful when addressing arrays and bufers.
Even the positioning of absolute encoders it can be used. INC2 can be
used to decrementieren with a negative D, while INC2 will ensure that the
result is not below zero.
INC2 := X + D, where L <= INC2 <= U.
Example: INC2(2, 2, -1, 3) result -1
INC2(2, -2, -1, 3) result 0
INC2(2, 1, -1, 3) result 3
INC2(0, -2, -1, 3) result 3

5.40. INV

Type Function: REAL
Input X: REAL (input)
Output REAL ( 1 / X )
INV calculates the inverse of X:

5.41. LAMBERT_W

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
The LAMBERT_W function is defned for x > = -1/e. When the value is be-
low the range the is result -1000. The range of LAMBERT_W function is >=
-1.

5.42. LANGEVIN.

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
The The Langevin Function is very similarto sigmoid function, but more
slowly approaching the limits. In contrast to the sigmoid are the values at
-1 and +1. The Langevin function is mainly at CPUs without foating point
unit much faster than the Sigmoid function.
The following chart shows the progress of the Langevin function:

5.43. MAX3

Type Function: REAL
Input IN1: REAL (input 1)
IN2: REAL (input 2)
IN3: REAL (input 3)
Output REAL (maximum of 3 inputs)
The function MAX3 delivers the maximum value of 3 inputs. Basically, the
in standard IEC61131-3 contained function MAX should be equipped with a
variable number of inputs. However, since in some systems the MAX func-
tion is supported only two inputs, the function MAX3 is ofered.
Example: MAX3(1,3,2) = 3.

5.44. MID3

Type Function: REAL
Input IN1: REAL (input 1)
IN2: REAL (input 2)
IN3: REAL (input 3)
Output REAL (mean value of the 3 inputs)
The function MID3 returns the average value of 3 inputs, but not the ma-
thematical average.
Example: MID3(1,5,2) = 2.

5.45. MIN3

Type Function: REAL
Input IN1: REAL (input 1)
IN2: REAL (input 2)
IN3: REAL (input 3)
Output REAL (Minimum 3 inputs)
The function MIN3 returns the minimum value of 3 inputs. Basically, the
function MIN in standard functionality IEC61131-3 should have a variable
number of inputs. However, since in some systems the function MIN sup-
portes only two inputs, the function MIN3 is available.
Example: MIN3(1,3,2) = 1.

5.46. MODR

Type Function: REAL
Input IN: REAL (Dividend)
DIVI: REAL (divisor)
Output REAL (remainder of division)
The function MODR returns the remainder of a division similar to the stan-
dard MOD function, but for REAL numbers. MODR internally uses the data
format of type DINT. This may come to an overfow because DINT can sto-
re a maximum of +/-2.14 * 10^9 The range of MODR is therefore limited
to +/- 2.14 * 10^9. For DIVI = 0 the function returns 0.
MODR(A, M) = A - M * FLOOR2(A / M).
Example: MODR(5.5, 2.5) result 0.5.

5.47. MUL_ADD

Type Function: REAL
Input X: REAL (input)
K: REAL (multiplier)
O: REAL (ofset)
Output : REAL (output value)
MUL_ADD multiplies the input value X with K and adds O.
MUL_ADD = X * K + O.
MUL_ADD (0.5, 10, 2) is 0.5 * 10 + 2 = 7

5.48. NEGX

Type Function: REAL
Input X: REAL (input)
Output REAL (-X)
NEGX returns the negated input value (-x).

5.49. RAD

Type Function: REAL
Input DEG: REAL (angle in degrees)
Output REAL (angle in radians)
The RAD function converts an angle value from degrees to radians. Taking
into account that the DEG will not be greater than 360. If DEG is greater
than 360, 360 is to be subtracted as long as DEG is again between 0-360
°.
RAD(0) = 0 RAD(180) = π
RAD(360) = 0 RAD(540) = π

5.50. RDM

Type Function: REAL
Input LAST: REAL (last calculated value)
Output REAL (random number between 0 and 1)
RDM calculates a pseudo- random number. This is the PLC's internal Timer
read and converted into a pseudo-random number. Because RDM's is writ-
ten as a function and not as a function module, it can not save data bet-
ween 2 calls and should therefore be used with caution. RDM is only called
once per cycle, it produces reasonable good results. But when it is repea-
tedly called within a cycle, it delivers the same number, most likely becau-
se of the PLC timer is still on the same value. If the function is repeatedly
used within a cycle, so it must be passed with each call a diferent number
of starts (LAST). It shall be called only once per cycle, is sufcient to call
RDM(0). As a starting number for each call, the last calculated number of
RDM can be used. Supplied by RDM random numbers between 0 and 1,
which does not contain 1 (0 <=random number < 1)

5.51. RDM2

Type Function: INT
Input LAST: INT (last calculated value)
LOW: INT (lowest generated value)
HIGH: INT (highest generated value)
Output INT (random number between LOW and HIGH)
RDM2 generates an integer random value in the range from LOW to HIGH,
where LOW and HIGH are being included in the range of values. If the
function is used only once per cycle, the input value LAST can remain at 0.
The function RDM2 used the PLC internal time base to generate the ran-
dom number. Since RDM2 uses LAST, an integer which represents the fnal
result between LOW and HIGH, it can lead to a situation, in which the re-
sult RDM2 always produces the same result, as long as PLC Timer does not
change in a cycle. This most often happens, if the result is identical to the
start value. Since then, the same start value will be reused within the
same cycle again, and the result is the same. This occurs more often, de-
pending on, if the specifc area of LOW and HIGH is smaller for the result.
One can avoid this efect easily by using as a starting value of loop coun-
ter which defnitely uses each time a new value, or better yet add a loop
counter with the fnal result used as initial value.

5.52. RDMDW

Type Function: DWORD
Input LAST: DWORD (last calculated value)
Output DWORD (Random Pattern)
RDMDW charges pseudo - random number with 32 bits in length in the for-
mat DWORD. This is the PLC's internal timer that is read and is transferred
into a pseudo random number. Since RDMDW as a function and was not
written as a function module, it can not save data between 2 calls and
should therefore be used with caution. If RDMDW called only once per cy-
cle, it produces reasonable good results. But when it is repeatedly called
within a cycle, it delivers the same number, most likely because of the PLC
timer is still on the same value. If the function is repeatedly used within a
cycle, so it must be passed with each call a diferent number of starts
(LAST). If it be called only once per cycle, it is sufcient to call RDMDW(0).
As a starting number for each call, the last number accounted by RDMDW
be used. That result from RDMDW is a random 32-bit wide bit pattern.

5.53. REAL_TO_FRAC

Type Function: FRACTION
Input X: REAL (input)
N: INT (maximum value of the denominator)
Output FRACTION (output value)
REAL_TO_FRAC converts a foating point number (REAL) in a fraction. The
function returns the data type is a FRACTION of the structure with 2 va-
lues. With the input X, the maximum size of the counter can be specifed.
Data type FRACTION:
*.NUMERATOR : INT (Numerator of the fraction)
*.DENOMINATOR : INT (Denominator of the fraction)
Example:
REAL_TO_FRAC(3.1415926, 1000) results 355 / 113.
355/133 gives the best approximation for the denominator < 1000

5.54. RND

Type Function: REAL
Input X: REAL (input)
N: integer (number of digits)
Output REAL (rounded value)
The function R ND rounds the input value IN to N digits. Follows the last
point a number that is greater than 5, the last digit is rounded up. RND in-
ternally uses the standard function TRUNC() which converts the input va-
lue to an INTEGER type DINT. This may come as an overfow because DINT
can store in maximum +/-2.14*10^9. The range of the RND is therefore li-
mited to +/-2.14*10^9. See also the ROUND function which rounds the in-
put value to N decimal places.
Example: RND(355.55, 2) = 360
RND(3.555, 2) = 3.6
ROUND(3.555, 2) = 3.56

5.55. ROUND

Type Function: REAL
Input IN: REAL (input value)
N: integer (number of decimal places)
Output REAL (rounded value)
The function ROUND rounds the input value IN to N digits. Follows the last
digit a digit greater than 5 the last digit is rounded up. ROUND internally
uses the standard function TRUNC() which converts the input value to an
INTEGER type DINT. This may come as an overfow because DINT can store
in maximum +/-2.14*10^9. The range of ROUND is therefore limited to +/-
2.14 * 10^9.
Example: ROUND(3.555, 2) = 3.56

5.56. SGN

Type Function: INT
Input X: REAL (input)
Output INT ( Signum the input X)
The function SGN calculates the Signum of X.
SGN = +1 if X > 0
SGN = 0 if X = 0
SGN = -1 if X < 0

5.57. SIGMOID

Type Function: INT
Input X: REAL (input)
Output REAL (result of Sigmoid )
The Sigmoid is also named Gooseneck - function and described by the fol-
lowing equation:
SIGMOID = 1 / (1 + EXP(-X))
The Sigmoid is often used as activation function. By its behavior the
Sigmoid is qualifed for soft switching transitions.
The following chart illustrates the progress of the Sigmoid :

5.58. SIGN_I.

Type Function: BOOL
Input IN: DINT (input)
Output BOOL (TRUE if the input is negative)
The function SIGN_I returns TRUE if the input value is negative. The input
values are of type DINT.

5.59. SIGN_R

Type Function: BOOL
Input IN: REAL (input)
Output BOOL (TRUE if the input is negative)
The SIGN_R function returns TRUE if the input value is negative. The input
values are of type REAL.

5.60. SINC

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
SINC calculates the sine Kardinalis or the gap function.
Mit SINC(0) = 1.

5.61. SINH

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
SINH calculates the sinus Hyperbolic according the following formula:

5.62. SQRTN

Type Function: REAL
Input X: REAL (input)
N: INT (input value)
Output REAL (output value)
SQRTN calculates the N-fold root of X as follows:

5.63. TANC

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
TANC TANC function calculates the following formula:
with TANC(0) = 1.

5.64. TANH

Type Function: REAL
Input X: REAL (input)
Output REAL (output value)
TANH calculates the Tangent Hyperbolic according to the following
formula:

5.65. WINDOW.

Type Function: BOOL
Input LOW: REAL (lower limit)
IN: REAL (input value)
HIGH: REAL (upper limit)
Output BOOL (TRUE, if in < HIGH and in > LOW)
The WINDOW function tests whether the input value is within the limits de-
fned by the LOW and HIGH.
WINDOW is exactly TRUE if IN < HIGH and IN > LOW.

5.66. WINDOW2.

Type Function: BOOL
Input LOW: REAL (lower limit)
IN: REAL (input value)
HIGH: REAL (upper limit)
Output BOOL (TRUE, if in <= HIGH and in >= LOW)
The WINDow2 function tests whether the input value IN <= HIGH and IN
>= LOW. In contrast to the function WINDOW which returns TRUE if the IN
is within the limits LOW and HIGH WINDOW2 supplies FALSE if IN is outside
the limits LOW and HIGH.