Class for types whose values can be decoded into the form
m * 2^e with an Integer mantissa m and an Int exponent e.
Minimal complete definition: one of decode and decodeL.
It is strongly recommended to override the default implementation of showDigits if the datatype allows distinguishing values without using an exact representation.
Methods
decode :: a -> (Integer, Int)decode is analogous to decodeFloat.
decodeL :: a -> (Int, Integer, Int)decodeL gives the integer base-
2logarithm of the mantissa in addition to the result of decode. If the absolute value of the mantissa always has the same highest set bit (excepting0), specifying that as a constant will be faster than calculating the logarithm for each individual mantissa. Ifx = m*2^ewithm /= 0, thendecodeL x == (integerLog2 (abs m), m, e)must hold.showDigits :: a -> IntThe number of significant digits needed to uniquely determine the value (or however many digits are desired). Usually, showDigits will be a constant function, but that is not necessary. However, all values of showDigits must be positive.
If the mantissa always has the same highest bit,
highBit, set when it is nonzero,showDigits _ = 2 + floor ((highBit+1) * logBase 10 2)is sufficient to make the values and formatted Strings uniquely determine each other and in general this is the smallest number to achieve that (calculate the number once and supply the result as a constant).
If the highest set bit of nonzero mantissae varies, things are not so easy. If the width of mantissae is bounded, plugging the largest possible value into the above formula works, but may yield an unduly large number for common cases. Using the formula with
highBitdetermined by the mantissa almost works, but if the representation is rounded at all, with sufficiently many bits in the mantissa, there will be values between the original and the representation. So, with mantissae of width varying over a large range, the only feasible way of obtaining a bijection between values and their decimal representations is printing to full precision in general, optionally capping at the upper limit.The default implementation prints values exactly, which in general is undesirable because it involves huge Integers and long representations.