Ruby Tutorial Ruby References

Ruby - Math.cos() Method

The Ruby Math.cos() method returns trigonometric cosine of an angle (angle should be in radians). The returned value will be of float type and in the range -1 through 1.

In the graph below, cos(x) vs x is plotted.

Cos Function




x Specify the angle in radian.

Return Value

Returns the trigonometric cosine of an angle.


In the example below, Math.cos() method is used to find out the trigonometric cosine of an angle.

#Math::PI - value of PI from Math module
pi = Math::PI

puts "Math.cos(pi/6) = #{Math.cos(pi/6)}"
puts "Math.cos(pi/4) = #{Math.cos(pi/4)}"
puts "Math.cos(pi/3) = #{Math.cos(pi/3)}"

The output of the above code will be:

Math.cos(pi/6) = 0.8660254037844387
Math.cos(pi/4) = 0.7071067811865476
Math.cos(pi/3) = 0.5000000000000001

❮ Ruby Math Module