Math functions
- Last UpdatedJul 22, 2024
- 2 minute read
Use math functions to return the answer to the specified mathematical expression.
In QuickScript, all mathematical operations are calculated internally as double, regardless of the operand data type. Following standard mathematical rules, the result is always rounded in division operations to maintain accuracy. Rounding only occurs on the end result, not intermediate values, and the quotient will match the target data type. This is the standard methodology for SCADA and DCS systems, and provides the data integrity, precision retention, time stamps, and overall data quality propagation and aggregation needed for these systems.
If you want to round at each step instead of only at the final result, you can leverage the support built into QuickScript for .NET libraries and utilize the System.Math.Floor and System.Math.Round methods to explicitly round the intermediate steps. As an example, consider the following script:
dim dividend as integer;
dim divisor as integer;
dim quotient as integer;
dim remainder as integer;
dividend = 8;
divisor = 3;
LogMessage("Value of dividend = " + dividend);
LogMessage("Value of divisor = " + divisor);
quotient = dividend/divisor;
LogMessage("Value of quotient = " + quotient);
remainder = dividend mod divisor;
LogMessage ("Value of remainder = " + remainder);
dividend = divisor*quotient +remainder;
LogMessage ("Value of dividend = " + dividend);
The result is: 8 / 3 = 3
If, instead, you want to drop the remainder (not rounding the final result to the nearest integer), you could add a call to the Math.Floor method and use the following:
dim dividend as integer;
dim divisor as integer;
dim quotient as integer;
dim remainder as integer;
dividend = 8;
divisor = 3;
LogMessage("Value of dividend = " + dividend);
LogMessage("Value of divisor = " + divisor);
// *** Add call to Math.Floor. This drops the remainder rather than rounding the internal
Double result to integer
quotient = System.Math.Floor(dividend/divisor);
LogMessage("Value of quotient = " + quotient);
remainder = dividend mod divisor;
LogMessage ("Value of remainder = " + remainder);
dividend = divisor*quotient +remainder;
LogMessage ("Value of dividend = " + dividend);
The result is: 8 / 3 = 2 (remainder 2)