4.23. `NumExts`
Prev | Chapter 4. The `lang` package: language support | Next

## 4.23. `NumExts`

The `NumExts` interface collect together various numeric operations that have proven to be commonly useful

     -- Going between Doubles and Floats:
    doubleToFloat :: Double -> Float
    floatToDouble :: Float  -> Double

    showHex       :: Integral a => a -> ShowS
    showOct       :: Integral a => a -> ShowS
    showBin       :: Integral a => a -> ShowS

    showIntAtBase :: Integral a
    	      => a            -- base
    	      -> (a -> Char)  -- digit to char
    	      -> a            -- number to show.
    	      -> ShowS

    showListWith  :: (a -> ShowS) -> [a] -> ShowS

Notes:

- If `doubleToFloat` is applied to a `Double` that is within the representable range for `Float`, the result may be the next higher or lower representable `Float` value. If the `Double` is out of range, the result is undefined.

- No loss of precision occurs in the other direction with `floatToDouble`, the floating value remains unchanged.

- `showOct`, `showHex` and `showBin` will prefix `0o`, `0x` and `0b`, respectively. Like `Numeric.showInt`, these show functions work on positive numbers only.

- `showIntAtBase` is the more general function for converting a number at some base into a series of characters. The above `show*` functions use it, for instance, here's how `showHex` could be defined

      showHex :: Integral a => a -> ShowS
      showHex n r =
       showString "0x" $
       showIntAtBase 16 (toChrHex) n r
       where
        toChrHex d
          | d < 10    = chr (ord '0' + fromIntegral d)
          | otherwise = chr (ord 'a' + fromIntegral (d - 10))

- `showListWith` is strictly speaking not a '`NumExts`' kind of function, but it's sometimes useful in conjunction with the other `show*` functions that `NumExts` exports. It is the non-overloaded version of `showList`, allowing you to supply the `shows` function to use per list element. For instance,

      putStrLn (NumExts.showListWith NumExts.showHex [0..16])

  will print out the elements of `[0..16]` in hexadecimal form.

Prev | Up | Next
4.22. `MarshalUtils` | Home | 4.24. `PackedString`