News:

  • October 13, 2025, 01:41:09 PM

Login with username, password and session length

Author Topic: Divide by Zero  (Read 4117 times)

Mike Nash

  • Hero Member
  • *****
  • Posts: 645
Divide by Zero
« 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.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: Divide by Zero
« Reply #1 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 "!=").

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: Divide by Zero
« Reply #2 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.

Mike Nash

  • Hero Member
  • *****
  • Posts: 645
Re: Divide by Zero
« Reply #3 on: December 15, 2015, 04:57:23 PM »
IF(D112 < 1, 0.0, 1.0 / (1.0E-09 + D112))

I "fixed" it!  8)

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: Divide by Zero
« Reply #4 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!

Bolt

  • Hero Member
  • *****
  • Posts: 591
Re: Divide by Zero
« Reply #5 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?

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: Divide by Zero
« Reply #6 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.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Bolt

  • Hero Member
  • *****
  • Posts: 591
Re: Divide by Zero
« Reply #7 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))

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3601
  • Darth Ladder
Re: Divide by Zero
« Reply #8 on: November 29, 2019, 11:28:04 AM »
Non-zero denominators, it's not just a good idea, it's the law!
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.