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.
26 import Char (ord, chr)
31 import PrelBase (ord_0)
37 doubleToFloat :: Double -> Float
38 floatToDouble :: Float -> Double
41 doubleToFloat = primDoubleToFloat
42 floatToDouble = primFloatToDouble
44 doubleToFloat (D# d#) = F# (double2Float# d#)
45 floatToDouble (F# f#) = D# (float2Double# f#)
49 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
50 showIntAtBase base toChr n r
51 | n < 0 = error ("NumExts.showIntAtBase: applied to negative number " ++ show n)
53 case quotRem n base of { (n', d) ->
55 seq c $ -- stricter than necessary
59 if n' == 0 then r' else showIntAtBase base toChr n' r'
62 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
63 showIntAtBase base toChr n r
64 | n < 0 = error ("NumExts.showIntAtBase: applied to negative number " ++ show n)
66 case quotRem n base of { (n', d) ->
67 case toChr d of { C# c# -> -- stricter than necessary
71 if n' == 0 then r' else showIntAtBase base toChr n' r'
75 showHex :: Integral a => a -> ShowS
78 showIntAtBase 16 (toChrHex) n r
81 | d < 10 = chr (ord_0 + fromIntegral d)
82 | otherwise = chr (ord 'a' + fromIntegral (d - 10))
84 showOct :: Integral a => a -> ShowS
87 showIntAtBase 8 (toChrOct) n r
88 where toChrOct d = chr (ord_0 + fromIntegral d)
90 showBin :: Integral a => a -> ShowS
93 showIntAtBase 2 (toChrOct) n r
94 where toChrOct d = chr (ord_0 + fromIntegral d)