919e832ff1d0fb6e51f318396484a97aeba0e3d2
[ghc-hetmet.git] / ghc / lib / std / 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 PrelArr
36 import PrelNum
37 import PrelRead
38 import PrelErr ( error )
39
40 \end{code}
41
42 %*********************************************************
43 %*                                                       *
44 \subsection[Numeric-signatures]{Signatures}
45 %*                                                       *
46 %*********************************************************
47
48 Interface on offer:
49
50 \begin{pseudocode}
51 fromRat    :: (RealFloat a) => Rational -> a
52
53 showSigned :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
54 readSigned :: (Real a) => ReadS a -> ReadS a
55
56 showInt    :: Integral a => a -> ShowS
57 readInt    :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
58
59 readDec    :: (Integral a) => ReadS a
60 readOct    :: (Integral a) => ReadS a
61 readHex    :: (Integral a) => ReadS a
62
63 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
64 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
65 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
66 showFloat     :: (RealFloat a) => a -> ShowS
67  
68 readFloat      :: (RealFloat a) => ReadS a
69
70 floatToDigits  :: (RealFloat a) => Integer -> a -> ([Int], Int)
71 lexDigits      :: ReadS String
72 \end{pseudocode}
73
74 \begin{code}
75 showInt :: Integral a => a -> ShowS
76 showInt n r
77   | n < 0     = error "Numeric.showInt: can't show negative numbers"
78   | otherwise = go n r
79     where
80      go n r = 
81       case quotRem n 10 of                 { (n', d) ->
82       case chr (ord_0 + fromIntegral d) of { C# c# -> -- stricter than necessary
83       let
84         r' = C# c# : r
85       in
86       if n' == 0 then r' else go n' r'
87       }}
88 \end{code}
89
90 Controlling the format and precision of floats. The code that
91 implements the formatting itself is in @PrelNum@ to avoid
92 mutual module deps.
93
94 \begin{code}
95 showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
96 showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
97 showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
98
99 showEFloat d x =  showString (formatRealFloat FFExponent d x)
100 showFFloat d x =  showString (formatRealFloat FFFixed d x)
101 showGFloat d x =  showString (formatRealFloat FFGeneric d x)
102
103 \end{code}