[project @ 1999-03-17 13:19:19 by simonm]
[ghc-hetmet.git] / ghc / docs / libraries / NumExts.sgml
1 <sect> <idx/NumExts/
2 <label id="sec:NumExts">
3 <p>
4
5 The <tt/NumExts/ interface collect together various numeric
6 operations that have proven to be commonly useful 
7
8 <tscreen> <verb>
9 -- Going between Doubles and Floats:
10 doubleToFloat :: Double -> Float
11 floatToDouble :: Float  -> Double
12
13 showHex       :: Integral a => a -> ShowS
14 showOct       :: Integral a => a -> ShowS
15 showBin       :: Integral a => a -> ShowS
16
17 showIntAtBase :: Integral a 
18               => a            -- base
19               -> (a -> Char)  -- digit to char
20               -> a            -- number to show.
21               -> ShowS
22 </verb> </tscreen>
23
24 Notes: 
25 <itemize>
26 <item>
27     If <tt/doubleToFloat/ is applied to a <tt/Double/ that is within
28     the representable range for <tt/Float/, the result may be the next
29     higher or lower representable <tt/Float/ value. If the <tt/Double/
30     is out of range, the result is undefined.
31 <item>
32     No loss of precision occurs in the other direction with
33     <tt/floatToDouble/, the floating value remains unchanged.
34 <item>
35     <tt/showOct/, <tt/showHex/ and <tt/showBin/ will prefix <tt/0o/,
36     <tt/0x/ and <tt/0b/, respectively. Like <tt/Numeric.showInt/,
37     these show functions work on positive numbers only.
38 <item>
39     <tt/showIntAtBase/ is the more general function for converting
40     a number at some base into a series of characters. The above
41     <tt/show*/ functions use it, for instance, here's how <tt/showHex/
42     could be defined
43
44 <tscreen><verb>
45
46 </verb></tscreen>
47 showHex :: Integral a => a -> ShowS
48 showHex n r = 
49  showString "0x" $
50  showIntAtBase 16 (toChrHex) n r
51  where  
52   toChrHex d
53     | d < 10    = chr (ord '0' + fromIntegral d)
54     | otherwise = chr (ord 'a' + fromIntegral (d - 10))
55 </itemize>