2 % (c) The AQUA Project, Glasgow University, 1998
5 \section[NumExts]{Misc numeric bits}
11 doubleToFloat -- :: Double -> Float
12 , floatToDouble -- :: Double -> Float
13 , showHex -- :: Integral a => a -> ShowS
14 , showOct -- :: Integral a => a -> ShowS
17 import Char (ord, chr)
22 import PrelBase (ord_0)
28 doubleToFloat :: Double -> Float
29 floatToDouble :: Float -> Double
32 doubleToFloat = primDoubleToFloat
33 floatToDouble = primFloatToDouble
35 doubleToFloat (D# d#) = F# (double2Float# d#)
36 floatToDouble (F# f#) = D# (float2Double# f#)
40 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
41 showIntAtBase base toChr n r
42 | n < 0 = error ("NumExts.showIntAtBase: applied to negative number " ++ show n)
44 case quotRem n base of { (n', d) ->
46 seq c $ -- stricter than necessary
50 if n' == 0 then r' else showIntAtBase base toChr n' r'
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) ->
58 case toChr d of { C# c# -> -- stricter than necessary
62 if n' == 0 then r' else showIntAtBase base toChr n' r'
66 showHex :: Integral a => a -> ShowS
69 showIntAtBase 16 (toChrHex) n r
72 | d < 10 = chr (ord_0 + fromIntegral d)
73 | otherwise = chr (ord 'a' + fromIntegral (d - 10))
75 showOct :: Integral a => a -> ShowS
78 showIntAtBase 8 (toChrOct) n r
79 where toChrOct d = chr (ord_0 + fromIntegral d)