error solved

Solved – Divide by zero encountered in double_scalars


In this article, you will learn how to solve Divide by zero encountered in double_scalars error in Python.

Let’s look at a code example that produces the same error.

import numpy as np

def k(x):
 x = np. float_(x)
 return 1. / (1. - x)


print ('k(1.) =', k(1.) )

output

<string>:5: RuntimeWarning: divide by zero encountered in double_scalars
k(1.) = inf

In order to solve Divide by zero encountered in double_scalars error in Python is by simply changing the value that does not confirm to the division specification like in the code snippet below:

import numpy as np

def k(x):
 x = np. float_(x)
 return 1. / (0. - x)

print ('k(1.) =', k(1.) )

Output

k(1.) = -1.0

Share on social media

//