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
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
where d = sqrt $ b^2 - 4*a*c</lang>
d = sqrt $ b ^ 2 - 4 * a * c


main :: IO ()
<pre>*Main> mapM_ print $ map quadraticRoots [(3, 4, 4/3), (3, 2, -1), (3, 2, 1), (1, -10e5, 1), (1, -10e9, 1)]
main =
((-0.6666666666666666) :+ (-0.0),(-0.6666666666666666) :+ 0.0)
mapM_
(print . quadraticRoots)
[(3, 4, 4 / 3), (3, 2, -1), (3, 2, 1), (1, -10e5, 1), (1, -10e9, 1)]</lang>
{{Out}}
<pre>((-0.6666666666666666) :+ 0.0,(-0.6666666666666666) :+ 0.0)
(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))