0

When trying to solve an optimization problem, I was constantly getting:

 RuntimeWarning: divide by zero encountered in divide

Although analitically writing 1/x and x^(-1) are the same thing, re-writing my function by substituting each division as shown solved my problem.

I was curious to know what makes it different for a computer to compute either a division or an exponentiation.

EDIT:

I am solving a physical problem involving nanometric scales. The complete error I was obtaining is:

    RuntimeWarning: divide by zero encountered in divide
    term = alpha / w

where alpha is some constant and w>0 is a length. The values of w that I am expecting are of the order of 1e-9 or smaller, and alpha is of the order of 0.1.

Nevertheless, the actual problem IS solved. I am actually surprised such a simple change in the code solved it all, and thus the question.

EDIT 2:

I solved the problem by writing:

term = alpha * w**(-1)

I do know ** equals to exponentiation in Python.

ma7642
  • 139
  • 2
  • 12
  • exponentiation = repeated multiplication (e.g. `mul` instruction). division = `div` instruction, basically. – Marc B Jul 13 '16 at 14:54
  • 1
    @MarcB That doesn't quite explain it in this case, as it's not immediately obvious how you repeat something -1 times. – Two-Bit Alchemist Jul 13 '16 at 14:56
  • 1
    I'm not sure how using exponentiation instead of division could fix your issue if you were running on the same data. `0 ** -1` gives a `ZeroDivisionError` just like `1 / 0`. I'd note that neither of them give exactly the same error message you describe, so maybe this is an XY problem. Can you give more details (and maybe some code) showing what you're actually doing? – Blckknght Jul 13 '16 at 14:58
  • @Blckknght Thank you, I added some extra information. – ma7642 Jul 13 '16 at 15:09
  • I believe you are using python 2.x. In that version, division of an integer by an integer will not give you a float. It rounds the result to an integer which probably gives you a 0 for term. – joel goldstick Jul 13 '16 at 15:14
  • because you work with very small numbers maybe is better to try http://mpmath.org/ . – valentin Jul 13 '16 at 15:15
  • @valentin Thank you so much for the recommendation! – ma7642 Jul 13 '16 at 15:25
  • for any reason if alpha * w evaluates to zero, your initial issue comes back. so replacing division with exponentiation is not the solution for ZeroDivision error. – Kevin Jul 13 '16 at 15:26
  • what are the values of alpha and w when you get your error? – joel goldstick Jul 13 '16 at 15:44
  • @Kevin Note that the error I got is not ZeroDivision error. I am using w such that it is never equal to zero (w>0). – ma7642 Jul 13 '16 at 15:51
  • @joelgoldstick I tried to reproduce the error by going back to the code I had before, but I must have changed something else because I am unable to get it again. I guess it had to be a non-zero but very small number for w, alpha is constant, 0.15. – ma7642 Jul 13 '16 at 15:56

2 Answers2

2

In python, ^ operator is XOR operation. Exponentiation is done using ** operator, which will give ZeroDivision error for 0**-1

Kevin
  • 901
  • 1
  • 7
  • 15
1

This is your problem:

>>> alpha = 1
>>> w = 2
>>> term = alpha/w
>>> term
0


>>> term = alpha * w **(-1)
>>> term
0.5
>>>
>>> term = float(alpha)/w
>>> term
0.5

In the first example, you are doing integer division which rounds to 0. In the second example you are implicitly converting to float by performing exponentiation. The third example shows that you can get the correct result with division by converting either value to a float

joel goldstick
  • 4,393
  • 6
  • 30
  • 46
  • I didn't know exponentiation implicitly converted to float, yet I am sure the values I am using are float type. I think it can be related with dealing with rather small numbers. However, this is the kind of answer I was looking for. – ma7642 Jul 13 '16 at 16:04