Haskell Program to find the arctangent of the given value


This tutorial will help us in finding the arctangent of the given value. The arctangent is the inverse function of the tangent. The tangent of an angle is defined as the ratio of the length of the side opposite the angle to the length of the side adjacent to the angle in a right triangle. The arctangent, therefore, gives the measure of an angle (in radians) whose tangent is a given value.

Syntax

atan(angle)

Here, atan() is a built-in function and value is passed as parameter to compute the arctangent of the value passed. The arctangent is a periodic function, with a period of π, so the range of the arctangent function is (-π/2, π/2) for the input value of tangent.

Method 1: Finding arctangent using in-built atan() function

In this method,the atan() function calculates the principal value of the arctangent, which is the angle in the range (-π/2, π/2).

Algorithm

  • Step 1 − Program execution will be started from main function. The main() function has whole control of the program.

  • Step 2 − A variable named, “angle” is being initialized. Initially, it will have the garbage value. Then, a constant value is being assigned to it. This value is assigned to the variable “angle” by using the assignment operator.

  • Step 3 − The angle value passed is then printed by using “putStrLn” statement.

  • Step 4 − The inbuilt arctangent function, atan() is called. It’s definition is already available in the standard library.

  • Step 5 − “Angle” variable that contains the value is passed as parameter to the atan() function.

  • Step 6 − The result is assigned to the “result” variable after atan() function computes the final resultant value and final output is displayed by printing the result value.

Example

In this example, we are going to see that how we can find the arctangent of the value passed. This can be done by using in-built atan() function.

main = do 
let angle = 1 
putStrLn "The value whose arctangent is to be computed is:  "
print (angle)
let result=atan(angle) 
putStrLn "The resultant arctangent value is:  "
print (result)

Output

The value whose arctangent is to be computed is:  
1.0
The resultant arctangent value is:  
0.7853981633974483

Method 2: Finding arctangent using in-built atan2() function

In this method,arctan2 function uses built-in atan2() function from the “Prelude” module. Here, atan2 function takes two arguments y and x which represent point's y and x coordinates, and calculates the angle between the positive x-axis and the point in the range (-π, π]. This function is especially useful in cases when x can be zero and the normal arctan function would not have enough information to determine the correct angle of the point (x, y)

Algorithm

  • Step 1 − A “Data.Fixed” and “Numeric” module is imported.

  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program.

  • Step 3 − Two variables named, ‘y’ and ‘x’ that will contain the coordinate value of a point , is being initialized. And a constant value is assigned to it.

  • Step 4 − The arctangent function atan2() or arctan2() that contains the function definition to compute the arctangent of the coordinate value passed, is called.

  • Step 5 − The ‘y’ and ‘x’ coordinates are passed as parameter to the atan2() or arctan2() function.

  • Step 6 − The result is assigned to the “result” variable after atan2() or arctan2() function computes the final resultant value and final output is displayed by printing the result value.

Example

import Data.Fixed
import Numeric

arctan2 :: Double -> Double -> Double
arctan2 y x = atan2 y x
main :: IO ()
main = do
let y = 3 
let x = 4 

let result = arctan2 y x
putStrLn "The resultant arctangent value is:  "
print (result)  

Output

The resultant arctangent value is:  
0.6435011087932844

Conclusion

The arctangent value for the given value in Haskell can be calculated by using the atan() function. For computing the arctangent value of a point (x,y), atan2() or arctan2() function is used. The atan2() or arctan2() function returns the angle whose tangent is the quotient of its arguments ( y/x) and is defined over the range of [-π, π]. While atan() function returns the angle whose tangent is the quotient of its arguments defined over the range of [-π/2, π/2].

Updated on: 20-Jan-2023

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements