[project @ 1997-05-18 04:04:30 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 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11 module Numeric
12         (
13          fromRat,
14          showSigned, 
15          readSigned, 
16          showInt, 
17          readInt,
18
19          readDec, readOct, readHex,
20          showDec, showOct, showHex,
21
22          showEFloat, 
23          showFFloat, 
24          showGFloat, 
25          showFloat,
26          readFloat, 
27          
28          floatToDigits,
29          lexDigits
30
31         ) where
32
33 import PrelBase
34 import ArrBase
35 import PrelNum
36 import PrelRead
37
38 \end{code}
39
40 %*********************************************************
41 %*                                                      *
42 \subsection{Signatures}
43 %*                                                      *
44 %*********************************************************
45
46 Interface on offer:
47
48 \begin{pseudocode}
49 fromRat    :: (RealFloat a) => Rational -> a
50
51 showSigned :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
52 readSigned :: (Real a) => ReadS a -> ReadS a
53
54 showInt    :: Integral a => a -> ShowS
55 readInt    :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
56
57 readDec    :: (Integral a) => ReadS a
58 readOct    :: (Integral a) => ReadS a
59 readHex    :: (Integral a) => ReadS a
60
61 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
62 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
63 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
64 showFloat     :: (RealFloat a) => a -> ShowS
65  
66 readFloat      :: (RealFloat a) => ReadS a
67
68 floatToDigits  :: (RealFloat a) => Integer -> a -> ([Int], Int)
69 lexDigits      :: ReadS String
70 \end{pseudocode}
71
72 \begin{code}
73 showInt :: Integral a => a -> ShowS
74 showInt n r
75   = case quotRem n 10 of                 { (n', d) ->
76     case chr (ord_0 + fromIntegral d) of { C# c# -> -- stricter than necessary
77     let
78         r' = C# c# : r
79     in
80     if n' == 0 then r' else showInt n' r'
81     }}
82
83 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
84 showIntAtBase base toChr n r
85   = case quotRem n base of { (n', d) ->
86     case toChr d        of { C# c# -> -- stricter than necessary
87     let
88         r' = C# c# : r
89     in
90     if n' == 0 then r' else showIntAtBase base toChr n' r'
91     }}
92
93 showDec :: Integral a => a -> ShowS
94 showDec n r = 
95  showIntAtBase 10 
96                (\ d -> chr (ord_0 + fromIntegral d)) 
97                n r
98
99 showHex :: Integral a => a -> ShowS
100 showHex n r = 
101  showString "0x" $
102  showIntAtBase 16 (toChrHex) n r
103  where  
104   toChrHex d = 
105    if d < 10 then
106       chr (ord_0   + fromIntegral d)
107    else
108       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 \begin{code}
119 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
120 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
121 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
122
123 showEFloat d x =  showString (formatRealFloat FFExponent d x)
124 showFFloat d x =  showString (formatRealFloat FFFixed d x)
125 showGFloat d x =  showString (formatRealFloat FFGeneric d x)
126
127 \end{code}
128       
129   
130