[project @ 1998-01-30 16:57:33 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
22          showEFloat, 
23          showFFloat, 
24          showGFloat, 
25          showFloat,
26          readFloat, 
27          
28          floatToDigits,
29          lexDigits
30
31         ) where
32
33 import PrelBase
34 import PrelMaybe
35 import ArrBase
36 import PrelNum
37 import PrelRead
38
39 \end{code}
40
41 %*********************************************************
42 %*                                                       *
43 \subsection[Numeric-signatures]{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 \end{code}
85
86 Controlling the format and precision of floats. The code that
87 implements the formatting itself is in @PrelNum@ to avoid
88 mutual module deps.
89
90 \begin{code}
91 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
92 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
93 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
94
95 showEFloat d x =  showString (formatRealFloat FFExponent d x)
96 showFFloat d x =  showString (formatRealFloat FFFixed d x)
97 showGFloat d x =  showString (formatRealFloat FFGeneric d x)
98
99 \end{code}