Ruby CMath sqrt() 方法与示例
sqrt 方法是 Ruby 中的 Math 类方法,用于计算给定值的平方根。
Syntax: Math.sqrt(z)
Parameter: Here, z is the value whose square root is to be calculated.
Returns: This method returns the square root of the given number.
下面的程序说明了上述方法的使用:
示例 1:
# Ruby code for sqrt() method
require 'cmath'
# Initialize values
a = 9
b = 78.15
# Printing result
puts "Square root value of a : #{CMath.sqrt(a)}\n\n"
puts "Square root value of b : #{CMath.sqrt(b)}\n\n"
输出:
Square root value of a : 3.0
Square root value of b : 8.84024886527523
示例 2:
# Ruby code for sqrt() method
require 'cmath'
# Initializing value
a = 7841
b = 1234
# Printing result
puts "Sqrt root value of a : #{CMath.sqrt(a)}\n\n"
puts "Sqrt root value of b : #{CMath.sqrt(b)}\n\n"
输出:
Sqrt root value of a : 88.54942122905152
Sqrt root value of b : 35.12833614050059