X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Numeric.hs;h=2fb214d949738cbaff87cba2eecbdd6cb88621e9;hb=f7a485978f04e84b086f1974b88887cc72d832d0;hp=9a2b6cca2f9dddf4a3d9cfeec7e368061244ea81;hpb=7de50399a42ee49b0473b7b6eea2b44a2f941a12;p=ghc-base.git diff --git a/Numeric.hs b/Numeric.hs index 9a2b6cc..2fb214d 100644 --- a/Numeric.hs +++ b/Numeric.hs @@ -1,15 +1,14 @@ +{-# OPTIONS -fno-implicit-prelude #-} ----------------------------------------------------------------------------- --- +-- | -- Module : Numeric -- Copyright : (c) The University of Glasgow 2002 --- License : BSD-style (see the file libraries/core/LICENSE) +-- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : provisional -- Portability : portable -- --- $Id: Numeric.hs,v 1.4 2002/02/05 17:32:24 simonmar Exp $ --- -- Odds and ends, mostly functions for reading and showing -- RealFloat-like kind of values. -- @@ -44,20 +43,73 @@ module Numeric ( ) where -import Prelude -- For dependencies import Data.Char #ifdef __GLASGOW_HASKELL__ -import GHC.Base ( Char(..), unsafeChr ) +import GHC.Base import GHC.Read -import GHC.Real ( showSigned ) +import GHC.Real import GHC.Float +import GHC.Num +import GHC.Show +import Data.Maybe +import Text.ParserCombinators.ReadP( ReadP, readP_to_S, pfail ) +import qualified Text.Read.Lex as L #endif #ifdef __HUGS__ import Array #endif + +-- ********************************************************* +-- * * +-- \subsection{Reading} +-- * * +-- ********************************************************* + +readInt :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a +readInt base isDigit valDigit = readP_to_S (L.readIntP base isDigit valDigit) + +readOct, readDec, readHex :: Num a => ReadS a +readOct = readP_to_S L.readOctP +readDec = readP_to_S L.readDecP +readHex = readP_to_S L.readHexP + +readFloat :: RealFrac a => ReadS a +readFloat = readP_to_S readFloatP + +readFloatP :: RealFrac a => ReadP a +readFloatP = + do L.Number x <- L.lex + case L.numberToRational x of + Nothing -> pfail + Just y -> return (fromRational y) + +-- It's turgid to have readSigned work using list comprehensions, +-- but it's specified as a ReadS to ReadS transformer +-- With a bit of luck no one will use it. +readSigned :: (Real a) => ReadS a -> ReadS a +readSigned readPos = readParen False read' + where read' r = read'' r ++ + (do + ("-",s) <- lex r + (x,t) <- read'' s + return (-x,t)) + read'' r = do + (str,s) <- lex r + (n,"") <- readPos str + return (n,s) + + +-- ********************************************************* +-- * * +-- \subsection{Showing} +-- * * +-- ********************************************************* + + + #ifdef __GLASGOW_HASKELL__ showInt :: Integral a => a -> ShowS showInt n cs