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
-
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.
-
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.
-
Thanks, that makes for a much cleaner instruction.
-
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?
-
... and
MIN(MAX(base-expression, min-clamp),max-clamp)
clamps between two values.
That is sweet.