[project @ 2001-11-08 16:36:39 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelFloat.lhs
index 889c520..be636ba 100644 (file)
@@ -1,5 +1,7 @@
+% ------------------------------------------------------------------------------
+% $Id: PrelFloat.lhs,v 1.13 2001/09/14 15:45:53 simonpj Exp $
 %
-% (c) The AQUA Project, Glasgow University, 1994-1996
+% (c) The University of Glasgow, 1994-2000
 %
 
 \section[PrelNum]{Module @PrelNum@}
@@ -18,11 +20,10 @@ and the classes
 \begin{code}
 {-# OPTIONS -fno-implicit-prelude #-}
 
-#include "../includes/ieee-flpt.h"
+#include "../../includes/ieee-flpt.h"
 
-module PrelFloat where
+module PrelFloat( module PrelFloat, Float#, Double# )  where
 
-import {-# SOURCE #-} PrelErr
 import PrelBase
 import PrelList
 import PrelEnum
@@ -150,9 +151,6 @@ instance  Num Float  where
        -- fromInteger in turn inlines,
        -- so that if fromInteger is applied to an (S# i) the right thing happens
 
-    {-# INLINE fromInt #-}
-    fromInt i          =  int2Float i
-
 instance  Real Float  where
     toRational x       =  (m%1)*(b%1)^^n
                           where (m,n) = decodeFloat x
@@ -163,10 +161,10 @@ instance  Fractional Float  where
     fromRational x     =  fromRat x
     recip x            =  1.0 / x
 
+{-# RULES "truncate/Float->Int" truncate = float2Int #-}
 instance  RealFrac Float  where
 
     {-# SPECIALIZE properFraction :: Float -> (Int, Float) #-}
-    {-# SPECIALIZE truncate :: Float -> Int #-}
     {-# SPECIALIZE round    :: Float -> Int #-}
     {-# SPECIALIZE ceiling  :: Float -> Int #-}
     {-# SPECIALIZE floor    :: Float -> Int #-}
@@ -292,8 +290,9 @@ instance  Num Double  where
 
     {-# INLINE fromInteger #-}
        -- See comments with Num Float
-    fromInteger n      =  encodeFloat n 0
-    fromInt (I# n#)    =  case (int2Double# n#) of { d# -> D# d# }
+    fromInteger (S# i#)    = case (int2Double# i#) of { d# -> D# d# }
+    fromInteger (J# s# d#) = encodeDouble# s# d# 0
+
 
 instance  Real Double  where
     toRational x       =  (m%1)*(b%1)^^n
@@ -326,10 +325,10 @@ instance  Floating Double  where
     acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
     atanh x = log ((x+1.0) / sqrt (1.0-x*x))
 
+{-# RULES "truncate/Double->Int" truncate = double2Int #-}
 instance  RealFrac Double  where
 
     {-# SPECIALIZE properFraction :: Double -> (Int, Double) #-}
-    {-# SPECIALIZE truncate :: Double -> Int #-}
     {-# SPECIALIZE round    :: Double -> Int #-}
     {-# SPECIALIZE ceiling  :: Double -> Int #-}
     {-# SPECIALIZE floor    :: Double -> Int #-}
@@ -425,17 +424,17 @@ for these (@numericEnumFromTo@ and @numericEnumFromThenTo@ below.)
 instance  Enum Float  where
     succ x        = x + 1
     pred x        = x - 1
-    toEnum         =  fromInt
-    fromEnum       =  fromInteger . truncate   -- may overflow
-    enumFrom      =  numericEnumFrom
-    enumFromTo     =  numericEnumFromTo
-    enumFromThen   =  numericEnumFromThen
-    enumFromThenTo =  numericEnumFromThenTo
+    toEnum         = int2Float
+    fromEnum       = fromInteger . truncate   -- may overflow
+    enumFrom      = numericEnumFrom
+    enumFromTo     = numericEnumFromTo
+    enumFromThen   = numericEnumFromThen
+    enumFromThenTo = numericEnumFromThenTo
 
 instance  Enum Double  where
     succ x        = x + 1
     pred x        = x - 1
-    toEnum         =  fromInt
+    toEnum         =  int2Double
     fromEnum       =  fromInteger . truncate   -- may overflow
     enumFrom      =  numericEnumFrom
     enumFromTo     =  numericEnumFromTo
@@ -497,13 +496,15 @@ formatRealFloat fmt decs x
        mk0 ls = case ls of { "" -> "0" ; _ -> ls}
       in
       case decs of
-       Nothing ->
-         let
-         f 0 s    rs  = mk0 (reverse s) ++ '.':mk0 rs
-         f n s    ""  = f (n-1) ('0':s) ""
-         f n s (r:rs) = f (n-1) (r:s) rs
-        in
-        f e "" ds
+       Nothing
+         | e <= 0    -> "0." ++ replicate (-e) '0' ++ ds
+         | otherwise ->
+            let
+               f 0 s    rs  = mk0 (reverse s) ++ '.':mk0 rs
+               f n s    ""  = f (n-1) ('0':s) ""
+               f n s (r:rs) = f (n-1) (r:s) rs
+            in
+               f e "" ds
        Just dec ->
         let dec' = max dec 0 in
        if e >= 0 then
@@ -517,8 +518,8 @@ formatRealFloat fmt decs x
          (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
          d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
         in
-        d : '.' : ds'
-        
+        d : (if null ds' then "" else '.':ds')
+
 
 roundTo :: Int -> Int -> [Int] -> (Int,[Int])
 roundTo base d is =
@@ -537,13 +538,18 @@ roundTo base d is =
        (c,ds) = f (n-1) xs
        i'     = c + i
 
---
 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
 -- by R.G. Burger and R.K. Dybvig in PLDI 96.
 -- This version uses a much slower logarithm estimator. It should be improved.
 
--- This function returns a list of digits (Ints in [0..base-1]) and an
--- exponent.
+-- floatToDigits takes a base and a non-negative RealFloat number,
+-- and returns a list of digits and an exponent. 
+-- In particular, if x>=0, and
+--     floatToDigits base x = ([d1,d2,...,dn], e)
+-- then
+--     (a) n >= 1
+--     (b) x = 0.d1d2...dn * (base**e)
+--     (c) 0 <= di <= base-1
 
 floatToDigits :: (RealFloat a) => Integer -> a -> ([Int], Int)
 floatToDigits _ 0 = ([0], 0)
@@ -582,7 +588,7 @@ floatToDigits base x =
        (p - 1 + e0) * 3 `div` 10
      else
         ceiling ((log (fromInteger (f+1)) +
-                fromInt e * log (fromInteger b)) /
+                fromInteger (int2Integer e) * log (fromInteger b)) /
                   log (fromInteger base))
 --WAS:           fromInt e * log (fromInteger b))
 
@@ -613,7 +619,7 @@ floatToDigits base x =
      let bk = expt base (-k) in
      gen [] (r * bk) s (mUp * bk) (mDn * bk)
  in
- (map toInt (reverse rds), k)
+ (map fromIntegral (reverse rds), k)
 
 \end{code}
 
@@ -827,6 +833,7 @@ int2Double   (I# x) = D# (int2Double#   x)
 
 double2Float :: Double -> Float
 double2Float (D# x) = F# (double2Float# x)
+
 float2Double :: Float -> Double
 float2Double (F# x) = D# (float2Double# x)
 
@@ -874,3 +881,20 @@ foreign import ccall "isDoubleInfinite" unsafe isDoubleInfinite :: Double -> Int
 foreign import ccall "isDoubleDenormalized" unsafe isDoubleDenormalized :: Double -> Int
 foreign import ccall "isDoubleNegativeZero" unsafe isDoubleNegativeZero :: Double -> Int
 \end{code}
+
+%*********************************************************
+%*                                                     *
+\subsection{Coercion rules}
+%*                                                     *
+%*********************************************************
+
+\begin{code}
+{-# RULES
+"fromIntegral/Int->Float"   fromIntegral = int2Float
+"fromIntegral/Int->Double"  fromIntegral = int2Double
+"realToFrac/Float->Float"   realToFrac   = id :: Float -> Float
+"realToFrac/Float->Double"  realToFrac   = float2Double
+"realToFrac/Double->Float"  realToFrac   = double2Float
+"realToFrac/Double->Double" realToFrac   = id :: Double -> Double
+    #-}
+\end{code}