2 % (c) The AQUA Project, Glasgow University, 1998
5 \section[NumExts]{Misc numeric bits}
11 doubleToFloat -- :: Double -> Float
12 , floatToDouble -- :: Double -> Float
14 , showHex -- :: Integral a => a -> ShowS
15 , showOct -- :: Integral a => a -> ShowS
16 , showBin -- :: Integral a => a -> ShowS
18 -- general purpose number->string converter.
19 , showIntAtBase -- :: Integral a
21 -- -> (a -> Char) -- digit to char
22 -- -> a -- number to show.
24 , showListWith -- :: (a -> ShowS)
29 import Char (ord, chr)
34 import PrelNum ( ord_0 )
35 import PrelShow( showList__ )
41 doubleToFloat :: Double -> Float
42 floatToDouble :: Float -> Double
45 doubleToFloat = primDoubleToFloat
46 floatToDouble = primFloatToDouble
48 doubleToFloat (D# d#) = F# (double2Float# d#)
49 floatToDouble (F# f#) = D# (float2Double# f#)
53 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
54 showIntAtBase base toChr n r
55 | n < 0 = error ("NumExts.showIntAtBase: applied to negative number " ++ show n)
57 case quotRem n base of { (n', d) ->
59 seq c $ -- stricter than necessary
63 if n' == 0 then r' else showIntAtBase base toChr n' r'
66 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
67 showIntAtBase base toChr n r
68 | n < 0 = error ("NumExts.showIntAtBase: applied to negative number " ++ show n)
70 case quotRem n base of { (n', d) ->
71 case toChr d of { C# c# -> -- stricter than necessary
75 if n' == 0 then r' else showIntAtBase base toChr n' r'
79 showHex :: Integral a => a -> ShowS
82 showIntAtBase 16 (toChrHex) n r
85 | d < 10 = chr (ord_0 + fromIntegral d)
86 | otherwise = chr (ord 'a' + fromIntegral (d - 10))
88 showOct :: Integral a => a -> ShowS
91 showIntAtBase 8 (toChrOct) n r
92 where toChrOct d = chr (ord_0 + fromIntegral d)
94 showBin :: Integral a => a -> ShowS
97 showIntAtBase 2 (toChrOct) n r
98 where toChrOct d = chr (ord_0 + fromIntegral d)
101 Easy enough to define by the user, but since it's
102 occasionally useful (when, say, printing out a
103 list of hex values), we define and export it
107 showListWith :: (a -> ShowS) -> [a] -> ShowS
108 showListWith = showList__