How to resolve the algorithm Trigonometric functions step by step in the Haskell programming language
How to resolve the algorithm Trigonometric functions step by step in the Haskell programming language
Table of Contents
Problem Statement
If your language has a library or built-in functions for trigonometry, show examples of: using the same angle in radians and degrees. For the non-inverse functions, each radian/degree pair should use arguments that evaluate to the same angle (that is, it's not necessary to use the same angle for all three regular functions as long as the two sine calls use the same angle). For the inverse functions, use the same number and convert its answer to radians and degrees. If your language does not have trigonometric functions available or only has some available, write functions to calculate the functions based on any known approximation or identity.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Trigonometric functions step by step in the Haskell programming language
The code is a Haskell program that prints the values of the sine, cosine, and tangent functions for both radians and degrees.
The fromDegrees
and toDegrees
functions convert between degrees and radians. The fromDegrees
function takes an angle in degrees and returns the equivalent angle in radians, and the toDegrees
function takes an angle in radians and returns the equivalent angle in degrees.
The main
function uses the mapM_
function to apply the print
function to a list of values. The list of values includes the sine, cosine, and tangent of both radians and degrees.
The output of the program is as follows:
0.5
0.5
0.8660254037844386
0.8660254037844386
0.5773502691896257
0.5773502691896257
0.5
30.0
0.8660254037844387
30.0
0.5773502691896258
30.0
Source code in the haskell programming language
fromDegrees :: Floating a => a -> a
fromDegrees deg = deg * pi / 180
toDegrees :: Floating a => a -> a
toDegrees rad = rad * 180 / pi
main :: IO ()
main =
mapM_
print
[ sin (pi / 6)
, sin (fromDegrees 30)
, cos (pi / 6)
, cos (fromDegrees 30)
, tan (pi / 6)
, tan (fromDegrees 30)
, asin 0.5
, toDegrees (asin 0.5)
, acos 0.5
, toDegrees (acos 0.5)
, atan 0.5
, toDegrees (atan 0.5)
]
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Haskell programming language
You may also check:How to resolve the algorithm Repeat step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the REXX programming language
You may also check:How to resolve the algorithm HTTP step by step in the Pike programming language
You may also check:How to resolve the algorithm Loops/While step by step in the ALGOL-M programming language