Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

Bug Report: Gear script crashed with divide by zero error

hex4def6
Observer Observer
470 Views
2 Replies
Message 1 of 3

Bug Report: Gear script crashed with divide by zero error

hex4def6
Observer
Observer

 

Tools / add-ins / SpurGear

 

Try to type "0" in module field (in my example, I want to use 0.7 as module). Note error that appears:

 

---------------------------
Fusion 360
---------------------------
Failed:
Traceback (most recent call last):
  File "C:/Users/SS/AppData/Local/Autodesk/webdeploy/production/1f559bb8ae333199306b5c4f1fe680c6eb7ab9e0/Python/Samples/SpurGear/SpurGear.py", line 390, in notify
    diaPitch = 25.4 / result[1]
ZeroDivisionError: float division by zero
---------------------------
OK  
---------------------------
Reply
Reply
0 Likes
471 Views
2 Replies
Replies (2)
Message 2 of 3

hex4def6
Observer
Observer

OK, I seemed to have fixed the error checking with the following:

 

Line 385 onwards:

                if result[0and result[1] > 0:
                    diaPitch = result[1]
            elif _standard.selectedItem.name == 'Metric':
                result = getCommandInputValue(_module, '')
                if result[0and result[1] > 0:
                    diaPitch = 25.4 / result[1]
            if not diaPitch == None:
                if _numTeeth.value.isdigit(): 
                    numTeeth = int(_numTeeth.value)
                    pitchDia = numTeeth/diaPitch

 

 

and line 444 onwards:

 

               elif result[1] <= 0:
                    _errMessage.text = 'The diametral pitch must be greater than 0'
                    eventArgs.areInputsValid = False
                else:
                    diaPitch = result[1]
            elif _standard.selectedItem.name == 'Metric':
                result = getCommandInputValue(_module, '')
                if result[0] == False:
                    eventArgs.areInputsValid = False
                    return
                elif result[1] <= 0:
                    _errMessage.text = 'The module must be greater than 0'
                    eventArgs.areInputsValid = False
                    return
 

 

 

 

Reply
Reply
0 Likes
Message 3 of 3

warnerjonn
Community Visitor
Community Visitor

The super class of ZeroDivisionError is ArithmeticError. This exception raised when the second argument of a division or modulo operation is zero. In Mathematics, when a number is divided by a zero, the result is an infinite number. It is impossible to write an Infinite number physically. Python interpreter throws “ZeroDivisionError” error if the result is infinite number. While implementing any program logic and there is division operation make sure always handle ArithmeticError or ZeroDivisionError so that program will not terminate.

 

try:

 

z = x / y
except ZeroDivisionError:
z = 0

 

Or check before you do the division:

 

if y == 0:
z = 0
else:
z = x / y

 

The latter can be reduced to:

 

z = 0 if y == 0 else (x / y)

 

Reply
Reply
0 Likes