numeric-logarithms-0.1.0.0: Efficient rounded log2 algorithms on integral and rational types.

Numeric.Logarithms

Description

Rounded base-2 logarithms of Integral and Real types.

Synopsis

# Real Logarithms

log2Floor :: (HasCallStack, Real a) => a -> Int Source #

Returns the floor of the base-2 logarithm of a Real argument.

log2Ceiling :: (HasCallStack, Real a) => a -> Int Source #

Returns the ceiling of the base-2 logarithm of a Real argument.

log2Approx :: (HasCallStack, Real a) => a -> (Int, Ordering) Source #

Returns an approximate base-2 logarithm of the argument.

The returned Int is one of the two nearest integers to the exact result, and the returned Ordering tells whether the exact result is greater than, equal to, or less than the Int. LT means the exact result is less than the rounded result.

This effectively gives results like "between 7 and 8" and leaves it up to the caller to decide how (or whether) to round the result. See also log2With for a version that will always round, but leaves the particular rounding strategy up to the caller.

log2With :: (HasCallStack, Real a) => (Int -> Ordering -> Int) -> a -> Int Source #

Returns the base-2 logarithm with custom rounding.

The first parameter is a rounding adjustment function: given the approximate result and an Ordering indicating its relation to the exact result, return the rounded result.

This could be useful if you want something other than floor or ceil, e.g. round-towards-0, round-towards-even, etc.

# Integer Logarithms

ilog2Floor :: (HasCallStack, Integral a) => a -> Int Source #

Returns the floor of the base-2 logarithm of an Integral argument.

ilog2Ceiling :: (HasCallStack, Integral a) => a -> Int Source #

Returns the ceiling of the base-2 logarithm of an Integral argument.

Returns the approximate base-2 logarithm of an Integral argument.
Returns the base-2 logarithm of an Integral with custom rounding.