Ruby BigDecimal sqrt()函数
BigDecimal#sqrt() : sqrt()是BigDecimal类的一个方法,用于返回数值的平方根。
语法。BigDecimal.sqrt()
参数。BigDecimal的值
返回:数值的平方根。
例子 #1 :
# Ruby code for BigDecimal.sqrt() method
# loading library
require 'bigdecimal'
# declaring bigdecimal
a = BigDecimal("10")
# declaring bigdecimal
b = BigDecimal("1000") *2
# declaring bigdecimal
c = BigDecimal("11.43")
# sqrt() method
puts "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"
输出:
BigDecimal a sqrt method : 3.1622776601683795
BigDecimal b sqrt method : 44.721359549995796
BigDecimal a sqrt method : 3.380828300875393
例子 #2 :
# Ruby code for BigDecimal.sqrt() method
# loading library
require 'bigdecimal'
# declaring bigdecimal
a = BigDecimal('12')*12
# declaring bigdecimal
b = BigDecimal('10')+(22 ** 7.1) ** 10
# declaring bigdecimal
c = -BigDecimal('-3')
# sqrt() method
puts "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"
输出:
BigDecimal a sqrt method : 12.0
BigDecimal b sqrt method : 4.5290297054731975e+47
BigDecimal a sqrt method : 1.7320508075688772