1 module Numbers(Number) where
    2 data Number = Tolerant Float
    3 -- if you are not using the Chalmers Haskell B compiler then remove the @
    4 {- Number is the same as Float except that Number uses a comparison
    5    tolerance eps.
    6 -}
    7 instance Eq Number where
    8         Tolerant a == Tolerant b       = abs(a-b) < eps
    9         Tolerant a /= Tolerant b       = abs(a-b) > eps
   10 instance Ord Number where
   11         Tolerant a <= Tolerant b       = a-eps < b
   12         Tolerant a < Tolerant b               = a < b-eps
   13 instance Num Number where
   14         Tolerant a + Tolerant b               = Tolerant (a+b)
   15         Tolerant a - Tolerant b               = Tolerant (a-b)
   16         Tolerant a * Tolerant b               = Tolerant (a*b)
   17         negate (Tolerant a)       = Tolerant (-a)
   18         fromInteger n           = Tolerant (fromInteger n)
   19 instance Fractional Number where
   20         Tolerant a / Tolerant b               = Tolerant (a/b)
   21 instance Floating Number where
   22         sqrt (Tolerant a)           = Tolerant (sqrt a)
   23 {- Allow both integral and floating denotations for numbers -}
   24 instance Read Number where
   25         readsPrec p s = [(Tolerant n,t) | (n,t) <- readsPrec p s] -- ++
   26                      --[(Tolerant (fromInteger n),t) | (n,t) <- readsPrec p s]
   27 instance Show Number where
   28         showsPrec p (Tolerant x) = showsPrec p x
   29 eps     = 0.0001 :: Float