- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to find the arccosine of the given value
This tutorial will help us in finding arccosine of the given value. The arccosine is the inverse function of the cosine. If given a value between -1 and 1, it returns the angle (in radians) whose cosine is equal to that value.
For example, the cosine of pi/3 radians is equal to 0.5. Therefore, if you pass 0.5 as an input to the arccosine function, it should return pi/3 radians.
Syntax
acos(angle)
Here, acos() is a built-in function and value is passed as parameter to compute the arccosine of the value passed.
Method 1: Finding arccosine using in-built acos() function
The acos() function in Haskell works only in radians. In this method,the value passed to the acos() function must be [-1,1]. If the value is outside of the range [-1, 1], an error message is displayed.
Algorithm
Step 1 − Program execution will be started from the 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.
Step 3 − The constant value must be between -1 and 1 inclusive, otherwise the acos() will give the value NaN. This value is assigned to the variable “angle” by using the assignment operator.
Step 4 − The inbuilt arccosine function, acos() 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 acos() function.
Step 6 − The result is assigned to the “result” variable after acos() 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 arccosine of the value passed. This can be done by using in-built acos() function.
main = do let angle = 0.5 putStrLn "The value between -1 and 1, whose arccosine is to be computed is: " print (angle) let result=acos(angle) putStrLn "The resultant arccosine value is: " print (result)
Output
The value between -1 and 1, whose arccosine is to be computed is: 0.5 The resultant arccosine value is: 1.0471975511965979
Method 2: Finding arccosine using in-built acos() function for complex numbers
In Haskell, the “Data.Complex” module provides a ‘cis’ function that can be used to create complex numbers with a given phase angle, which we can use to calculate the arccosine of a complex number. The arccosine of a complex number is multi-valued, which means that there will be an infinite number of complex numbers whose cosine is equal to the input number.
Algorithm
Step 1 − A “Data.Complex” module is imported to work over complex numbers.
Step 2 − “cacos” function takes a complex number as input and uses acos(z) = -i*log(z + i*sqrt(1 - z^2)) to return the arccosine of that number.
Step 3 − Program execution will be started from main function. The main() function has whole control of the program.
Step 4 − A variable named “value” that contains the complex number , is being initialized. And a constant complex value is assigned to it.
Step 5 − The arccosine function definition for complex numbers that is contained or computed by cacos() function, is called.
Step 6 − “value” variable that contains the complex number, is passed as parameter to the cacos() function.
Step 7 − The result is assigned to the “result” variable after cacos() function computes the final resultant value and final output is displayed by printing the result value.
Example
In this example we are going to find arccosine using in-built acos() function for complex numbers.
import Data.Complex cacos :: Complex Double -> Complex Double cacos z = (negate 1 :+ 0) * log (z + (0 :+ 1) * sqrt (1 - z * z)) main :: IO () main = do let value = 2.3 :+ 4.5 let result = cacos value putStrLn $ "The arccosine of " ++ show value ++ " is " ++ show result --final result is
Output
The arccosine of 2.3 :+ 4.5 is (-2.31904592228528) :+ (-1.1060974844200153)
Conclusion
The arccosine value for the given value in Haskell can be calculated by using the acos() function. The value passed as parameter to this function must be between -1 and 1 inclusive.
The arccosine of a complex number is multi-valued and can be computed using cacos() function. The output of the program will depend on the chosen branch of the logarithm function and the interval of the angle.