Learn to manage BIM workflows and create professional Archicad templates with the BIM Manager Program.

Libraries & objects
About Archicad and BIMcloud libraries, their management and migration, objects and other library parts, etc.

Programing question

Anonymous
Not applicable
What is the best bit of code to use to avoid "divide by zero" errors?
I run into this issue often when using circular functions
such as in the following piece of code.

ratio=b/a
ang=atn(ratio)

If "a" happens to be zero there is an error.
Usually I resort to placing the following at the
beginning of the script.

if a=0 then a=0.0000001

This "work-around" is logically very dissatisfying to me.
Is there a better way?

Thank you,
Peter Devlin
13 REPLIES 13
TomWaltz
Participant

IF A <> 0 THEN
     ratio=b/a
     ang=atn(ratio)
ELSE
   (something to fix the problem)
ENDIF
Tom Waltz
Anonymous
Not applicable
Tom,
Thank you for your reply.
Its the "(something to fix the problem)"
that I was looking for.
Something more elegant than if a=0 then a=0.00000001.
Peter
TomWaltz
Participant
Where are A and B coming from?
Tom Waltz
Anonymous
Not applicable
Tom,
In the example I gave "a" and "b" are the obligatory
x and y dimensions of a library part.
But they could be any numeric parameter or variable
that has a possible value of zero either as initialized or
as the result of calculation.
Peter
TomWaltz
Participant
Peter

I was wondering if there was a certain situation this was coming from, that maybe there would be some kind of logic that would prevent it from happening. Since objects should probably never have an A or B value of 0, that should kickj up an error if its own.
Tom Waltz
Anonymous
Not applicable
Tom,
I appreciate your interest in this issue.
I made a library part that is a detail marker.
(see attached image)
I wanted (0,0) to be at the center of the "bubble"
and the point (a,b) to be at the edge of the call-out symbol
so that the connecting line between bubble and symbol
can be stretched to any angle and length and
still point to the center of the bubble similar to
what Red was trying to do in the thread titled "tangent".
The code must allow the library part's "A" to equal zero
to display the shape labeled #2
I know that there are ways to code this part so that
"A" equaling zero does not cause a problem, but
this is not the point I only use this lib part as an
example of where one value divides another value
and the dividing value may equal zero
and therefor cause an error.
Thanks,
Peter
rocorona
Booster
I don't have a more efficient solution. Usually, when I know that a divisor can became zero, I use the following form:

epsilon = 0.000001 ! <--at the beginning of the current (or Master) Script

ratio=b/MAX(a, epsilon)
ang=atn(ratio)


This way the error message can be avoided.
But, if the number can also be negative, the formula has to be more complex. ... I think something like this:

ratio=b / (MAX(ABS(a), epsilon) * (sgn(a)+(a=0)))

GASP!
_______________________________


--Roberto Corona--
_________________

--Roberto Corona--
www.archiradar.com
AC18 - ITA full on Win10
_________________
_________________
Laszlo Nagy
Community Admin
Community Admin
rocorona wrote:
I don't have a more efficient solution. Usually, when I know that a divisor can became zero, I use the following form:

epsilon = 0.000001 ! <--at the beginning of the current (or Master) Script

ratio=b/MAX(a, epsilon)
ang=atn(ratio)


This way the error message can be avoided.
But, if the number can also be negative, the formula has to be more complex. ... I think something like this:

ratio=b / (MAX(ABS(a), epsilon) * (sgn(a)+(a=0)))

GASP!
_______________________________


--Roberto Corona--
Yes, this is similar to what GS programmers do in their GDL Scripts.
One can check out any AC9 Door or Window script and will find many examples similar to this:

IF ABS(Whatever) > EPS THEN
...............
ENDIF

EPS is for Epsilon.
Loving Archicad since 1995 - Find Archicad Tips at x.com/laszlonagy
AMD Ryzen9 5900X CPU, 64 GB RAM 3600 MHz, Nvidia GTX 1060 6GB, 500 GB NVMe SSD
2x28" (2560x1440), Windows 10 PRO ENG, Ac20-Ac28
Anonymous
Not applicable
I have always preferred a 'logical check';

oEpsilon = 0.0000001
ratio = oA / ( (oB=0) * Epsilon + oB )


For angles this is extended to;

oAngle = atn ( oA / ( (oB=0) * Epsilon + oB)

Oh how I wish we had 'proper' macro statements ...

oAngle = myATN (oA, oB)
!------
define macro "myATN" (value1, value2)
return atn ( value1 / ( (value2=0) * 0.000001 + value2)


- Stuart