Ruby Tutorial Ruby References

Ruby - Math.atan2() Method



The Ruby Math.atan2() method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). The returned value will be of float type and in the range -𝜋 through 𝜋.

Syntax

Math.atan2(y, x)

Parameters

y Specify the ordinate coordinate.
x Specify the abscissa coordinate.

Return Value

Returns theta of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.

Example:

In the example below, Math.atan2() method is used to calculate the theta of a point.

puts "Math.atan2(10, 20) = #{Math.atan2(10, 20)}"
puts "Math.atan2(-10, 20) = #{Math.atan2(-10, 20)}"
puts "Math.atan2(10, -20) = #{Math.atan2(10, -20)}"
puts "Math.atan2(-10, -20) = #{Math.atan2(-10, -20)}"

The output of the above code will be:

Math.atan2(10, 20) = 0.4636476090008061
Math.atan2(-10, 20) = -0.4636476090008061
Math.atan2(10, -20) = 2.677945044588987
Math.atan2(-10, -20) = -2.677945044588987

❮ Ruby Math Module

5