Ruby Tutorial Ruby References

Ruby - Math.sin() Method



The Ruby Math.sin() method returns trigonometric sine 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, sin(x) vs x is plotted.

Sin Function

Syntax

Math.sin(x)

Parameters

x Specify the angle in radian.

Return Value

Returns the trigonometric sine of an angle.

Example:

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

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

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

The output of the above code will be:

Math.sin(pi/6) = 0.49999999999999994
Math.sin(pi/4) = 0.7071067811865475
Math.sin(pi/3) = 0.8660254037844386

❮ Ruby Math Module