[ create a new paste ] login | about

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

johannes - Haskell, pasted on Dec 13:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module Main where

sine :: Double -> Double
sine x 
  | x < -pi = sine (x + 2*pi)
  | x >= pi = sine (x - 2*pi)
  | otherwise = x' * foldr f 0 [1, 3 .. 11]
  where
    facs = scanl (*) 1 [1 .. ]
    f n r = 1/fromIntegral (facs !! n) - x2*r
    x2 = x'*x'
    x' | x >  pi/2 =  pi - x   --  pi/2 <  x <  pi
       | x < -pi/2 = -pi - x   -- -pi   <= x < -pi/2
       | otherwise =  x        -- -pi/2 <= x <= pi/2

main :: IO ()
main = mapM_ print [sine (2*pi*x/20) | x <- [0 .. 19]]


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0.0
0.309016994374947
0.587785252292092
0.809016994300917
0.951056513189244
0.999999943741051
0.951056513189244
0.809016994300917
0.587785252292092
0.309016994374947
0.0
-0.309016994374947
-0.587785252292092
-0.809016994300917
-0.951056513189244
-0.999999943741051
-0.951056513189244
-0.809016994300917
-0.587785252292092
-0.309016994374947


Create a new paste based on this one


Comments: