What is Math.atan() in Ruby?

The atan() function returns the inverse tangent of a number. To be more specific, it returns the inverse tangent of a number in radians. It is also called the arc tangent function.

Figure 1 shows the mathematical representation of the atan() function and Figure 2 shows the visual representation of the atan() function.

Figure 1: Mathematical representation of inverse tangent function
Figure 2: Visual representation of inverse tangent function

This function is present in the Math module.

To convert radians to degrees, use:

degrees = radians * ( 180.0 / pi )

Syntax

atan(num)

Parameter

This function requires a number as a parameter.

Return value

atan() will return the inverse tangent of a number (in radians) that is sent as a parameter. The return value lies in the interval of [-pi/2,pi/2] radians.

Example

#positive number in radians
print "The value of atan(0.5) : ", Math.atan(0.5), " Radians\n"
#negative number in radians
print "The value of atan(-0.5) : ", Math.atan(-0.5), " Radians\n"
#applying atan() and then converting the result in radians to degrees
#radians = 1.0
#PI = 3.14159265
result = Math.atan(1.0) * (180.0 / Math::PI)
print "The value of atan(1.0) : ", result, " Degrees"

Free Resources