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