Commit 342852c9 authored by qrczak's avatar qrczak
Browse files

[project @ 2000-12-16 17:46:57 by qrczak]

Specialize show[EFG]Float to Float and Double.
Specialize Ratio instances to Rational.

This speeds up a show[EFG]Float call by about 50000 processor cycles.
parent b756fcaa
% -----------------------------------------------------------------------------
% $Id: Numeric.lhs,v 1.9 2000/06/30 13:39:35 simonmar Exp $
% $Id: Numeric.lhs,v 1.10 2000/12/16 17:46:57 qrczak Exp $
%
% (c) The University of Glasgow, 1997-2000
%
......@@ -78,6 +78,16 @@ implements the formatting itself is in @PrelNum@ to avoid
mutual module deps.
\begin{code}
{-# SPECIALIZE showEFloat ::
Maybe Int -> Float -> ShowS,
Maybe Int -> Double -> ShowS #-}
{-# SPECIALIZE showFFloat ::
Maybe Int -> Float -> ShowS,
Maybe Int -> Double -> ShowS #-}
{-# SPECIALIZE showGFloat ::
Maybe Int -> Float -> ShowS,
Maybe Int -> Double -> ShowS #-}
showEFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
showFFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
showGFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
......
% ------------------------------------------------------------------------------
% $Id: PrelReal.lhs,v 1.6 2000/06/30 13:39:36 simonmar Exp $
% $Id: PrelReal.lhs,v 1.7 2000/12/16 17:46:57 qrczak Exp $
%
% (c) The University of Glasgow, 1994-2000
%
......@@ -212,10 +212,12 @@ instance Integral Integer where
\begin{code}
instance (Integral a) => Ord (Ratio a) where
{-# SPECIALIZE instance Ord Rational #-}
(x:%y) <= (x':%y') = x * y' <= x' * y
(x:%y) < (x':%y') = x * y' < x' * y
instance (Integral a) => Num (Ratio a) where
{-# SPECIALIZE instance Num Rational #-}
(x:%y) + (x':%y') = reduce (x*y' + x'*y) (y*y')
(x:%y) - (x':%y') = reduce (x*y' - x'*y) (y*y')
(x:%y) * (x':%y') = reduce (x * x') (y * y')
......@@ -225,18 +227,22 @@ instance (Integral a) => Num (Ratio a) where
fromInteger x = fromInteger x :% 1
instance (Integral a) => Fractional (Ratio a) where
{-# SPECIALIZE instance Fractional Rational #-}
(x:%y) / (x':%y') = (x*y') % (y*x')
recip (x:%y) = if x < 0 then (-y) :% (-x) else y :% x
fromRational (x:%y) = fromInteger x :% fromInteger y
instance (Integral a) => Real (Ratio a) where
{-# SPECIALIZE instance Real Rational #-}
toRational (x:%y) = toInteger x :% toInteger y
instance (Integral a) => RealFrac (Ratio a) where
{-# SPECIALIZE instance RealFrac Rational #-}
properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
where (q,r) = quotRem x y
instance (Integral a) => Show (Ratio a) where
{-# SPECIALIZE instance Show Rational #-}
showsPrec p (x:%y) = showParen (p > ratio_prec)
(shows x . showString " % " . shows y)
......@@ -244,6 +250,7 @@ ratio_prec :: Int
ratio_prec = 7
instance (Integral a) => Enum (Ratio a) where
{-# SPECIALIZE instance Enum Rational #-}
succ x = x + 1
pred x = x - 1
......@@ -287,7 +294,7 @@ x ^ n | n > 0 = f x (n-1) x
| otherwise = f b (i-1) (b*y)
_ ^ _ = error "Prelude.^: negative exponent"
{- SPECIALISE (^^) ::
{-# SPECIALISE (^^) ::
Rational -> Int -> Rational #-}
(^^) :: (Fractional a, Integral b) => a -> b -> a
x ^^ n = if n >= 0 then x^n else recip (x^(negate n))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment