[project @ 1999-05-18 14:59:04 by simonpj]
[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 PrelShow
40 import PrelArr
41 import PrelNum
42 import PrelNumExtra
43 import PrelRead
44 import PrelErr ( error )
45
46 \end{code}
47
48 \begin{code}
49 showInt :: Integral a => a -> ShowS
50 showInt i rs
51   | i < 0     = error "Numeric.showInt: can't show negative numbers"
52   | otherwise = go i rs
53     where
54      go n r = 
55       case quotRem n 10 of                 { (n', d) ->
56       case chr (ord_0 + fromIntegral d) of { C# c# -> -- stricter than necessary
57       let
58         r' = C# c# : r
59       in
60       if n' == 0 then r' else go n' r'
61       }}
62 \end{code}
63
64 Controlling the format and precision of floats. The code that
65 implements the formatting itself is in @PrelNum@ to avoid
66 mutual module deps.
67
68 \begin{code}
69 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
70 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
71 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
72
73 showEFloat d x =  showString (formatRealFloat FFExponent d x)
74 showFFloat d x =  showString (formatRealFloat FFFixed d x)
75 showGFloat d x =  showString (formatRealFloat FFGeneric d x)
76
77 \end{code}