number-length-0.2.0.0: Number of digits in a number in decimal and hexadecimal representation.

Copyright(c) 2015-2016 Peter Trško
LicenseBSD3
Stabilityexperimental
PortabilityDefaultSignatures, NoImplicitPrelude
Safe HaskellSafe
LanguageHaskell2010

Data.NumberLength

Description

Polymorphic interface for getting number of digits of a number in decimal or hexadecimal representation.

Synopsis

Documentation

class NumberLength a where Source #

Get number of digits of a number in base 10 and base 16. Note the following:

  • There is no Num constraint, so that type wrappers aren't forced to provide instance for it. This is because there are things represented using numbers, but they aren't numbers, e.g. telephone numbers.
  • This type class doesn't handle signed numbers, in an intuitive way. See also SignedNumberLength.
  • There is a special class for bounded numbers, see BoundedNumberLength, that provides similar functionality as Bounded, but for number of digits in a number.

Minimal complete definition

numberLength, numberLengthHex

Methods

numberLength :: a -> Int Source #

Get number of digits in base 10 for specified number. Note that if number is signed, then this function will return length of its absolute value.

>>> numberLength (123 :: Int)
3
>>> numberLength (-123 :: Int)
3

See also signedNumberLength.

numberLengthHex :: a -> Int Source #

Get number of digits in base 16 for specified number. Note that if number is signed, then this function will return length of its absolute value.

>>> numberLengthHex (123 :: Int)  -- 123 = 7b in hex
2
>>> numberLengthHex (-123 :: Int)
2

See also signedNumberLengthHex.

class NumberLength a => SignedNumberLength a where Source #

Get number of digits of a signed number in base 10 and base 16.

Minimal complete definition

signedNumberLengthHex

Methods

signedNumberLength :: a -> Int Source #

Get number of digits in base 10 for specified number.

>>> signedNumberLength (123 :: Int)
3
>>> signedNumberLength (-123 :: Int)
4

Default implementation provided if a has also Num and Ord instances:

signedNumberLength n = signLength + numberLength n
  where
    signLength = if n < 0 then 1 else 0

signedNumberLength :: (Num a, Ord a) => a -> Int Source #

Get number of digits in base 10 for specified number.

>>> signedNumberLength (123 :: Int)
3
>>> signedNumberLength (-123 :: Int)
4

Default implementation provided if a has also Num and Ord instances:

signedNumberLength n = signLength + numberLength n
  where
    signLength = if n < 0 then 1 else 0

signedNumberLengthHex :: a -> Int Source #

Get number of digits in base 16 for specified number.

>>> signedNumberLengthHex (123 :: Int)
2
>>> signedNumberLengthHex (-123 :: Int)
16

Negative number is shown as ones' complement, e.g. (-123 :: Int) = ffffffffffffff85 on 64 bit platform.

class NumberLength a => BoundedNumberLength a where Source #

Get maximum number of digits of a number in base 10 and 16. Minimal number of digits is considered to be always 1, and therefore there is no method for it.

Minimal complete definition

maxNumberLength, maxNumberLengthHex

Methods

maxNumberLength :: proxy a -> Int Source #

Get maximum number of digits of a number in base 10.

maxNumberLengthHex :: proxy a -> Int Source #

Get maximum number of digits of a number in base 16.