crackNum-2.3: Crack various integer, floating-point data formats

Copyright | (c) Levent Erkok
License | BSD3
Maintainer | erkokl@gmail.com
Stability | experimental
Safe Haskell | None
Language | Haskell2010

Data.Numbers.CrackNum

Contents

- Internal representation of a Floating-point numbers
- Creating FP values
- Displaying FP and Int/Word values
- Converting between floats and bit-representations
- Orphan instances

Description

A library for formatting/analyzing FP and Integer values

Synopsis

- data FP = FP {
  - intVal :: Integer
  - prec :: Precision
  - sign :: Bool
  - stExpt :: Int
  - bias :: Int
  - expt :: Int
  - fracBits :: [Bool]
  - bitLayOut :: String
  - kind :: Kind
  }
- data Precision
  - = HP
  - | SP
  - | DP
- data IPrecision
  - = W8
  - | I8
  - | W16
  - | I16
  - | W32
  - | I32
  - | W64
  - | I64
- data Kind
  - = Zero Bool
  - | Infty Bool
  - | SNaN
  - | QNaN
  - | Denormal
  - | Normal
- floatToFP :: Float -> FP
- doubleToFP :: Double -> FP
- stringToFP :: Precision -> String -> FP
- integerToFP :: Precision -> Integer -> FP
- displayFP :: FP -> String
- displayWord :: IPrecision -> Integer -> String
- floatToWord :: Float -> Word32
- wordToFloat :: Word32 -> Float
- doubleToWord :: Double -> Word64
- wordToDouble :: Word64 -> Double

# Internal representation of a Floating-point numbers

data FP Source #

Complete internal representation for a floating-point number

Constructors

FP |

Fields

- intVal :: Integer

  The value as represented as a full Integer. Storage purposes only.

- prec :: Precision

  FP precision.

- sign :: Bool

  Sign. If True then negative, otherwise positive.

- stExpt :: Int

  The exponent as it is stored.

- bias :: Int

  The implicit bias of the exponent.

- expt :: Int

  The actual exponent.

- fracBits :: [Bool]

  Bits in the fractional part

- bitLayOut :: String

  Layout representation

- kind :: Kind

  Floating-point kind (i.e., value)

#### Instances

Instances details

Show FP Source # | Show instance for FP
Instance details Defined in Data.Numbers.CrackNum Methods showsPrec :: Int -> FP -> ShowS Source # show :: FP -> String Source # showList :: [FP] -> ShowS Source #

data Precision Source #

Floating point precision

Constructors

HP | Half precision; 16 bits = 1 sign + 5 exponent + 10 mantissa
SP | Single precision; 32 bits = 1 sign + 8 exponent + 23 mantissa
DP | Double precision; 64 bits = 1 sign + 11 exponent + 52 mantissa

#### Instances

Instances details

Eq Precision Source # |
Instance details Defined in Data.Numbers.CrackNum.Data Methods (==) :: Precision -> Precision -> Bool Source # (/=) :: Precision -> Precision -> Bool Source #
Show Precision Source # |
Instance details Defined in Data.Numbers.CrackNum.Data Methods showsPrec :: Int -> Precision -> ShowS Source # show :: Precision -> String Source # showList :: [Precision] -> ShowS Source #

data IPrecision Source #

Integer/Word precision

Constructors

W8 | 8-bit unsigned (byte)
I8 | 8-bit signed
W16 | 16-bit unsigned (word)
I16 | 16-bit signed
W32 | 32-bit unsigned (double-word)
I32 | 32-bit signed
W64 | 64-bit unsigned (quad-word)
I64 | 64-bit signed

#### Instances

Instances details

Eq IPrecision Source # |
Instance details Defined in Data.Numbers.CrackNum.Data Methods (==) :: IPrecision -> IPrecision -> Bool Source # (/=) :: IPrecision -> IPrecision -> Bool Source #
Show IPrecision Source # | Show instance for integer-precisions
Instance details Defined in Data.Numbers.CrackNum.Data Methods showsPrec :: Int -> IPrecision -> ShowS Source # show :: IPrecision -> String Source # showList :: [IPrecision] -> ShowS Source #

data Kind Source #

Kinds of floating point values

Constructors

Zero Bool | Zero: 0. If Bool is true, then this is -0; otherwise +0.
Infty Bool | Infinity: oo. If Bool is true, then this is -oo, otherwie +oo.
SNaN | The signaling-NaN.
QNaN | The quiet-NaN.
Denormal | Denormalized number, i.e., leading bit is not 1
Normal | Normal value.

# Creating FP values

floatToFP :: Float -> FP Source #

Turn a Haskell float to the internal detailed FP representation

doubleToFP :: Double -> FP Source #

Turn a Haskell double to the internal detailed FP representation

stringToFP :: Precision -> String -> FP Source #

Convert the given string to a IEEE number with the required precision

integerToFP :: Precision -> Integer -> FP Source #

Crack a Haskell Integer value as the given precision floating value. The Integer should be the value corresponding to the bit-pattern as the float is laid out in memory according to the IEEE rules.

# Displaying FP and Int/Word values

displayFP :: FP -> String Source #

Display a Floating-point number in a nicely formatted way. (This function is also available through the `Show` instance for `FP`, but is provided here for symmetry with `displayWord`.)

displayWord :: IPrecision -> Integer -> String Source #

Display a Integer (signed/unsigned) number in a nicely formatted way

# Converting between floats and bit-representations

floatToWord :: Float -> Word32 Source #

Reinterpret-casts a `Float` to a `Word32`.

wordToFloat :: Word32 -> Float Source #

Reinterpret-casts a `Word32` to a `Float`.

doubleToWord :: Double -> Word64 Source #

Reinterpret-casts a `Double` to a `Word64`.

wordToDouble :: Word64 -> Double Source #

Reinterpret-casts a `Word64` to a `Double`.

# Orphan instances

Show FP Source # | Show instance for FP
Instance details Methods showsPrec :: Int -> FP -> ShowS Source # show :: FP -> String Source # showList :: [FP] -> ShowS Source #