Host Engineering Forum
General Category => Do-more CPUs and Do-more Designer Software => Topic started by: hdsi44 on June 17, 2021, 01:44:01 PM
-
I'm having a little trouble using IF in the MATH box. I'm trying to avoid a divide by zero error by using this statement:
IF(MHR2 > 0, TOREAL(T27.Acc / 1000) / (MHR2 * 6), 0) or:
IF(MHR2 == 0, 0, TOREAL(T27.Acc / 1000) / (MHR2 * 6))
Both ways result in a divide by zero error. What am I doing wrong?
-
The equation is evaluated even though you are trying to check for zero first, so you get the error. Just add 0.0000001 or something to MHR2 where it could be an issue. E.g. IF(MHR2 > 0, TOREAL(T27.Acc / 1000) / ((MHR2+0.0000001) * 6), 0)
You could possibly get rid of the MHR2>0 test then.
-
Interesting, I would not have thought it worked like that. Your expression will give a very large result. I'll just use ladder logic and two different math boxes.
Thanks.
-
Interesting, I would not have thought it worked like that. Your expression will give a very large result. I'll just use ladder logic and two different math boxes.
Thanks.
IF(cond, value1, value2) is simply a function, rather than branches in execution. The normal high level language construct IF(cond1){ return value1; } else { return value2; } is a significantly more difficult thing to add to the MATH box. We do hope to have a high level language eventually, but the MATH box is comparatively basic.
-
Thanks for the explanation, that makes sense.
-
Interesting, I would not have thought it worked like that. Your expression will give a very large result. I'll just use ladder logic and two different math boxes.
Thanks.
If you keep the check for MHR2 being non-zero or not too small, you'll get pretty darn close to an accurate result. While adding a very small number to MHR2 only in the equation in the MATH instruction would seem to give a large number, it doesn't matter so much because it is only there to keep from getting the divide by zero error. You will still get the "0" result in your output.
But different MATH instructions works too.