From 00f5f5eacd23b3264239faf3131051c0182b4efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Seidel?= Date: Fri, 26 May 2023 23:58:04 +0200 Subject: [PATCH 1/4] add trunc function --- src/TRUNC.st | 34 +++++++++++ test/TruncateTest.st | 134 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 src/TRUNC.st create mode 100644 test/TruncateTest.st 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 From dbbc1d95865f65be95e15f965c24999f3d3c9c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Seidel?= Date: Sat, 27 May 2023 00:04:00 +0200 Subject: [PATCH 2/4] describe TRUNC --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f7b638e..1c6153a 100644 --- a/README.md +++ b/README.md @@ -38,14 +38,6 @@ END_NAMESPACE |-|-|-| |NONE | Integer.ToString(value := 123) | '123' |FORCE_SIGN | Integer.ToString(value := 123, mode := ConversionMode#FORCE_SIGN) | '+123' - - - ## Functions @@ -136,6 +128,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 zo zero + +```iecst +TRUNC(value : LREAL) : DINT; +TRUNC(value : REAL) : DINT; +``` + ## Contribution Thanks for your interest in contributing. Anybody is free to report bugs, unclear documentation, and other problems regarding this repository in the Issues section or, even better, is free to propose any changes to this repository using Merge Requests. From c7481036b269e95a66d79485ae0383cbd2054aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Seidel?= Date: Fri, 26 May 2023 23:58:04 +0200 Subject: [PATCH 3/4] add trunc function --- src/TRUNC.st | 34 +++++++++++ test/TruncateTest.st | 134 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 src/TRUNC.st create mode 100644 test/TruncateTest.st 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 From 1214000c18871aaf6403c622f7b0022be5f67cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Seidel?= Date: Sat, 27 May 2023 00:04:00 +0200 Subject: [PATCH 4/4] describe TRUNC --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e7891cc..cad0287 100644 --- a/README.md +++ b/README.md @@ -38,14 +38,6 @@ END_NAMESPACE |-|-|-| |NONE | Integer.ToString(value := 123) | '123' |FORCE_SIGN | Integer.ToString(value := 123, mode := ConversionMode#FORCE_SIGN) | '+123' - - - ## Functions @@ -153,6 +145,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 zo zero + +```iecst +TRUNC(value : LREAL) : DINT; +TRUNC(value : REAL) : DINT; +``` + ## Contribution Thanks for your interest in contributing. Anybody is free to report bugs, unclear documentation, and other problems regarding this repository in the Issues section or, even better, is free to propose any changes to this repository using Merge Requests.