Find square root of negative number using python
We can get the square root of a negative number in python using the cmath python library. It provides us various functions to work with complex numbers.
In this article, we will learn how to find the square root of a negative number using python.
To find the square root of a negative number we have to use complex numbers. In python, we have the cmath that provides us with various mathematical functions to work with complex numbers.
Square root of a negative number in Python
To work with complex numbers we can use the cmath library of python. And to get the square of a negative number we can use the cmath.sqrt() method from cmath library.
Example:
import cmath number = -5 negative_sq_val = cmath.sqrt(number) print(negative_sq_val)
Output:
2.23606797749979j
Here, in this example, we have imported the cmath module and then passed the negative number to the sqrt() method.
The cmath.sqrt() method performs the mathematical operations to find the square root and returns the number.
Other Articles You'll Also Like:
Related Posts
How to merge JSON files in python
Learn different techniques to merge multiple JSON files in Python using built-in JSON module and Pandas library. Includes examples for combining data arrays and merging dictionaries by key.
Pytube Description and Keyword Not Showing: Solution
Solve the issue with Pytube not showing youtube description and tags in this article.
How to Fix the subprocess-exited-with-error in Python
Learn what cause the subprocess-exited-with-error in Python and also learn how to solve the error in your code.
Python Integer Division: The Floor Division Operator Explained
Article on Python Integer Division operator (//) with examples and the difference between standard division and floor division.
