# How to Calculate Python Square Root?

The square root of a given number x is a number that multiplied by itself gives x. Mathematically, the roots of x2  are x and -x.

In this article, the focus will be on the temporal analysis of the square root in python using a variety of methods.

Time analysis is calculated using decorator @time_decorator. All values are displayed in nanoseconds (ns).

## Square roots using python

Every positive number has 2 square roots (positive and negative). In this blog post, we consider both positive and negative integers and their result.

Why are we even talking about roots? The roots have always been computationally difficult to compute (in time). With newer and more powerful CPUs this problem has been reduced to a minimum.

There are multiple methods of computing these roots.

We will try to explore the differences between each and give some understandable summary using python.

So what is a square root in python? The square root in python is a number that multiplied by itself gives a squared number.

In this blog post, we will consider 5 methods and they are summarized here,

Each of these methods has some advantages depending on the use case.

• math: `math.sqrt(x)`
• cmath: `cmath.sqrt(x)`
• ** operator (power operator): `x**0.5`
• NumPy: `numpy.sqrt(x)`
• pow: `pow(x, 0.5)`

For basic usage consider math library.

The pros and cons of every method are described here.

## Python square root using math library

Math module is one of the most used libraries within python. By default, a variety of methods is defined for more or less all the mathematical functions used. One of those methods is sqrt().

According to the documentation, `sqrt(x)` returns the rooted number x.

Example 1.

```import math

x = math.sqrt(4)
print(f"The square root of 4 = {x} and type of x is {type(x).__name__}")
```
`Output: The square root of 4 = 2.0 and type of x is float`

As you can see `math.sqrt(x)` returns the float by default.

Let’s look at the following python square root example.

Example 2.

```import math

x = math.sqrt(-4)
print(f"The square root of -4 = {x} and type of x is {type(x).__name__}")
```
`Output: ValueError: math domain error`

Math module does not support rooting negative numbers and ValueError occurs. Rooting zero works as intended as it returns 0.

Math library documentation

### Time analysis of math.sqrt function

As already mentioned python square roots time analysis will be displayed in nanoseconds (ns) to be as accurate as possible.

Example 1.

```@time_decorator
def math_lib_time_example(number):
return math.sqrt(number)

print(f"The square root of 4 = {math_lib_time_example(4)}")
```
```Output:
Total execution time: 2669 ns
The square root of 4 = 2.0```

Execution time can and must vary depending on the computer’s specifications. In my example, during one execution, the execution time lasted 2669 ns.

The average execution time based on 100 executions is 2354 ns.

## Python square root using cmath library

Cmath is an extension of the standard math module that supports working with complex numbers.

Example 1.

```import cmath

x = cmath.sqrt(4)
print(f"The square root of 4 = {x} and type of x is {type(x).__name__}")
```
`Output: The square root of 4 = (2+0j) and type of x is complex`

`cmath.sqrt(x)` returns complex by default.

Let’s look at the following python square root example.

Example 2.

```import cmath

x = cmath.sqrt(-4)
print(f"The square root of -4 = {x} and type of x is {type(x).__name__}")
```
`Output: The square root of -4 = 2j and type of x is complex`

Here, unlike the standard math module, there is no ValueError.

Cmath library documentation

### Time analysis of cmath.sqrt function

Example 1.

```@time_decorator
def cmath_lib_time_example(number):
return cmath.sqrt(number)

print(f"The square root of 4 = {cmath_lib_time_example(4)}”)
```
```Output:
Total execution time: 16665 ns
The square root of 4 = (2+0j)```

The average execution time based on 100 executions is 14085 ns.

## Python square root using ** operator

The Double star operator (**) is one of Python’s built-in arithmetic operators. In the official python documentation also referred to as the power operator. It works similarly to the pow function that we will cover in the next section.

Example 1.

```x = 4 ** 0.5
print(f"The square root of 4 = {x} and type of x is {type(x).__name__}")
```
`Output: The square root of 4 = 2.0 and type of x is float`

If we root a positive number we get a float as a result.

Let’s look at what happens when we root a negative number:

Example 2.

```x =(-4)**0.5   #parentheses are important because ** operator has an advantage over multiplication
print(f"The square root of -4 = {x} and type of x is {type(x).__name__}")
```
`Output: The square root of -4 = (1.2246467991473532e-16+2j) and type of x is complex`

Here we get a complex number as with the cmath module but a slightly different result, interesting? 🙂

### Time analysis of ** operator

Example 1.

```@time_decorator
def double_star_time_example(number):
return number**0.5

print(f"The square root of 4 = {double_star_time_example(4)})
```
```Output:
Total execution time: 10332 ns
The square root of 4 = 2.0```

The average execution time based on 100 executions is 11069 ns.

## Python square root using NumPy

Numpy is a popular library for mathematical calculations in python. Numpy offers all kinds of data manipulation tools and built-in functions.

We will focus on `numpy.sqrt(x)`.

Example 1.

```import numpy

x = numpy.sqrt(4)
print(f"The square root of 4 = {x} and type of x is {type(x).__name__}")
```
`Output: The square root of 4 = 2.0 and type of x is float64`

`numpy.sqrt(x)` returns the float64 class by default. Float64 is a NumPy class built into the NumPy library.

Let’s look at the following python square root example in NumPy.

Example 2.

```import numpy

x = numpy.sqrt(-4)
print(f"The square root of -4 = {x} and type of x is {type(x).__name__}")
```
```Output:
RuntimeWarning: invalid value encountered in sqrt x = numpy.sqrt(-4)
The square root of -4 = nan and type of x is float64```

This method does not support negative numbers but does not throw an error but a warning. The return value is, therefore, nan (not a number) of class float64.

NumPy documentation

### Time analysis of numpy.sqrt function

Example 1.

```@time_decorator
def numpy_time_example(number):
return numpy.sqrt(number)

print(f"The square root of 4 = {numpy_time_example(4)}")
```
```Output:
Total execution time: 5912 ns
The square root of 4 = 2.0```

The average execution time based on 100 executions is 6099 ns.

## Python square root using pow

As we have already mentioned the pow function works similarly to the ** operator, so let’s compare results.

Example 1.

```x = pow(4, 0.5)
print(f"The square root of 4 = {x} and type of x is {type(x).__name__}")
```
`Output: The square root of 4 = 2.0 and type of x is float`

As you can see `pow(x)` returns float by default.

Let’s look at the following python square root example.

Example 2.

```x = pow(-4, 0.5)
print(f"The square root of -4 = {x} and type of x is {type(x).__name__}")
```
`Output: The square root of -4 = (1.2246467991473532e-16+2j) and type of x is complex`

Same result as double star (**) operator 🙂

We expect similar time analysis results but let’s check just in case.

Pow function documentation

### Time analysis of pow

Example 1.

```@time_decorator
def pow_time_example(number):
return pow(number, 0.5)

print(f"The square root of 4 = {pow_time_example(4)}”)
```
```Output:
Total execution time: 11561 ns
The square root of 4 = 2.0```

The average execution time based on 100 executions is 10883 ns.

We see that the temporal analysis also showed that the pow and ** operators are the same things.

## Summary

In this section, I will make a comparison of all 5 methods of calculating python square root.

Personally, for calculating square roots in python, I would use the math library.

Math library turns out to be most time-efficient while containing the same complexity of usage.

In case we need complex numbers, I would use cmath library for the reason that it outputs the result in a slightly nicer format than ** operator and pow().

```System specs:

CPU: Intel® Core™ i5-7200U CPU @ 2.50GHz × 4
GPU: NV117 / Mesa Intel® HD Graphics 620
RAM: 8GB

Python 3.8.10
[GCC 9.3.0] on Linux```