cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

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
Durval
Enthusiast
Instead of conditional change of 'a', why not simply change 'ang' directly?
IF a<epsilon THEN ang=90
Stuart, wouldn't this code
ratio = oA / ( (oB=0) * Epsilon + oB )
trigger the annoying 'precision problems' warning?
--- www.dtabach.com.br ---
AC 24 BR – MacBook Pro 2,9 GHz Intel Core i7 16GB RAM Mac OS 10.14
Anonymous
Not applicable
Durval;

I might be wrong, but IMHE, "x=0" doesn't seem to create a precision problem. x=1 (or any other number) can.

Will test a little over the w/e.

? - Stuart
Oleg
Expert
I would prefer:

IF ABS(a)<EPS THEN ang=SGN(b)*90 ELSE ang=TAN(b/a)

as imho it is more readable and clear
Anonymous
Not applicable
Hello,
Looking at all of the replies to my original question I can only conclude
that some conditional, somewhat arbitrary, change to the devisor must be made when the devisor acquires a zero value.
At the time I posted my question I was wondering if there was some mathematical algorithm or programing command that would say in effect
if a=0 then
a=(smallest non-zero quantity allowed by the program/math processor).
I was thinking that in floating point arithmetic "zero" must have some
internal definition limited by precision.
Since I don't have a computer science background the above statements
may be complete gibberish.
This thread has certainly brought up some fascinating issues.
Thank you all for your comments,
Peter Devlin