[project @ 2001-08-22 11:45:06 by sewardj]
[ghc-hetmet.git] / ghc / tests / programs / ipoole_spec_class / Lognum.lhs
1 MODIFICATIONS
2 -------------
3 07-04-94   ipoole  added pi method for Lognum
4
5 SCCS: %W% %G%
6
7 A packeage for log representations of numbers.
8
9 > module Lognum where
10 > import Lib
11
12 > data Lognum = LN Double
13
14 > instance Num Lognum where
15 >       LN x * LN y = LN (x+y)
16 >       LN x + LN y = LN (d+(mylog 1 (exp (x-d) + exp (y-d))))
17 >                     where d = max x y
18 >       LN x - LN y = if y > x then error "subtract LN" else
19 >                    LN (d+(mylog 2 (exp (x-d) - exp (y-d))))
20 >                     where d = x
21 >       fromInteger 0 = LN (-1.0e99)
22 >       fromInteger x = LN (mylog 3 (fromInteger x))
23
24 > instance Ord Lognum where
25 >       LN x > LN y = x > y
26 >       a <= b = not  (a > b)
27
28 > instance Eq Lognum where
29 >       LN x == LN y = x == y
30
31 > instance Floating Lognum where
32 >       sqrt (LN x) = LN (x/2.0)
33 >       (LN x) ** (LN y) = (LN (x * exp y))
34 >       pi = (LN (log pi))
35
36 > instance Fractional Lognum where
37 >       LN x / LN y = LN (x-y)
38 >       fromRational x = if x == toRational 0.0 then fromInteger 0 
39 >                        else LN (mylog 4 (toDouble x))
40
41
42 > instance Enum Lognum where
43 >    enumFrom n       = iterate ((fromRational 1.0)+) n
44 >    enumFromThen n m = iterate ((m-n)+) n
45
46
47 > instance Real Lognum where
48 >       toRational (LN x) = toRational (exp x)
49
50 > toLognum :: Real a => a -> Lognum
51 > toLognum = fromRational . toRational
52
53 > instance RealFloat Lognum
54
55 > instance RealFrac Lognum
56
57 > instance Show{-was:Text-} Lognum 
58
59 > mylog :: Int -> Double -> Double
60 > mylog n x = if toDouble x < 0.0 then error ("mylog" ++ show n) else log x
61
62
63