[project @ 1999-05-11 17:05:43 by keithw]
[ghc-hetmet.git] / ghc / lib / std / Numeric.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1997-99
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          -- :: (RealFloat a) => Rational -> a
15         , showSigned       -- :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
16         , readSigned       -- :: (Real a) => ReadS a -> ReadS a
17         , showInt          -- :: Integral a => a -> ShowS
18         , readInt          -- :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
19         
20         , readDec          -- :: (Integral a) => ReadS a
21         , readOct          -- :: (Integral a) => ReadS a
22         , readHex          -- :: (Integral a) => ReadS a
23
24         , showEFloat       -- :: (RealFloat a) => Maybe Int -> a -> ShowS
25         , showFFloat       -- :: (RealFloat a) => Maybe Int -> a -> ShowS
26         , showGFloat       -- :: (RealFloat a) => Maybe Int -> a -> ShowS
27         , showFloat        -- :: (RealFloat a) => a -> ShowS
28         , readFloat        -- :: (RealFloat a) => ReadS a
29         
30          
31         , floatToDigits    -- :: (RealFloat a) => Integer -> a -> ([Int], Int)
32         , lexDigits        -- :: ReadS String
33
34           -- Implementation checked wrt. Haskell 98 lib report, 1/99.
35         ) where
36
37 import PrelBase
38 import PrelMaybe
39 import PrelArr
40 import PrelNum
41 import PrelNumExtra
42 import PrelRead
43 import PrelErr ( error )
44
45 \end{code}
46
47 \begin{code}
48 showInt :: Integral a => a -> ShowS
49 showInt i rs
50   | i < 0     = error "Numeric.showInt: can't show negative numbers"
51   | otherwise = go i rs
52     where
53      go n r = 
54       case quotRem n 10 of                 { (n', d) ->
55       case chr (ord_0 + fromIntegral d) of { C# c# -> -- stricter than necessary
56       let
57         r' = C# c# : r
58       in
59       if n' == 0 then r' else go n' r'
60       }}
61 \end{code}
62
63 Controlling the format and precision of floats. The code that
64 implements the formatting itself is in @PrelNum@ to avoid
65 mutual module deps.
66
67 \begin{code}
68 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
69 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
70 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
71
72 showEFloat d x =  showString (formatRealFloat FFExponent d x)
73 showFFloat d x =  showString (formatRealFloat FFFixed d x)
74 showGFloat d x =  showString (formatRealFloat FFGeneric d x)
75
76 \end{code}