module Foundation.Numerical
( IsIntegral(..)
, IsNatural(..)
, Signed(..)
, Additive(..)
, Subtractive(..)
, Multiplicative(..)
, IDivisible(..)
, Divisible(..)
, Sign(..)
, recip
, IntegralRounding(..)
, FloatingPoint(..)
) where
import Basement.Compat.Base
import Basement.Numerical.Number
import Basement.Numerical.Additive
import Basement.Numerical.Subtractive
import Basement.Numerical.Multiplicative
import Foundation.Numerical.Floating
import qualified Prelude
data Sign = SignNegative | SignZero | SignPositive
deriving (Eq)
orderingToSign :: Ordering -> Sign
orderingToSign EQ = SignZero
orderingToSign GT = SignNegative
orderingToSign LT = SignPositive
class Signed a where
abs :: a -> a
signum :: a -> Sign
instance Signed Integer where
abs = Prelude.abs
signum = orderingToSign . compare 0
instance Signed Int where
abs = Prelude.abs
signum = orderingToSign . compare 0
instance Signed Int8 where
abs = Prelude.abs
signum = orderingToSign . compare 0
instance Signed Int16 where
abs = Prelude.abs
signum = orderingToSign . compare 0
instance Signed Int32 where
abs = Prelude.abs
signum = orderingToSign . compare 0
instance Signed Int64 where
abs = Prelude.abs
signum = orderingToSign . compare 0
instance Signed Float where
abs = Prelude.abs
signum = orderingToSign . compare 0
instance Signed Double where
abs = Prelude.abs
signum = orderingToSign . compare 0
class IntegralRounding a where
roundUp :: Integral n => a -> n
roundDown :: Integral n => a -> n
roundTruncate :: Integral n => a -> n
roundNearest :: Integral n => a -> n
instance IntegralRounding Prelude.Rational where
roundUp = fromInteger . Prelude.ceiling
roundDown = fromInteger . Prelude.floor
roundTruncate = fromInteger . Prelude.truncate
roundNearest = fromInteger . Prelude.round
instance IntegralRounding Prelude.Double where
roundUp = fromInteger . Prelude.ceiling
roundDown = fromInteger . Prelude.floor
roundTruncate = fromInteger . Prelude.truncate
roundNearest = fromInteger . Prelude.round
instance IntegralRounding Prelude.Float where
roundUp = fromInteger . Prelude.ceiling
roundDown = fromInteger . Prelude.floor
roundTruncate = fromInteger . Prelude.truncate
roundNearest = fromInteger . Prelude.round