Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: Mike Nash on December 15, 2015, 03:41:35 PM

Title: Divide by Zero
Post by: Mike Nash on December 15, 2015, 03:41:35 PM
I am seeing a Divide by zero - $DivideByZero (ST141)

for a MATH Box Result to R112
IF(D112 != 0, 1.0 / D112, 0.0)

I also tried
IF (D112 > 0, 1.0 / D112, 0.0)

So it seems it is evaluating the second term even when the first term is false. If I do the comparison as an If Greater Than CONTACT, no problems, but I was wanting to keep it all in one rung after some previous branches massage some registers.

I have tried rearranging terms and conditions to no avail. This is somewhat annoying behavior.

I also could not use (D112 = 0, 0.0, 1.0 / D112) as it will not accept D112=0 ?? It will take D112<1 but still gives divide by zero if D112 is zero.
Title: Re: Divide by Zero
Post by: franji1 on December 15, 2015, 04:27:46 PM
MATH's IF is a function, not a program logic branch mechanism.

The IF function takes 3 parameters, and all 3 parameters must be evaluated and then be passed in to the "IF" function.  Hence, why you see the divide by 0 error  :-\.

Also, the grammar utilized by MATH is based upon C/C++/Java/PHP/C#, so "equal to" is "==" because "=" is assignment.

So just change your D112 = 0 to D112 == 0.  We can add some helpers with that (also "<>" is not supported for "not equal to", but use "!=").
Title: Re: Divide by Zero
Post by: franji1 on December 15, 2015, 04:40:55 PM
Regarding the "=" "equal-to" token error, minimally we can give you a much better error message telling you to use "==" in replace of "=", but even better would be to auto-correct it to "==", applying the adage do what I mean, not what I say.
Title: Re: Divide by Zero
Post by: Mike Nash on December 15, 2015, 04:57:23 PM
IF(D112 < 1, 0.0, 1.0 / (1.0E-09 + D112))

I "fixed" it!  8)
Title: Re: Divide by Zero
Post by: franji1 on December 15, 2015, 05:06:12 PM
INGENIOUS!  I was trying to figure out something similar, but could not figure it out ;D.

GREAT HACK!
Title: Re: Divide by Zero
Post by: Bolt on November 27, 2019, 06:51:33 PM
So I ran into this problem the hard way too, and sit scratching my head at it.

Code: [Select]
R114 = IF(R113 == 0, 0, R112 / R113)
returns Divide by zero errors.  What would be a workaround here (besides multiple extra rungs containing a relational contact and triggered by extra coils)?

I see how you say all 3 parameters are analyzed, but I'm wondering why.  This seems to not be the case in other programs/languages, such as Excel or PHP.
...the grammar utilized by MATH is based upon C/C++/Java/PHP/C#...

Code: [Select]
<?php
if($R113 == 0)
{
$R114 0;}
else
{
$R114 $R112/$R113;}
?>


This seems to work regardless of a R113=0.  The grammar is based on C/C++,etc, but the logic is not?
Title: Re: Divide by Zero
Post by: BobO on November 27, 2019, 07:33:48 PM
The IF only controls the assignment, not the calculation, both sides are calculated. It's not preferred, but the IF is a function call, not a language flow construct. The only alternative was to not have the IF function at all.
Title: Re: Divide by Zero
Post by: Bolt on November 27, 2019, 10:15:58 PM
Alright. Finally this makes sense to me.
Code: [Select]
R114 = IF(R113 == 0, 0, R112 / (0.000000001 + R113))
Title: Re: Divide by Zero
Post by: Controls Guy on November 29, 2019, 11:28:04 AM
Non-zero denominators, it's not just a good idea, it's the law!