diff --git a/README.md b/README.md index 5f7cf96..aa9735a 100644 --- a/README.md +++ b/README.md @@ -44,14 +44,6 @@ END_NAMESPACE |-|-|-| |NONE | Integer.ToString(value := 123) | '123' |FORCE_SIGN | Integer.ToString(value := 123, mode := ConversionMode#FORCE_SIGN) | '+123' - - - ## Functions @@ -167,6 +159,15 @@ Convert a String "[123, 456, 789]" to an ARRAY[*] OF LINT and returns the number > - startIdx out of the array boundaries startIdx : 1 and Array[5..10] > - endIdx out of the array boundaries endIdx : 15 and Array[0..10] +## TRUNC + +TRUNC() round a floating number no the next DINT value downwards to zero + +```iecst +TRUNC(value : LREAL) : DINT; +TRUNC(value : REAL) : DINT; +``` + ### Times Convert the date and time of a SIMOTION system into the data type LDATE_AND_TIME (LDT) and back. The SIMOTION format is a structured data type consisting of two 32-bit values. For the sake of simplicity, they are interpeted as DWORD. diff --git a/src/TRUNC.st b/src/TRUNC.st new file mode 100644 index 0000000..699ca62 --- /dev/null +++ b/src/TRUNC.st @@ -0,0 +1,34 @@ +NAMESPACE Simatic.Ax.Conversion + + FUNCTION TRUNC : DINT + VAR_INPUT + Value : LREAL; + END_VAR + TRUNC := TO_DINT(Value); + IF Value > 0.0 THEN + IF TO_LREAL(TRUNC) > Value THEN + TRUNC := TRUNC - 1; + END_IF; + ELSE + IF TO_LREAL(TRUNC) < Value THEN + TRUNC := TRUNC + 1; + END_IF; + END_IF; + END_FUNCTION + + FUNCTION TRUNC : DINT + VAR_INPUT + Value : REAL; + END_VAR + TRUNC := TO_DINT(Value); + IF Value > 0.0 THEN + IF TO_LREAL(TRUNC) > Value THEN + TRUNC := TRUNC - 1; + END_IF; + ELSE + IF TO_LREAL(TRUNC) < Value THEN + TRUNC := TRUNC + 1; + END_IF; + END_IF; + END_FUNCTION +END_NAMESPACE diff --git a/test/TruncateTest.st b/test/TruncateTest.st new file mode 100644 index 0000000..d443dff --- /dev/null +++ b/test/TruncateTest.st @@ -0,0 +1,134 @@ +USING AxUnit.Assert; + +NAMESPACE Simatic.Ax.Conversion + + {TestFixture} + CLASS TestTruncate + + {Test (v := 0.0000000000001, e := 0)} + {Test (v := 0.4, e := 0)} + {Test (v := 0.4000011111231, e := 0)} + {Test (v := 0.50000000000001, e := 0)} + {Test (v := 0.9, e := 0)} + {Test (v := 1.0, e := 1)} + {Test (v := 1.4, e := 1)} + {Test (v := 1.5, e := 1)} + {Test (v := 1.9, e := 1)} + {Test (v := 4.98235469312341234, e := 4)} + {Test (v := 5.0219410234234, e := 5)} + {Test (v := 9.0, e := 9)} + {Test (v := 9.4, e := 9)} + {Test (v := 9.5, e := 9)} + {Test (v := 9.9, e := 9)} + {Test (v := 10.0, e := 10)} + {Test (v := 10.4, e := 10)} + {Test (v := 10.5, e := 10)} + {Test (v := 10.9, e := 10)} + {Test (v := 100.0, e := 100)} + {Test (v := 100.4, e := 100)} + {Test (v := 100.5, e := 100)} + {Test (v := 100.9, e := 100)} + {Test (v := 1000.0, e := 1000)} + {Test (v := 1000.4, e := 1000)} + {Test (v := 1000.5, e := 1000)} + {Test (v := 1000.9, e := 1000)} + {Test (v := -0.0000000000001, e := 0)} + {Test (v := -0.4, e := -0)} + {Test (v := -0.4000011111231, e := -0)} + {Test (v := -0.50000000000001, e := -0)} + {Test (v := -0.9, e := -0)} + {Test (v := -1.0, e := -1)} + {Test (v := -1.4, e := -1)} + {Test (v := -1.5, e := -1)} + {Test (v := -1.9, e := -1)} + {Test (v := -4.98235469312341234, e := -4)} + {Test (v := -5.0219410234234, e := -5)} + {Test (v := -9.0, e := -9)} + {Test (v := -9.4, e := -9)} + {Test (v := -9.5, e := -9)} + {Test (v := -9.9, e := -9)} + {Test (v := -10.0, e := -10)} + {Test (v := -10.4, e := -10)} + {Test (v := -10.5, e := -10)} + {Test (v := -10.9, e := -10)} + {Test (v := -100.0, e := -100)} + {Test (v := -100.4, e := -100)} + {Test (v := -100.5, e := -100)} + {Test (v := -100.9, e := -100)} + {Test (v := -1000.0, e := -1000)} + {Test (v := -1000.4, e := -1000)} + {Test (v := -1000.5, e := -1000)} + {Test (v := -1000.9, e := -1000)} + METHOD PUBLIC Test_Different_LREALS + VAR_INPUT + v : LREAL; + e : DINT; + END_VAR + Equal(expected := e, actual := TRUNC(v)); + END_METHOD + + {Test (v := REAL#0.0000000000001, e := 0)} + {Test (v := REAL#0.4, e := 0)} + {Test (v := REAL#0.4000011111231, e := 0)} + {Test (v := REAL#0.50000000000001, e := 0)} + {Test (v := REAL#0.9, e := 0)} + {Test (v := REAL#1.0, e := 1)} + {Test (v := REAL#1.4, e := 1)} + {Test (v := REAL#1.5, e := 1)} + {Test (v := REAL#1.9, e := 1)} + {Test (v := REAL#4.98235469312341234, e := 4)} + {Test (v := REAL#5.0219410234234, e := 5)} + {Test (v := REAL#9.0, e := 9)} + {Test (v := REAL#9.4, e := 9)} + {Test (v := REAL#9.5, e := 9)} + {Test (v := REAL#9.9, e := 9)} + {Test (v := REAL#10.0, e := 10)} + {Test (v := REAL#10.4, e := 10)} + {Test (v := REAL#10.5, e := 10)} + {Test (v := REAL#10.9, e := 10)} + {Test (v := REAL#100.0, e := 100)} + {Test (v := REAL#100.4, e := 100)} + {Test (v := REAL#100.5, e := 100)} + {Test (v := REAL#100.9, e := 100)} + {Test (v := REAL#1000.0, e := 1000)} + {Test (v := REAL#1000.4, e := 1000)} + {Test (v := REAL#1000.5, e := 1000)} + {Test (v := REAL#1000.9, e := 1000)} + {Test (v := REAL#-0.0000000000001, e := 0)} + {Test (v := REAL#-0.4, e := -0)} + {Test (v := REAL#-0.4000011111231, e := -0)} + {Test (v := REAL#-0.50000000000001, e := -0)} + {Test (v := REAL#-0.9, e := -0)} + {Test (v := REAL#-1.0, e := -1)} + {Test (v := REAL#-1.4, e := -1)} + {Test (v := REAL#-1.5, e := -1)} + {Test (v := REAL#-1.9, e := -1)} + {Test (v := REAL#-4.98235469312341234, e := -4)} + {Test (v := REAL#-5.0219410234234, e := -5)} + {Test (v := REAL#-9.0, e := -9)} + {Test (v := REAL#-9.4, e := -9)} + {Test (v := REAL#-9.5, e := -9)} + {Test (v := REAL#-9.9, e := -9)} + {Test (v := REAL#-10.0, e := -10)} + {Test (v := REAL#-10.4, e := -10)} + {Test (v := REAL#-10.5, e := -10)} + {Test (v := REAL#-10.9, e := -10)} + {Test (v := REAL#-100.0, e := -100)} + {Test (v := REAL#-100.4, e := -100)} + {Test (v := REAL#-100.5, e := -100)} + {Test (v := REAL#-100.9, e := -100)} + {Test (v := REAL#-1000.0, e := -1000)} + {Test (v := REAL#-1000.4, e := -1000)} + {Test (v := REAL#-1000.5, e := -1000)} + {Test (v := REAL#-1000.9, e := -1000)} + METHOD PUBLIC Test_Different_REALS + VAR_INPUT + v : REAL; + e : DINT; + END_VAR + Equal(expected := e, actual := TRUNC(v)); + END_METHOD + + END_CLASS + +END_NAMESPACE