Skip to content

Commit

Permalink
Merge pull request #61 from simatic-ax/feat-add_new_conversions
Browse files Browse the repository at this point in the history
Feat add new conversions
  • Loading branch information
ThKindler authored Dec 8, 2023
2 parents e169e48 + 5aebb2c commit 9a0c201
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 8 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ END_NAMESPACE
|-|-|-|
|NONE | Integer.ToString(value := 123) | '123'
|FORCE_SIGN | Integer.ToString(value := 123, mode := ConversionMode#FORCE_SIGN) | '+123'
<!-- |FORCE_LEADING_ZEROS | Integer.ToString(value := INT#123) | '+00123'
|FORCE_LEADING_ZEROS | Integer.ToString(value := DINT#123) | '+0000000123' -->

<!-- > Modes can be combined. Example:
> ```
> Integer.ToString(value := 123, mode := ConversionMode#FORCE_SIGN)
> ```
> -->

## Functions

Expand Down Expand Up @@ -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.
Expand Down
34 changes: 34 additions & 0 deletions src/TRUNC.st
Original file line number Diff line number Diff line change
@@ -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
134 changes: 134 additions & 0 deletions test/TruncateTest.st
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9a0c201

Please sign in to comment.