GeeksforGeeks App
Open App
Browser
Continue

# Ruby | BigDecimal sqrt() function

BigDecimal#sqrt() : sqrt() is a BigDecimal class method which returns the square root of the value.

Syntax: BigDecimal.sqrt()

Parameter: BigDecimal values

Return: the square root of the value.

Example #1 :

 # Ruby code for BigDecimal.sqrt() method  # loading libraryrequire 'bigdecimal'  # declaring bigdecimala = BigDecimal("10")  # declaring bigdecimalb = BigDecimal("1000") *2  # declaring bigdecimalc = BigDecimal("11.43")  # sqrt() methodputs "BigDecimal a sqrt method : #{Math.sqrt(a)}\n\n"  puts "BigDecimal b sqrt method : #{Math.sqrt(b)}\n\n"  puts "BigDecimal a sqrt method : #{Math.sqrt(c)}\n\n"

Output :

BigDecimal a sqrt method : 3.1622776601683795

BigDecimal b sqrt method : 44.721359549995796

BigDecimal a sqrt method : 3.380828300875393

Example #2 :

 # Ruby code for BigDecimal.sqrt() method  # loading libraryrequire 'bigdecimal'  # declaring bigdecimala = BigDecimal('12')*12  # declaring bigdecimalb = BigDecimal('10')+(22 ** 7.1) ** 10  # declaring bigdecimalc = -BigDecimal('-3')  # sqrt() methodputs "BigDecimal a sqrt method : #{Math.sqrt(a)}\n\n"  puts "BigDecimal b sqrt method : #{Math.sqrt(b)}\n\n"  puts "BigDecimal a sqrt method : #{Math.sqrt(c)}\n\n"

Output :

BigDecimal a sqrt method : 12.0

BigDecimal b sqrt method : 4.5290297054731975e+47

BigDecimal a sqrt method : 1.7320508075688772

My Personal Notes arrow_drop_up