I'm doing a program that does numerical calculations, but I constantly see operations where there is division by 0 (zero)
. I ended up finding a solution using if
, but I still wanted to know why try
can not handle this exception.
def substituicoesRetroativas(A,b):
n = len(b)
x = zeros(n)
for i in range(n-1,-1,-1):
soma = 0
for j in range(i+1,n):
soma += A[i,j] * x[j]
try:
x[i] = float(b[i] - soma)/A[i,i]
except:
print "Matriz Impossivel"
break
return np.matrix(x).transpose()
When running, I get the following error message
Eliminacao de Gauss :
/home/ubuntu/workspace/PASSINI/Trab 2/sS.py:23: RuntimeWarning: divide by zero encountered in double_scalars
x[i] = float(b[i] - soma)/A[i,i]
/home/ubuntu/workspace/PASSINI/Trab 2/sS.py:20: RuntimeWarning: invalid value encountered in double_scalars
soma += A[i,j] * x[j]
[[ nan]
[-inf]
[ inf]]