What is the atan() function in Swift?

In Swift, the atan() function, also called the arc tangent function, is used to return the inverse tangent of a number.

Below is the mathematical representation of the atan() function:

Mathematical representation of inverse tangent function

Note: We need to import Foundation in our code to use the atan() function. We can import it like with the import Foundation command.

To convert radians to degrees, use the following formula:

degrees = radians * ( 180.0 / pi )

Syntax

atan(number)
//number can be real, float, or double.

Parameter

This function requires a number as a parameter.

Return value

This function returns the inverse tangent of a number that is sent as a parameter.

The return value lies in the interval [-PI/2, PI/2] radians.

Example

import Swift
import Foundation
//positive number in radians
print("The value of atan(0.5) :", atan(0.5), "Radians");
// negative number in radians
print("The value of atan(-0.5) :", atan(-0.5), "Radians");
//applying atan() and then converting the result in radians to degrees
// radians = 1.0
// PI = 3.14159265
print("The value of atan(1.0): ",atan(1.0) * (180.0 / Double.pi)," Degrees");

Explanation

  • Line 2: We add the Foundation header required for the atan() function.
  • Line 5: We use the atan() function to calculate the arc tangent of a positive number.
  • Line 8: We use the atan() function to calculate the arc tangent of a negative number.
  • Line 13: We use the atan() function to calculate the arc tangent of a positive number and convert the result to degrees.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources