[project @ 1998-01-22 11:07:36 by sof]
[ghc-hetmet.git] / ghc / lib / required / Numeric.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1997-98
3 %
4 \section[Numeric]{Numeric interface}
5
6 Odds and ends, mostly functions for reading and showing
7 \tr{RealFloat}-like kind of values.
8
9
10 \begin{code}
11 {-# OPTIONS -fno-implicit-prelude #-}
12 module Numeric
13         (
14          fromRat,
15          showSigned, 
16          readSigned, 
17          showInt, 
18          readInt,
19
20          readDec, readOct, readHex,
21          showDec, showOct, showHex,
22
23          showEFloat, 
24          showFFloat, 
25          showGFloat, 
26          showFloat,
27          readFloat, 
28          
29          floatToDigits,
30          lexDigits
31
32         ) where
33
34 import PrelBase
35 import PrelMaybe
36 import ArrBase
37 import PrelNum
38 import PrelRead
39
40 \end{code}
41
42 %*********************************************************
43 %*                                                       *
44 \subsection[Numeric-signatures]{Signatures}
45 %*                                                       *
46 %*********************************************************
47
48 Interface on offer:
49
50 \begin{pseudocode}
51 fromRat    :: (RealFloat a) => Rational -> a
52
53 showSigned :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
54 readSigned :: (Real a) => ReadS a -> ReadS a
55
56 showInt    :: Integral a => a -> ShowS
57 readInt    :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
58
59 readDec    :: (Integral a) => ReadS a
60 readOct    :: (Integral a) => ReadS a
61 readHex    :: (Integral a) => ReadS a
62
63 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
64 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
65 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
66 showFloat     :: (RealFloat a) => a -> ShowS
67  
68 readFloat      :: (RealFloat a) => ReadS a
69
70 floatToDigits  :: (RealFloat a) => Integer -> a -> ([Int], Int)
71 lexDigits      :: ReadS String
72 \end{pseudocode}
73
74 \begin{code}
75 showInt :: Integral a => a -> ShowS
76 showInt n r
77   = case quotRem n 10 of                 { (n', d) ->
78     case chr (ord_0 + fromIntegral d) of { C# c# -> -- stricter than necessary
79     let
80         r' = C# c# : r
81     in
82     if n' == 0 then r' else showInt n' r'
83     }}
84
85 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
86 showIntAtBase base toChr n r
87   = case quotRem n base of { (n', d) ->
88     case toChr d        of { C# c# -> -- stricter than necessary
89     let
90         r' = C# c# : r
91     in
92     if n' == 0 then r' else showIntAtBase base toChr n' r'
93     }}
94
95 showDec :: Integral a => a -> ShowS
96 showDec n r = 
97  showIntAtBase 10 
98                (\ d -> chr (ord_0 + fromIntegral d)) 
99                n r
100
101 showHex :: Integral a => a -> ShowS
102 showHex n r = 
103  showString "0x" $
104  showIntAtBase 16 (toChrHex) n r
105  where  
106   toChrHex d
107     | d < 10    = chr (ord_0   + fromIntegral d)
108     | otherwise = chr (ord 'a' + fromIntegral (d - 10))
109
110 showOct :: Integral a => a -> ShowS
111 showOct n r = 
112  showString "0o" $
113  showIntAtBase 8 (toChrOct) n r
114  where toChrOct d = chr (ord_0   + fromIntegral d)
115
116 \end{code}
117
118 Controlling the format and precision of floats. The code that
119 implements the formatting itself is in @PrelNum@ to avoid
120 mutual module deps.
121
122 \begin{code}
123 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
124 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
125 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
126
127 showEFloat d x =  showString (formatRealFloat FFExponent d x)
128 showFFloat d x =  showString (formatRealFloat FFFixed d x)
129 showGFloat d x =  showString (formatRealFloat FFGeneric d x)
130
131 \end{code}