Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Haskell help

Name: Anonymous 2010-05-05 18:52

here's a program I wrote...can anyone tell me what I'm doing wrong? inb4 use <insert fave language>

roots :: (Float, Float, Float) -> (Float, Float)
roots (a,b,c) = (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)
main :: IO Float -> IO
main = do{
           print "a = "; a <- readLn;
           print "b = "; b <- readLn;
           print "c = "; c <- readLn;
           ans <- roots (a,b,c); print ans;
         }

Name: Anonymous 2010-05-05 19:51

>>1
Easy solution, I hope.
x1 = e + (sqrt d) / (2 * a)
x2 = e - (sqrt d) / (2 * a)
d = (b * b) - (4 * a * c)
e = - b / (2 * a)
x1 = e + (sqrt d) / (2 * a)
x2 = e - (sqrt d) / (2 * a)

You should only have to make sure your math is correct.

Name: Anonymous 2010-05-05 20:04

solveCubic s1 s2 s3 = -- solve the polynomial  t^3 - s1 t^2 + s2 t - s3 = 0
 let w = exp(2*sqrt(-1)*pi/3) :: Complex Double
     k = 2*s1 - s1*s2 + 15*s3
     h = s1^6 - 9*s1^4*s2 + 27*s1^2*s2^2 - 27*s2^3
     yc = (1/2)*(k + sqrt(k^2 - 4*h))
     zc = (1/2)*(k - sqrt(k^2 - 4*h))
     y' = yc ** (1/3)
     z' = zc ** (1/3) in
    ( (1/3)*(s1 + y' + z')
    , (1/3)*(s1 + w*y' + w^2*z')
    , (1/3)*(s1 + w^2*y' + w*z')
    )

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List