Roots of a quadratic function: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: hlint, hindent, dropped a redundant map from the test, specified imports, added a main, updated output) |
|||
Line 781: | Line 781: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell>import Data.Complex |
<lang haskell>import Data.Complex (Complex, realPart) |
||
type CD = Complex Double |
type CD = Complex Double |
||
Line 787: | Line 787: | ||
quadraticRoots :: (CD, CD, CD) -> (CD, CD) |
quadraticRoots :: (CD, CD, CD) -> (CD, CD) |
||
quadraticRoots (a, b, c) = |
quadraticRoots (a, b, c) = |
||
if realPart b > 0 |
|||
then ((2*c) / (-b - d), (-b - d) / (2*a)) |
then ((2 * c) / (-b - d), (-b - d) / (2 * a)) |
||
else ((-b + d) / (2*a), (2*c) / (-b + d)) |
else ((-b + d) / (2 * a), (2 * c) / (-b + d)) |
||
where |
|||
d = sqrt $ b ^ 2 - 4 * a * c |
|||
main :: IO () |
|||
⚫ | |||
main = |
|||
⚫ | |||
mapM_ |
|||
(print . quadraticRoots) |
|||
⚫ | |||
{{Out}} |
|||
⚫ | |||
(0.3333333333333333 :+ 0.0,(-1.0) :+ 0.0) |
(0.3333333333333333 :+ 0.0,(-1.0) :+ 0.0) |
||
((-0.33333333333333326) :+ 0.4714045207910316,(-0.3333333333333333) :+ (-0.47140452079103173)) |
((-0.33333333333333326) :+ 0.4714045207910316,(-0.3333333333333333) :+ (-0.47140452079103173)) |