# Ruby – Find Square Root (Non-Negative) – sqrt() Method with Examples

You can use sqrt() method to find the square root of any given complex number in Ruby programming. The given number should be non-negative. sqrt() method exists in CMath library of Ruby.

## sqrt() method in Ruby

sqrt() method exists in Standard sqrt() Library of Ruby. The purpose of this method is to obtain the non-negative square root of any given complex number we cmath.sqrt() method.

## Syntax of the sqrt() Method in Ruby

Syntax of sqrt() method is:

cmath.sqrt(x)

## Parameter of sqrt() Method in Ruby

 Parameter Description x Any valid complex number. This parameter is required.

Note: If the x parameter is not a complex number, sqrt() method will return an error.

## Return Value of sqrt()

sqrt() method will return the non-negative square root of any given complex number.

## Ruby – Get the sqrt() of any Number Example 1

# import CMath library
require 'cmath'
x = CMath.sqrt(5)
puts x

Output

2.23606797749979

## Ruby – Get the sqrt() of any Number Example 2

# import CMath library
require 'cmath'
x = CMath.sqrt(-5)
puts x

Output

0+2.23606797749979i

## Ruby – Get the sqrt() of any Number Example 3

# import CMath library
require 'cmath'
x = CMath.sqrt(a5)
puts x

Output

Error

Note: Any invalid complex number as sowing in the above example, always gives error.

## Ruby – Get the sqrt() of any Number Example 4

# import CMath library
require 'cmath'
x = CMath.sqrt(5 + 5i)
puts x

Output

2.4567323635131153+1.0176118640880412i