Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: Bolt on December 29, 2020, 12:42:04 PM

Title: "Clamping" in a MATH Instruction
Post by: Bolt on December 29, 2020, 12:42:04 PM
I have a MATH instruction referencing many other variables calculating to an Unsigned Word.  V0 = -R0 + (!C0 * D0) + (C0 * R1) + R2 + R3 Due to the many inputs, sometimes the calculation goes slightly negative, such as -5.23, so the value returns as ~ 65540.  Is there a clean way to prevent this from happening, returning a value of 0, without having to wrap the whole expression in an IF, V0 = IF(-R0 + (!C0 * D0) + (C0 * R1) + R2 + R3 < 0, -R0 + (!C0 * D0) + (C0 * R1) + R2 + R3)?  I'm trying to keep my logic simple and avoid helper elements and/or additional instructions.
Title: Re: "Clamping" in a MATH Instruction
Post by: franji1 on December 29, 2020, 02:54:19 PM
MAX(base-expression, 0.0)

Even though the DESTINATION is UNSIGNED, the calculation is REAL (since you are using R's).  Hence, MAX(anything, 0.0) will return 0.0 if anything is negative, otherwise it returns anything.
Title: Re: "Clamping" in a MATH Instruction
Post by: Bolt on December 29, 2020, 08:13:16 PM
Thanks, that makes for a much cleaner instruction.
Title: Re: "Clamping" in a MATH Instruction
Post by: Bolt on December 29, 2020, 08:24:04 PM
What are the chances y'all could make a "MathSim", where we could test drive MATH instructions in a program/utility without having to write a few rungs with trigger coils and dummy values to the DmSim or PLC to test drive a complicated math equation?
Title: Re: "Clamping" in a MATH Instruction
Post by: Mike Nash on December 29, 2020, 10:53:19 PM
... and

MIN(MAX(base-expression, min-clamp),max-clamp)

clamps between two values.

That is sweet.