Implement a Haskell function to solve the quadratic equation ax^2 + bx + c = 0 q
ID: 3876288 • Letter: I
Question
Implement a Haskell function to solve the quadratic equation ax^2 + bx + c = 0quad a b c =
The function takes the three coefficients of the equation and produces a tuple containing the two solutions of the equation. For example, to solve the equation x^2 + 4 x + 3 = 0 , apply the following call:
quad 1 4 3 (-1.0,-3.0)
You may use the Prelude function sqrt to find the square root of a number. Implement a Haskell function to solve the quadratic equation ax^2 + bx + c = 0
quad a b c =
The function takes the three coefficients of the equation and produces a tuple containing the two solutions of the equation. For example, to solve the equation x^2 + 4 x + 3 = 0 , apply the following call:
quad 1 4 3 (-1.0,-3.0)
You may use the Prelude function sqrt to find the square root of a number. Implement a Haskell function to solve the quadratic equation ax^2 + bx + c = 0
quad a b c =
The function takes the three coefficients of the equation and produces a tuple containing the two solutions of the equation. For example, to solve the equation x^2 + 4 x + 3 = 0 , apply the following call:
quad 1 4 3 (-1.0,-3.0)
You may use the Prelude function sqrt to find the square root of a number.
Explanation / Answer
roots :: float -> float -> float -> (float,float)
roots a b c = if d < 0 then
error " can not solve for complex roots"
else (x1,x2)
where x1= e + sqrt d / (2*a)
x2 = e + sqrt d / (2*a)
d = b*b-4*a*c
e = -b/(2*a)
Sqrt d = prelude > sqrt d
Here we take float values for three coefficients and two floats for the roots.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.