📜  红宝石 |数学 sqrt()函数

📅  最后修改于: 2022-05-13 01:55:00.959000             🧑  作者: Mango

红宝石 |数学 sqrt()函数

sqrt()是 Ruby 中的内置函数,返回给定值的平方根。

示例 1

#Ruby program for sqrt() function
  
#Assigning values
val1 = 4 val2 = 9 val3 = 64 val4 = 100
  
#Prints the sqrt() value
                                   puts Math.sqrt(val1)
                                       puts Math.sqrt(val2)
                                           puts Math.sqrt(val3)
                                               puts Math.sqrt(val4)

输出

2.0
3.0
8.0
10.0

示例 2

#Ruby program for sqrt() function
  
#Assigning values
val1 = 40 val2 = 19 val3 = 164 val4 = 1200
  
#Prints the sqrt() value
                                      puts Math.sqrt(val1)
                                          puts Math.sqrt(val2)
                                              puts Math.sqrt(val3)
                                                  puts Math.sqrt(val4)

输出

6.324555320336759
4.358898943540674
12.806248474865697
34.64101615137755

参考:https://devdocs.io/ruby~2.5/math#method-c-sqrt