summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-03-26 00:15:48 +0200
committerMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-03-26 00:15:48 +0200
commit8613f7154f815835fc5e6ddbe15e9aebc62109b1 (patch)
treee07b8b77378d6bd99ac83f7615c1fac775419008
parentd01a91022cc55841cf968a60ecee8f27b57b88e2 (diff)
downloadffp-8613f7154f815835fc5e6ddbe15e9aebc62109b1.tar.gz
ffp-8613f7154f815835fc5e6ddbe15e9aebc62109b1.tar.bz2
ffp-8613f7154f815835fc5e6ddbe15e9aebc62109b1.zip
Fixed different values for h-function
Using Integer instead of Int solved the difference in values for the different h-functions. Apparently they have equal performance. Added basic testcases.
-rw-r--r--AufgabeFFP2.hs18
-rw-r--r--TestAufgabeFFP2.hs69
2 files changed, 78 insertions, 9 deletions
diff --git a/AufgabeFFP2.hs b/AufgabeFFP2.hs
index 9e466b7..7993c01 100644
--- a/AufgabeFFP2.hs
+++ b/AufgabeFFP2.hs
@@ -56,23 +56,23 @@ facGen = map (fac) [1..]
56 56
57-- function g with memoization (using hFast) 57-- function g with memoization (using hFast)
58fMT :: Int -> Int -> Float 58fMT :: Int -> Int -> Float
59fMT z k = g z k hFast 59fMT z k = g z k hMT
60 60
61-- function g without memoization (uning hSlow) 61-- function g without memoization (uning hSlow)
62f :: Int -> Int -> Float 62f :: Int -> Int -> Float
63f z k = g z k hSlow 63f z k = g z k h
64 64
65-- actual function g 65-- actual function g (converts Int to Integer for more precision)
66g :: Int -> Int -> (Int -> Int -> Float) -> Float 66g :: Int -> Int -> (Integer -> Integer -> Float) -> Float
67g z k h = sum $ map (h z) [1..k] 67g z k h = sum $ map (h $ fromIntegral z) [1..(fromIntegral k)]
68 68
69-- helper function h using mem-table for the power-series (z^i) and for faculty (i!) 69-- helper function h using mem-table for the power-series (z^i) and for faculty (i!)
70hFast :: Int -> Int -> Float 70hMT :: Integer -> Integer -> Float
71hFast z i = (fromInteger $ pofNs (fromIntegral z) !! i) / (fromInteger $ facGen !! (i-1)) 71hMT z i = (fromInteger $ pofNs z !! (fromInteger i)) / (fromInteger $ facGen !! ((fromIntegral i)-1))
72 72
73-- helper function h without memoization 73-- helper function h without memoization
74hSlow :: Int -> Int -> Float 74h :: Integer -> Integer -> Float
75hSlow z i = (fromIntegral $ z^i) / (fromInteger $ fac $ fromIntegral i) 75h z i = (fromInteger $ z^i) / (fromInteger $ fac i)
76 76
77------------------------------------------------------------------------------- 77-------------------------------------------------------------------------------
78 78
diff --git a/TestAufgabeFFP2.hs b/TestAufgabeFFP2.hs
new file mode 100644
index 0000000..0df8612
--- /dev/null
+++ b/TestAufgabeFFP2.hs
@@ -0,0 +1,69 @@
1module Main where
2
3import Test.HUnit
4import Control.Monad
5import AufgabeFFP2
6
7assertBoolF :: String -> Bool -> Assertion
8assertBoolF msg b = when b (assertFailure msg)
9
10-------------------------------------------------------------------------------
11-- 1
12
13pps1 :: Test
14pps1 = TestCase (assertEqual "pps"
15 [(3,5),(5,7),(11,13),(17,19),(29,31),(41,43), (59,61),(71,73),(101,103),(107,109)]
16 (take 10 pps))
17
18pps2 :: Test
19pps2 = TestCase (assertEqual "pps"
20 (347,349)
21 (pps!!20))
22
23pps3 :: Test
24pps3 = TestCase (assertEqual "pps"
25 (809,811)
26 (head (drop 30 pps)))
27
28ppsTests = TestList [pps1, pps2, pps3]
29
30-------------------------------------------------------------------------------
31-- 2
32
33-------------------------------------------------------------------------------
34-- 3
35
36-------------------------------------------------------------------------------
37-- 4
38
39gz1 :: Test
40gz1 = TestCase (assertEqual "gz"
41 144
42 (gz 42))
43
44gz2 :: Test
45gz2 = TestCase (assertEqual "gz"
46 400
47 (gz 402))
48
49gzs1 :: Test
50gzs1 = TestCase (assertEqual "gzs"
51 144
52 (gzs!!41))
53
54gzs2 :: Test
55gzs2 = TestCase (assertEqual "gzs"
56 400
57 (gzs!!401))
58
59
60gzTests = TestList [gz1, gz2, gzs1, gzs2]
61
62-------------------------------------------------------------------------------
63
64tests :: [Test]
65tests = [ppsTests, gzTests]
66
67main = do
68 forM tests $ \test ->
69 runTestTT test