module Data.ExtendedReal
( Extended (..)
, inf
, isFinite
, isInfinite
) where
import Prelude hiding (isInfinite)
import Control.DeepSeq
import Data.Data
import Data.Hashable
import Data.Typeable
data Extended r
= NegInf
| Finite !r
| PosInf
deriving (Ord, Eq, Show, Read, Typeable, Data)
instance Bounded (Extended r) where
minBound = NegInf
maxBound = PosInf
instance Functor Extended where
fmap _ NegInf = NegInf
fmap f (Finite x) = Finite (f x)
fmap _ PosInf = PosInf
instance NFData r => NFData (Extended r) where
rnf (Finite x) = rnf x
rnf _ = ()
instance Hashable r => Hashable (Extended r) where
hashWithSalt s NegInf = s `hashWithSalt` (0::Int)
hashWithSalt s (Finite x) = s `hashWithSalt` (1::Int) `hashWithSalt` x
hashWithSalt s PosInf = s `hashWithSalt` (2::Int)
inf :: Extended r
inf = PosInf
isFinite :: Extended r -> Bool
isFinite (Finite _) = True
isFinite _ = False
isInfinite :: Extended r -> Bool
isInfinite = not . isFinite
instance (Num r, Ord r) => Num (Extended r) where
Finite a + Finite b = Finite (a+b)
PosInf + NegInf = error "PosInf + NegInf is undefined"
NegInf + PosInf = error "NegInf + PosInf is undefined"
PosInf + _ = PosInf
_ + PosInf = PosInf
NegInf + _ = NegInf
_ + NegInf = NegInf
Finite x1 * e = scale x1 e
e * Finite x2 = scale x2 e
PosInf * PosInf = PosInf
PosInf * NegInf = NegInf
NegInf * PosInf = NegInf
NegInf * NegInf = PosInf
negate NegInf = PosInf
negate (Finite x) = Finite (negate x)
negate PosInf = NegInf
abs NegInf = PosInf
abs (Finite x) = Finite (abs x)
abs PosInf = PosInf
signum NegInf = Finite (1)
signum (Finite x) = Finite (signum x)
signum PosInf = Finite 1
fromInteger = Finite . fromInteger
instance (Fractional r, Ord r) => Fractional (Extended r) where
recip (Finite x) = Finite (1/x)
recip _ = Finite 0
fromRational = Finite . fromRational
scale :: (Num r, Ord r) => r -> Extended r -> Extended r
scale a e = seq e $
case a `compare` 0 of
EQ -> Finite 0
GT ->
case e of
NegInf -> NegInf
Finite b -> Finite (a*b)
PosInf -> PosInf
LT ->
case e of
NegInf -> PosInf
Finite b -> Finite (a*b)
PosInf -> NegInf