[ create a new paste ] login | about

Link: http://codepad.org/1VZIecvt    [ raw code | output | fork ]

Haskell, pasted on Nov 4:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
data G = G { fs :: Int, sn :: Int } deriving (Show)

gsum a b = G (fs a + fs b) (sn a + sn b)
gdif a b = G (fs a - fs b) (sn a - sn b)

gmul a b = G (o a b) (i a b)
gdiv a b = G (x o a b) (x i a b)
    where n       = fromIntegral $ fs b ^ 2 + sn b ^ 2
          x f a b = round $ (fromIntegral $ f a b) / n
grem a b = let q = gdiv a b in gdif a (gmul q b)

o a b = fs a * fs b - sn a * sn b
i a b = fs a * sn b + sn a * fs b

main = let a = G 10 20; b = G 3 4 in
       (print $ (gsum a b, gdif a b, gmul a b, gdiv a b, grem a b)) >>
       (print $ gsum (grem a b) (gmul (gdiv a b) b))


Output:
1
2
(G {fs = 13, sn = 24},G {fs = 7, sn = 16},G {fs = -50, sn = 100},G {fs = -2, sn = 4},G {fs = 32, sn = 16})
G {fs = 10, sn = 20}


Create a new paste based on this one


Comments: