Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

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:

 

Parameter of sqrt() Method in Ruby

Parameter Description
xAny 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


Output

2.23606797749979

 

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


Output

0+2.23606797749979i

 

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


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


Output

2.4567323635131153+1.0176118640880412i

 

Loading...
LearnAndLearn.com © 2018 - All rights reserved