Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Ruby | Math cos() function

  • Last Updated : 07 Jan, 2020

The cos() is an inbuilt function in Ruby returns the cosine value of a given angle expressed in radians which is in range [-inf, +inf]. The returned value is in range [-1, +1].

Syntax: Math.cos(value)

Parameters: The function accepts one mandatory parameter value whose corresponding cosine value is returned.

Return Value: It returns the cosine value.

Example 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)

Output:

 0.5403023058681398
0.8775825618903728
0.5403023058681398
0.6216099682706644

Example 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)

Output:

-0.577002178942952
0.4948722204034305
-0.37447564566939956
-0.40075875500376773

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :