Skip to content

Instantly share code, notes, and snippets.

@spockz
Created March 31, 2010 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spockz/350193 to your computer and use it in GitHub Desktop.
Save spockz/350193 to your computer and use it in GitHub Desktop.
solveQuadratic :: Double -> Double -> Double -> Intersections
solveQuadratic a b c =
case compare discr 0.0 of
LT -> []
EQ -> [-b / (2 * a)]
GT -> [abc (-), abc (+)]
where discr = b ^ 2 - 4 * a * c
abc op = (-b `op` sqrt discr) / (2 * a)
-- vs
solveQuadratic :: Double -> Double -> Double -> [Double]
solveQuadratic a b c =
case compare discr 0.0 of
LT -> []
EQ -> [-b / (2 * a)]
GT -> [abc (-), abc (+)]
where discr = b ^ 2 - 4 * a * c
sqrtdiscr = sqrt discr
a2 = 2*a
abc op = (-b `op` sqrtdiscr) / a2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment