Ruby CMath cbrt() 方法与示例
cbrt 方法是 Ruby 中的 Math 类方法,用于计算给定值的立方根。
Syntax: Math.cbrt(z)
Parameter: Here, z is the value whose cube root is to be calculated.
Returns: This method returns the cube root of the given number.
下面的程序说明了上述方法的使用:
示例 1:
# Ruby code for cbrt() method
require 'cmath'
# Initializing value
a = 27
b = 5.45
# Printing result
puts "Cube root value of a : #{CMath.cbrt(a)}\n\n"
puts "cube root value of b : #{CMath.cbrt(b)}\n\n"
输出:
Cube root value of a : 3.0
cube root value of b : 1.7598088635408027
示例 2:
# Ruby code for cbrt() method
require 'cmath'
# Initializing values
a = 78
b = 729
# Printing result
puts "Cube root value of a : #{CMath.cbrt(a)}\n\n"
puts "cube root value of b : #{CMath.cbrt(b)}\n\n"
输出:
Cube root value of a : 4.272658681697917
cube root value of b : 8.999999999999998