當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Math cos()用法及代碼示例


cos()是Ruby中的內置函數,返回以弧度表示的給定角度的餘弦值,其範圍為[-inf,+ inf]。返回的值在[-1,+1]範圍內。

用法:Math.cos(value) 

參數:該函數接受一個強製性參數值,其返回相應的餘弦值。


返回值:返回餘弦值。

例子1

#Ruby program for cos() function 
  
#Assigning values 
val1 = 1 val2 = 0.5 val3 = -1 val4 = -0.9 
  
#Prints the cos() value 
                                      puts Math.cos(val1) 
                                          puts Math.cos(val2) 
                                              puts Math.cos(val3) 
                                                  puts Math.cos(val4)

輸出

 0.5403023058681398
0.8775825618903728
0.5403023058681398
0.6216099682706644

例子2

#Ruby program for cos() function 
  
#Assigning values 
val1 = 109 val2 = 133 val3 = -13243 val4 = -9898 
  
#Prints the cos() value 
                                            puts Math.cos(val1) 
                                                puts Math.cos(val2) 
                                                    puts Math.cos(val3) 
                                                        puts Math.cos(val4)

輸出

-0.577002178942952
0.4948722204034305
-0.37447564566939956
-0.40075875500376773

參考:https://devdocs.io/ruby~2.5/math#method-c-cos



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Math cos() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。