[project @ 1997-09-20 19:32:26 by sof]
[ghc-hetmet.git] / ghc / lib / required / Numeric.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1997
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 ArrBase
36 import PrelNum
37 import PrelRead
38
39 \end{code}
40
41 %*********************************************************
42 %*                                                      *
43 \subsection{Signatures}
44 %*                                                      *
45 %*********************************************************
46
47 Interface on offer:
48
49 \begin{pseudocode}
50 fromRat    :: (RealFloat a) => Rational -> a
51
52 showSigned :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
53 readSigned :: (Real a) => ReadS a -> ReadS a
54
55 showInt    :: Integral a => a -> ShowS
56 readInt    :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
57
58 readDec    :: (Integral a) => ReadS a
59 readOct    :: (Integral a) => ReadS a
60 readHex    :: (Integral a) => ReadS a
61
62 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
63 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
64 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
65 showFloat     :: (RealFloat a) => a -> ShowS
66  
67 readFloat      :: (RealFloat a) => ReadS a
68
69 floatToDigits  :: (RealFloat a) => Integer -> a -> ([Int], Int)
70 lexDigits      :: ReadS String
71 \end{pseudocode}
72
73 \begin{code}
74 showInt :: Integral a => a -> ShowS
75 showInt n r
76   = case quotRem n 10 of                 { (n', d) ->
77     case chr (ord_0 + fromIntegral d) of { C# c# -> -- stricter than necessary
78     let
79         r' = C# c# : r
80     in
81     if n' == 0 then r' else showInt n' r'
82     }}
83
84 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
85 showIntAtBase base toChr n r
86   = case quotRem n base of { (n', d) ->
87     case toChr d        of { C# c# -> -- stricter than necessary
88     let
89         r' = C# c# : r
90     in
91     if n' == 0 then r' else showIntAtBase base toChr n' r'
92     }}
93
94 showDec :: Integral a => a -> ShowS
95 showDec n r = 
96  showIntAtBase 10 
97                (\ d -> chr (ord_0 + fromIntegral d)) 
98                n r
99
100 showHex :: Integral a => a -> ShowS
101 showHex n r = 
102  showString "0x" $
103  showIntAtBase 16 (toChrHex) n r
104  where  
105   toChrHex d = 
106    if d < 10 then
107       chr (ord_0   + fromIntegral d)
108    else
109       chr (ord 'a' + fromIntegral (d - 10))
110
111 showOct :: Integral a => a -> ShowS
112 showOct n r = 
113  showString "0o" $
114  showIntAtBase 8 (toChrOct) n r
115  where toChrOct d = chr (ord_0   + fromIntegral d)
116
117 \end{code}
118
119 \begin{code}
120 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
121 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
122 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
123
124 showEFloat d x =  showString (formatRealFloat FFExponent d x)
125 showFFloat d x =  showString (formatRealFloat FFFixed d x)
126 showGFloat d x =  showString (formatRealFloat FFGeneric d x)
127
128 \end{code}
129       
130   
131