[project @ 2001-05-07 11:42:31 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelFloat.lhs
index 889c520..67eb2a7 100644 (file)
@@ -1,5 +1,7 @@
+% ------------------------------------------------------------------------------
+% $Id: PrelFloat.lhs,v 1.11 2001/02/28 00:01:03 qrczak 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
@@ -582,7 +581,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 +612,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 +826,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 +874,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}