math-extras-0.1.0.0: A variety of mathematical utilities

MaintainerZiyang Liu <free@cofree.io>
Safe HaskellSafe
LanguageHaskell2010

Math.Extras.Int

Contents

Description

Arithmetic on integral numbers.

Synopsis

Saturated arithmetic

saturatedFromInteger :: forall a. (Integral a, Bounded a) => Integer -> a Source #

Like fromInteger, but returns maxBound and minBound in case of overflow and underflow, respectively.

saturatedAdd :: (Integral a, Bounded a) => a -> a -> a Source #

Sum of two integral numbers. Returns maxBound and minBound in case of overflow and underflow, respectively.

saturatedSubtract :: (Integral a, Bounded a) => a -> a -> a Source #

Difference of two integral numbers. Returns maxBound and minBound in case of overflow and underflow, respectively.

saturatedMultiply :: (Integral a, Bounded a) => a -> a -> a Source #

Product of two integral numbers. Returns maxBound and minBound in case of overflow and underflow, respectively.

saturatedPow :: Int -> Int -> Int Source #

saturatedPow a b computes a ^ b. Returns maxBound and minBound in case of overflow and underflow, respectively.

NB: Like ^, the exponent must be non-negative.

Other helper functions

toBinaryString :: FiniteBits b => b -> NonEmpty Char Source #

Returns the bit representation of the input as a NonEmpty Char consisting of 0s and 1s, without leading zeros. The length of the result is at most finiteBitSize b.

Data.List.NonEmpty.toList (toBinaryString (100 :: Int)) == "1100100"
Data.List.NonEmpty.toList (toBinaryString (-1 :: Int8)) == "11111111"