+% ------------------------------------------------------------------------------
+% $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@}
\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
-- 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
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 #-}
{-# 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
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 #-}
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
(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))
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}
double2Float :: Double -> Float
double2Float (D# x) = F# (double2Float# x)
+
float2Double :: Float -> Double
float2Double (F# x) = D# (float2Double# x)
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}