# [Solved] I am getting a warning

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error I am getting a warning <RuntimeWarning: invalid value encountered in sqrt> in python. So Here I am Explain to you all the possible solutions here.

## How I am getting a warning <RuntimeWarning: invalid value encountered in sqrt> Error Occurs?

Today I get the following error I am getting a warning <RuntimeWarning: invalid value encountered in sqrt> in python.

## How To Solve I am getting a warning <RuntimeWarning: invalid value encountered in sqrt> Error ?

1. How To Solve I am getting a warning Error ?

To Solve I am getting a warning <RuntimeWarning: invalid value encountered in sqrt> Error This is not 100% Python related. You can't calculate the square root of a negative number (when dealing with real numbers that is).

2. I am getting a warning

To Solve I am getting a warning <RuntimeWarning: invalid value encountered in sqrt> Error This is not 100% Python related. You can't calculate the square root of a negative number (when dealing with real numbers that is).

## Solution 1

This is not 100% Python related. You can’t calculate the square root of a negative number (when dealing with real numbers that is).

You didn’t take any precautions for when b**2 - (4*a*c) is a negative number.

>>> import numpy as np
>>>
>>> np.sqrt(4)
2.0
>>> np.sqrt(-4)
__main__:1: RuntimeWarning: invalid value encountered in sqrt
nan

Let’s test if you have negative values:

>>> import numpy as np
>>>
>>> a = 0.75 + (1.25 - 0.75) * np.random.randn(10000)
>>> b = 8 + (12 - 8) * np.random.randn(10000)
>>> c = -12 + 2 * np.random.randn(10000)
>>>
>>> z = b ** 2 - (4 * a * c)
>>> print len([_ for _ in z if _ < 0])
71

## Solution 2

If you’re hoping to do complex analysis (working with imaginary numbers as defined by sqrt(-1)) you can import cmath and use cmath.sqrt(-1) instead of numpy.sqrt(-1).

For example, when I’m calculating the refractive index of materials from permittivity and permeability (by definition, j is involved), I’ll write functions in python as such:

def n(f):
y = cmath.sqrt(mu1f(f) - j*mu2f(f)) * (e1f(f) - j*e2f(f))
return y.real

Where e1f etc. are previously defined interpolating functions, all of which are a function of incident frequency f. The y resultant is, in it of itself, a complex value, the complex index of refraction, but I’m oftentimes only interested in the real portion (the refractive index) so that is what is returned.

Hope this helps

## Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.