Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE CPP , GHCForeignImportPrim , NoImplicitPrelude , MagicHash , UnboxedTuples , UnliftedFFITypes #-}{-# LANGUAGE CApiFFI #-}-- We believe we could deorphan this module, by moving lots of things-- around, but we haven't got there yet:{-# OPTIONS_GHC -Wno-orphans #-}{-# OPTIONS_HADDOCK not-home #-}------------------------------------------------------------------------------- |-- Module : GHC.Float-- Copyright : (c) The University of Glasgow 1994-2002-- Portions obtained from hbc (c) Lennart Augusstson-- License : see libraries/base/LICENSE---- Maintainer : cvs-ghc@haskell.org-- Stability : internal-- Portability : non-portable (GHC Extensions)---- The types 'Float' and 'Double', the classes 'Floating' and 'RealFloat' and-- casting between Word32 and Float and Word64 and Double.-------------------------------------------------------------------------------#include "ieee-flpt.h"#include "MachDeps.h"moduleGHC.Float(moduleGHC.Float,Float(..),Double(..),Float#,Double#,double2Int,int2Double,float2Int,int2Float-- * Monomorphic equality operators-- | See GHC.Classes#matching_overloaded_methods_in_rules,eqFloat,eqDouble)whereimportData.MaybeimportData.BitsimportGHC.BaseimportGHC.ListimportGHC.EnumimportGHC.ShowimportGHC.NumimportGHC.RealimportGHC.WordimportGHC.ArrimportGHC.Float.RealFracMethodsimportGHC.Float.ConversionUtilsimportGHC.Integer.Logarithms(integerLogBase#)importGHC.Integer.Logarithms.Internalsinfixr8**-------------------------------------------------------------------------- Standard numeric classes-------------------------------------------------------------------------- | Trigonometric and hyperbolic functions and related functions.---- The Haskell Report defines no laws for 'Floating'. However, @('+')@, @('*')@-- and 'exp' are customarily expected to define an exponential field and have-- the following properties:---- * @exp (a + b)@ = @exp a * exp b@-- * @exp (fromInteger 0)@ = @fromInteger 1@--class(Fractionala)=>Floatingawherepi::aexp,log,sqrt::a->a(**),logBase::a->a->asin,cos,tan::a->aasin,acos,atan::a->asinh,cosh,tanh::a->aasinh,acosh,atanh::a->a-- | @'log1p' x@ computes @'log' (1 + x)@, but provides more precise-- results for small (absolute) values of @x@ if possible.---- @since 4.9.0.0log1p::a->a-- | @'expm1' x@ computes @'exp' x - 1@, but provides more precise-- results for small (absolute) values of @x@ if possible.---- @since 4.9.0.0expm1::a->a-- | @'log1pexp' x@ computes @'log' (1 + 'exp' x)@, but provides more-- precise results if possible.---- Examples:---- * if @x@ is a large negative number, @'log' (1 + 'exp' x)@ will be-- imprecise for the reasons given in 'log1p'.---- * if @'exp' x@ is close to @-1@, @'log' (1 + 'exp' x)@ will be-- imprecise for the reasons given in 'expm1'.---- @since 4.9.0.0log1pexp::a->a-- | @'log1mexp' x@ computes @'log' (1 - 'exp' x)@, but provides more-- precise results if possible.---- Examples:---- * if @x@ is a large negative number, @'log' (1 - 'exp' x)@ will be-- imprecise for the reasons given in 'log1p'.---- * if @'exp' x@ is close to @1@, @'log' (1 - 'exp' x)@ will be-- imprecise for the reasons given in 'expm1'.---- @since 4.9.0.0log1mexp::a->a{-# INLINE(**)#-}{-# INLINElogBase#-}{-# INLINEsqrt#-}{-# INLINEtan#-}{-# INLINEtanh#-}ax**ay=a -> aforall a. Floating a => a -> aexp(a -> aforall a. Floating a => a -> alogaxa -> a -> aforall a. Num a => a -> a -> a*ay)logBaseaxay=a -> aforall a. Floating a => a -> alogaya -> a -> aforall a. Fractional a => a -> a -> a/a -> aforall a. Floating a => a -> alogaxsqrtax=axa -> a -> aforall a. Floating a => a -> a -> a**a0.5tanax=a -> aforall a. Floating a => a -> asinaxa -> a -> aforall a. Fractional a => a -> a -> a/a -> aforall a. Floating a => a -> acosaxtanhax=a -> aforall a. Floating a => a -> asinhaxa -> a -> aforall a. Fractional a => a -> a -> a/a -> aforall a. Floating a => a -> acoshax{-# INLINElog1p#-}{-# INLINEexpm1#-}{-# INLINElog1pexp#-}{-# INLINElog1mexp#-}log1pax=a -> aforall a. Floating a => a -> alog(a1a -> a -> aforall a. Num a => a -> a -> a+ax)expm1ax=a -> aforall a. Floating a => a -> aexpaxa -> a -> aforall a. Num a => a -> a -> a-a1log1pexpax=a -> aforall a. Floating a => a -> alog1p(a -> aforall a. Floating a => a -> aexpax)log1mexpax=a -> aforall a. Floating a => a -> alog1p(a -> aforall a. Num a => a -> anegate(a -> aforall a. Floating a => a -> aexpax))-- | Default implementation for @'log1mexp'@ requiring @'Ord'@ to test-- against a threshold to decide which implementation variant to use.log1mexpOrd::(Orda,Floatinga)=>a->a{-# INLINElog1mexpOrd#-}log1mexpOrd :: a -> alog1mexpOrdaa|aaa -> a -> Boolforall a. Ord a => a -> a -> Bool>-(a -> aforall a. Floating a => a -> aloga2)=a -> aforall a. Floating a => a -> alog(a -> aforall a. Num a => a -> anegate(a -> aforall a. Floating a => a -> aexpm1aa))|Boolotherwise=a -> aforall a. Floating a => a -> alog1p(a -> aforall a. Num a => a -> anegate(a -> aforall a. Floating a => a -> aexpaa))-- | Efficient, machine-independent access to the components of a-- floating-point number.class(RealFraca,Floatinga)=>RealFloatawhere-- | a constant function, returning the radix of the representation-- (often @2@)floatRadix::a->Integer-- | a constant function, returning the number of digits of-- 'floatRadix' in the significandfloatDigits::a->Int-- | a constant function, returning the lowest and highest values-- the exponent may assumefloatRange::a->(Int,Int)-- | The function 'decodeFloat' applied to a real floating-point-- number returns the significand expressed as an 'Integer' and an-- appropriately scaled exponent (an 'Int'). If @'decodeFloat' x@-- yields @(m,n)@, then @x@ is equal in value to @m*b^^n@, where @b@-- is the floating-point radix, and furthermore, either @m@ and @n@-- are both zero or else @b^(d-1) <= 'abs' m < b^d@, where @d@ is-- the value of @'floatDigits' x@.-- In particular, @'decodeFloat' 0 = (0,0)@. If the type-- contains a negative zero, also @'decodeFloat' (-0.0) = (0,0)@.-- /The result of/ @'decodeFloat' x@ /is unspecified if either of/-- @'isNaN' x@ /or/ @'isInfinite' x@ /is/ 'True'.decodeFloat::a->(Integer,Int)-- | 'encodeFloat' performs the inverse of 'decodeFloat' in the-- sense that for finite @x@ with the exception of @-0.0@,-- @'Prelude.uncurry' 'encodeFloat' ('decodeFloat' x) = x@.-- @'encodeFloat' m n@ is one of the two closest representable-- floating-point numbers to @m*b^^n@ (or @±Infinity@ if overflow-- occurs); usually the closer, but if @m@ contains too many bits,-- the result may be rounded in the wrong direction.encodeFloat::Integer->Int->a-- | 'exponent' corresponds to the second component of 'decodeFloat'.-- @'exponent' 0 = 0@ and for finite nonzero @x@,-- @'exponent' x = snd ('decodeFloat' x) + 'floatDigits' x@.-- If @x@ is a finite floating-point number, it is equal in value to-- @'significand' x * b ^^ 'exponent' x@, where @b@ is the-- floating-point radix.-- The behaviour is unspecified on infinite or @NaN@ values.exponent::a->Int-- | The first component of 'decodeFloat', scaled to lie in the open-- interval (@-1@,@1@), either @0.0@ or of absolute value @>= 1\/b@,-- where @b@ is the floating-point radix.-- The behaviour is unspecified on infinite or @NaN@ values.significand::a->a-- | multiplies a floating-point number by an integer power of the radixscaleFloat::Int->a->a-- | 'True' if the argument is an IEEE \"not-a-number\" (NaN) valueisNaN::a->Bool-- | 'True' if the argument is an IEEE infinity or negative infinityisInfinite::a->Bool-- | 'True' if the argument is too small to be represented in-- normalized formatisDenormalized::a->Bool-- | 'True' if the argument is an IEEE negative zeroisNegativeZero::a->Bool-- | 'True' if the argument is an IEEE floating point numberisIEEE::a->Bool-- | a version of arctangent taking two real floating-point arguments.-- For real floating @x@ and @y@, @'atan2' y x@ computes the angle-- (from the positive x-axis) of the vector from the origin to the-- point @(x,y)@. @'atan2' y x@ returns a value in the range [@-pi@,-- @pi@]. It follows the Common Lisp semantics for the origin when-- signed zeroes are supported. @'atan2' y 1@, with @y@ in a type-- that is 'RealFloat', should return the same value as @'atan' y@.-- A default definition of 'atan2' is provided, but implementors-- can provide a more accurate implementation.atan2::a->a->aexponentax=ifIntegermInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer0thenInt0elseIntnInt -> Int -> Intforall a. Num a => a -> a -> a+a -> Intforall a. RealFloat a => a -> IntfloatDigitsaxwhere(Integerm,Intn)=a -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloataxsignificandax=Integer -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegerm(Int -> Intforall a. Num a => a -> anegate(a -> Intforall a. RealFloat a => a -> IntfloatDigitsax))where(Integerm,Int_)=a -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloataxscaleFloatInt0ax=axscaleFloatIntkax|BoolisFix=ax|Boolotherwise=Integer -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegerm(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int -> Int -> IntclampIntbIntk)where(Integerm,Intn)=a -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloatax(Intl,Inth)=a -> (Int, Int)forall a. RealFloat a => a -> (Int, Int)floatRangeaxd :: Intd=a -> Intforall a. RealFloat a => a -> IntfloatDigitsaxb :: Intb=InthInt -> Int -> Intforall a. Num a => a -> a -> a-IntlInt -> Int -> Intforall a. Num a => a -> a -> a+Int4Int -> Int -> Intforall a. Num a => a -> a -> a*Intd-- n+k may overflow, which would lead-- to wrong results, hence we clamp the-- scaling parameter.-- If n + k would be larger than h,-- n + clamp b k must be too, simliar-- for smaller than l - d.-- Add a little extra to keep clear-- from the boundary cases.isFix :: BoolisFix=axa -> a -> Boolforall a. Eq a => a -> a -> Bool==a0Bool -> Bool -> Bool||a -> Boolforall a. RealFloat a => a -> BoolisNaNaxBool -> Bool -> Bool||a -> Boolforall a. RealFloat a => a -> BoolisInfiniteaxatan2ayax|axa -> a -> Boolforall a. Ord a => a -> a -> Bool>a0=a -> aforall a. Floating a => a -> aatan(aya -> a -> aforall a. Fractional a => a -> a -> a/ax)|axa -> a -> Boolforall a. Eq a => a -> a -> Bool==a0Bool -> Bool -> Bool&&aya -> a -> Boolforall a. Ord a => a -> a -> Bool>a0=aforall a. Floating a => apia -> a -> aforall a. Fractional a => a -> a -> a/a2|axa -> a -> Boolforall a. Ord a => a -> a -> Bool<a0Bool -> Bool -> Bool&&aya -> a -> Boolforall a. Ord a => a -> a -> Bool>a0=aforall a. Floating a => apia -> a -> aforall a. Num a => a -> a -> a+a -> aforall a. Floating a => a -> aatan(aya -> a -> aforall a. Fractional a => a -> a -> a/ax)|(axa -> a -> Boolforall a. Ord a => a -> a -> Bool<=a0Bool -> Bool -> Bool&&aya -> a -> Boolforall a. Ord a => a -> a -> Bool<a0)Bool -> Bool -> Bool||(axa -> a -> Boolforall a. Ord a => a -> a -> Bool<a0Bool -> Bool -> Bool&&a -> Boolforall a. RealFloat a => a -> BoolisNegativeZeroay)Bool -> Bool -> Bool||(a -> Boolforall a. RealFloat a => a -> BoolisNegativeZeroaxBool -> Bool -> Bool&&a -> Boolforall a. RealFloat a => a -> BoolisNegativeZeroay)=-a -> a -> aforall a. RealFloat a => a -> a -> aatan2(-ay)ax|aya -> a -> Boolforall a. Eq a => a -> a -> Bool==a0Bool -> Bool -> Bool&&(axa -> a -> Boolforall a. Ord a => a -> a -> Bool<a0Bool -> Bool -> Bool||a -> Boolforall a. RealFloat a => a -> BoolisNegativeZeroax)=aforall a. Floating a => api-- must be after the previous test on zero y|axa -> a -> Boolforall a. Eq a => a -> a -> Bool==a0Bool -> Bool -> Bool&&aya -> a -> Boolforall a. Eq a => a -> a -> Bool==a0=ay-- must be after the other double zero tests|Boolotherwise=axa -> a -> aforall a. Num a => a -> a -> a+ay-- x or y is a NaN, return a NaN (via +)-------------------------------------------------------------------------- Float-------------------------------------------------------------------------- | @since 2.01-- Note that due to the presence of @NaN@, not all elements of 'Float' have an-- additive inverse.---- >>> 0/0 + (negate 0/0 :: Float)-- NaN---- Also note that due to the presence of -0, `Float`'s 'Num' instance doesn't-- have an additive identity---- >>> 0 + (-0 :: Float)-- 0.0instanceNumFloatwhere+ :: Float -> Float -> Float(+)FloatxFloaty=Float -> Float -> FloatplusFloatFloatxFloaty(-)FloatxFloaty=Float -> Float -> FloatminusFloatFloatxFloatynegate :: Float -> FloatnegateFloatx=Float -> FloatnegateFloatFloatx* :: Float -> Float -> Float(*)FloatxFloaty=Float -> Float -> FloattimesFloatFloatxFloatyabs :: Float -> FloatabsFloatx=Float -> FloatfabsFloatFloatxsignum :: Float -> FloatsignumFloatx|FloatxFloat -> Float -> Boolforall a. Ord a => a -> a -> Bool>Float0=Float1|FloatxFloat -> Float -> Boolforall a. Ord a => a -> a -> Bool<Float0=Float -> FloatnegateFloatFloat1|Boolotherwise=Floatx-- handles 0.0, (-0.0), and NaN{-# INLINEfromInteger#-}fromInteger :: Integer -> FloatfromIntegerIntegeri=Float# -> FloatF#(Integer -> Float#floatFromIntegerIntegeri)-- | @since 2.01instanceRealFloatwheretoRational :: Float -> RationaltoRational(F#Float#x#)=caseFloat# -> (# Int#, Int# #)decodeFloat_Int#Float#x#of(#Int#m#,Int#e##)|Int# -> BoolisTrue#(Int#e#Int# -> Int# -> Int#>=#Int#0#)->(Int# -> IntegersmallIntegerInt#m#Integer -> Int# -> Integer`shiftLInteger`Int#e#)Integer -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integer1|Int# -> BoolisTrue#((Int# -> Word#int2Word#Int#m#Word# -> Word# -> Word#`and#`Word#1##)Word# -> Word# -> Int#`eqWord#`Word#0##)->caseInt# -> Int# -> (# Integer, Int# #)elimZerosInt#Int#m#(Int# -> Int#negateInt#Int#e#)of(#Integern,Int#d##)->IntegernInteger -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integer -> Int# -> IntegershiftLIntegerInteger1Int#d#|Boolotherwise->Int# -> IntegersmallIntegerInt#m#Integer -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integer -> Int# -> IntegershiftLIntegerInteger1(Int# -> Int#negateInt#Int#e#)-- | @since 2.01-- Note that due to the presence of @NaN@, not all elements of 'Float' have an-- multiplicative inverse.---- >>> 0/0 * (recip 0/0 :: Float)-- NaNinstanceFractionalFloatwhere/ :: Float -> Float -> Float(/)FloatxFloaty=Float -> Float -> FloatdivideFloatFloatxFloaty{-# INLINEfromRational#-}fromRational :: Rational -> FloatfromRational(Integern:%Integerd)=Integer -> Integer -> FloatrationalToFloatIntegernIntegerdrecip :: Float -> FloatrecipFloatx=Float1.0Float -> Float -> Floatforall a. Fractional a => a -> a -> a/FloatxrationalToFloat::Integer->Integer->Float{-# NOINLINE[1]rationalToFloat#-}rationalToFloat :: Integer -> Integer -> FloatrationalToFloatIntegernInteger0|IntegernInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer0=Float0Float -> Float -> Floatforall a. Fractional a => a -> a -> a/Float0|IntegernInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool<Integer0=(-Float1)Float -> Float -> Floatforall a. Fractional a => a -> a -> a/Float0|Boolotherwise=Float1Float -> Float -> Floatforall a. Fractional a => a -> a -> a/Float0rationalToFloatIntegernIntegerd|IntegernInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer0=Integer -> Int -> Floatforall a. RealFloat a => Integer -> Int -> aencodeFloatInteger0Int0|IntegernInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool<Integer0=-(Int -> Int -> Integer -> Integer -> Floatforall a. RealFloat a => Int -> Int -> Integer -> Integer -> afromRat''IntminExIntmantDigs(-Integern)Integerd)|Boolotherwise=Int -> Int -> Integer -> Integer -> Floatforall a. RealFloat a => Int -> Int -> Integer -> Integer -> afromRat''IntminExIntmantDigsIntegernIntegerdwhereminEx :: IntminEx=FLT_MIN_EXPmantDigs :: IntmantDigs=FLT_MANT_DIG-- RULES for Integer and Int{-# RULES"properFraction/Float->Integer"properFraction=properFractionFloatInteger"truncate/Float->Integer"truncate=truncateFloatInteger"floor/Float->Integer"floor=floorFloatInteger"ceiling/Float->Integer"ceiling=ceilingFloatInteger"round/Float->Integer"round=roundFloatInteger"properFraction/Float->Int"properFraction=properFractionFloatInt"truncate/Float->Int"truncate=float2Int"floor/Float->Int"floor=floorFloatInt"ceiling/Float->Int"ceiling=ceilingFloatInt"round/Float->Int"round=roundFloatInt#-}-- | @since 2.01instanceRealFracFloatwhere-- ceiling, floor, and truncate are all small{-# INLINE[1]ceiling#-}{-# INLINE[1]floor#-}{-# INLINE[1]truncate#-}-- We assume that FLT_RADIX is 2 so that we can use more efficient code#if FLT_RADIX != 2#error FLT_RADIX must be 2#endifproperFraction :: Float -> (b, Float)properFraction(F#Float#x#)=caseFloat# -> (# Int#, Int# #)decodeFloat_Int#Float#x#of(#Int#m#,Int#n##)->letm :: Intm=Int# -> IntI#Int#m#n :: Intn=Int# -> IntI#Int#n#inifIntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0then(Int -> bforall a b. (Integral a, Num b) => a -> bfromIntegralIntmb -> b -> bforall a. Num a => a -> a -> a*(b2b -> Int -> bforall a b. (Num a, Integral b) => a -> b -> a^Intn),Float0.0)elseleti :: Inti=ifIntmInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0thenIntmInt -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int -> Intforall a. Num a => a -> anegateIntnelseInt -> Intforall a. Num a => a -> anegate(Int -> Intforall a. Num a => a -> anegateIntmInt -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int -> Intforall a. Num a => a -> anegateIntn)f :: Intf=IntmInt -> Int -> Intforall a. Num a => a -> a -> a-(IntiInt -> Int -> Intforall a. Bits a => a -> Int -> a`shiftL`Int -> Intforall a. Num a => a -> anegateIntn)in(Int -> bforall a b. (Integral a, Num b) => a -> bfromIntegralInti,Integer -> Int -> Floatforall a. RealFloat a => Integer -> Int -> aencodeFloat(Int -> Integerforall a b. (Integral a, Num b) => a -> bfromIntegralIntf)Intn)truncate :: Float -> btruncateFloatx=caseFloat -> (b, Float)forall a b. (RealFrac a, Integral b) => a -> (b, a)properFractionFloatxof(bn,Float_)->bnround :: Float -> broundFloatx=caseFloat -> (b, Float)forall a b. (RealFrac a, Integral b) => a -> (b, a)properFractionFloatxof(bn,Floatr)->letm :: bm=ifFloatrFloat -> Float -> Boolforall a. Ord a => a -> a -> Bool<Float0.0thenbnb -> b -> bforall a. Num a => a -> a -> a-b1elsebnb -> b -> bforall a. Num a => a -> a -> a+b1half_down :: Floathalf_down=Float -> Floatforall a. Num a => a -> aabsFloatrFloat -> Float -> Floatforall a. Num a => a -> a -> a-Float0.5incase(Float -> Float -> Orderingforall a. Ord a => a -> a -> OrderingcompareFloathalf_downFloat0.0)ofOrderingLT->bnOrderingEQ->ifb -> Boolforall a. Integral a => a -> BoolevenbnthenbnelsebmOrderingGT->bmceiling :: Float -> bceilingFloatx=caseFloat -> (b, Float)forall a b. (RealFrac a, Integral b) => a -> (b, a)properFractionFloatxof(bn,Floatr)->ifFloatrFloat -> Float -> Boolforall a. Ord a => a -> a -> Bool>Float0.0thenbnb -> b -> bforall a. Num a => a -> a -> a+b1elsebnfloor :: Float -> bfloorFloatx=caseFloat -> (b, Float)forall a b. (RealFrac a, Integral b) => a -> (b, a)properFractionFloatxof(bn,Floatr)->ifFloatrFloat -> Float -> Boolforall a. Ord a => a -> a -> Bool<Float0.0thenbnb -> b -> bforall a. Num a => a -> a -> a-b1elsebn-- | @since 2.01instanceFloatingFloatwherepi :: Floatpi=Float3.141592653589793238exp :: Float -> FloatexpFloatx=Float -> FloatexpFloatFloatxlog :: Float -> FloatlogFloatx=Float -> FloatlogFloatFloatxsqrt :: Float -> FloatsqrtFloatx=Float -> FloatsqrtFloatFloatxsin :: Float -> FloatsinFloatx=Float -> FloatsinFloatFloatxcos :: Float -> FloatcosFloatx=Float -> FloatcosFloatFloatxtan :: Float -> FloattanFloatx=Float -> FloattanFloatFloatxasin :: Float -> FloatasinFloatx=Float -> FloatasinFloatFloatxacos :: Float -> FloatacosFloatx=Float -> FloatacosFloatFloatxatan :: Float -> FloatatanFloatx=Float -> FloatatanFloatFloatxsinh :: Float -> FloatsinhFloatx=Float -> FloatsinhFloatFloatxcosh :: Float -> FloatcoshFloatx=Float -> FloatcoshFloatFloatxtanh :: Float -> FloattanhFloatx=Float -> FloattanhFloatFloatx** :: Float -> Float -> Float(**)FloatxFloaty=Float -> Float -> FloatpowerFloatFloatxFloatylogBase :: Float -> Float -> FloatlogBaseFloatxFloaty=Float -> Floatforall a. Floating a => a -> alogFloatyFloat -> Float -> Floatforall a. Fractional a => a -> a -> a/Float -> Floatforall a. Floating a => a -> alogFloatxasinh :: Float -> FloatasinhFloatx=Float -> FloatasinhFloatFloatxacosh :: Float -> FloatacoshFloatx=Float -> FloatacoshFloatFloatxatanh :: Float -> FloatatanhFloatx=Float -> FloatatanhFloatFloatxlog1p :: Float -> Floatlog1p=Float -> Floatlog1pFloatexpm1 :: Float -> Floatexpm1=Float -> Floatexpm1Floatlog1mexp :: Float -> Floatlog1mexpFloatx=Float -> Floatforall a. (Ord a, Floating a) => a -> alog1mexpOrdFloatx{-# INLINElog1mexp#-}log1pexp :: Float -> Floatlog1pexpFloata|FloataFloat -> Float -> Boolforall a. Ord a => a -> a -> Bool<=Float18=Float -> Floatlog1pFloat(Float -> Floatforall a. Floating a => a -> aexpFloata)|FloataFloat -> Float -> Boolforall a. Ord a => a -> a -> Bool<=Float100=FloataFloat -> Float -> Floatforall a. Num a => a -> a -> a+Float -> Floatforall a. Floating a => a -> aexp(Float -> Floatforall a. Num a => a -> anegateFloata)|Boolotherwise=Floata{-# INLINElog1pexp#-}-- | @since 2.01instanceRealFloatFloatwherefloatRadix :: Float -> IntegerfloatRadixFloat_=FLT_RADIX-- from float.hfloatDigits :: Float -> IntfloatDigitsFloat_=FLT_MANT_DIG-- dittofloatRange :: Float -> (Int, Int)floatRangeFloat_=(FLT_MIN_EXP,FLT_MAX_EXP)-- dittodecodeFloat :: Float -> (Integer, Int)decodeFloat(F#Float#f#)=caseFloat# -> (# Int#, Int# #)decodeFloat_Int#Float#f#of(#Int#i,Int#e#)->(Int# -> IntegersmallIntegerInt#i,Int# -> IntI#Int#e)encodeFloat :: Integer -> Int -> FloatencodeFloatIntegeri(I#Int#e)=Float# -> FloatF#(Integer -> Int# -> Float#encodeFloatIntegerIntegeriInt#e)exponent :: Float -> IntexponentFloatx=caseFloat -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloatFloatxof(Integerm,Intn)->ifIntegermInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer0thenInt0elseIntnInt -> Int -> Intforall a. Num a => a -> a -> a+Float -> Intforall a. RealFloat a => a -> IntfloatDigitsFloatxsignificand :: Float -> FloatsignificandFloatx=caseFloat -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloatFloatxof(Integerm,Int_)->Integer -> Int -> Floatforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegerm(Int -> Intforall a. Num a => a -> anegate(Float -> Intforall a. RealFloat a => a -> IntfloatDigitsFloatx))scaleFloat :: Int -> Float -> FloatscaleFloatInt0Floatx=FloatxscaleFloatIntkFloatx|BoolisFix=Floatx|Boolotherwise=caseFloat -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloatFloatxof(Integerm,Intn)->Integer -> Int -> Floatforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegerm(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int -> Int -> IntclampIntbfIntk)wherebf :: Intbf=FLT_MAX_EXP-(FLT_MIN_EXP)+4*FLT_MANT_DIGisFix :: BoolisFix=FloatxFloat -> Float -> Boolforall a. Eq a => a -> a -> Bool==Float0Bool -> Bool -> Bool||Float -> IntisFloatFiniteFloatxInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0isNaN :: Float -> BoolisNaNFloatx=Int0Int -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Float -> IntisFloatNaNFloatxisInfinite :: Float -> BoolisInfiniteFloatx=Int0Int -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Float -> IntisFloatInfiniteFloatxisDenormalized :: Float -> BoolisDenormalizedFloatx=Int0Int -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Float -> IntisFloatDenormalizedFloatxisNegativeZero :: Float -> BoolisNegativeZeroFloatx=Int0Int -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Float -> IntisFloatNegativeZeroFloatxisIEEE :: Float -> BoolisIEEEFloat_=BoolTrue-- | @since 2.01instanceShowFloatwhereshowsPrec :: Int -> Float -> ShowSshowsPrecIntx=(Float -> ShowS) -> Int -> Float -> ShowSforall a. RealFloat a => (a -> ShowS) -> Int -> a -> ShowSshowSignedFloatFloat -> ShowSforall a. RealFloat a => a -> ShowSshowFloatIntxshowList :: [Float] -> ShowSshowList=(Float -> ShowS) -> [Float] -> ShowSforall a. (a -> ShowS) -> [a] -> ShowSshowList__(Int -> Float -> ShowSforall a. Show a => Int -> a -> ShowSshowsPrecInt0)-------------------------------------------------------------------------- Double-------------------------------------------------------------------------- | @since 2.01-- Note that due to the presence of @NaN@, not all elements of 'Double' have an-- additive inverse.---- >>> 0/0 + (negate 0/0 :: Double)-- NaN---- Also note that due to the presence of -0, `Double`'s 'Num' instance doesn't-- have an additive identity---- >>> 0 + (-0 :: Double)-- 0.0instanceNumDoublewhere+ :: Double -> Double -> Double(+)DoublexDoubley=Double -> Double -> DoubleplusDoubleDoublexDoubley(-)DoublexDoubley=Double -> Double -> DoubleminusDoubleDoublexDoubleynegate :: Double -> DoublenegateDoublex=Double -> DoublenegateDoubleDoublex* :: Double -> Double -> Double(*)DoublexDoubley=Double -> Double -> DoubletimesDoubleDoublexDoubleyabs :: Double -> DoubleabsDoublex=Double -> DoublefabsDoubleDoublexsignum :: Double -> DoublesignumDoublex|DoublexDouble -> Double -> Boolforall a. Ord a => a -> a -> Bool>Double0=Double1|DoublexDouble -> Double -> Boolforall a. Ord a => a -> a -> Bool<Double0=Double -> DoublenegateDoubleDouble1|Boolotherwise=Doublex-- handles 0.0, (-0.0), and NaN{-# INLINEfromInteger#-}fromInteger :: Integer -> DoublefromIntegerIntegeri=Double# -> DoubleD#(Integer -> Double#doubleFromIntegerIntegeri)-- | @since 2.01instanceRealDoublewheretoRational :: Double -> RationaltoRational(D#Double#x#)=caseDouble# -> (# Integer, Int# #)decodeDoubleIntegerDouble#x#of(#Integerm,Int#e##)|Int# -> BoolisTrue#(Int#e#Int# -> Int# -> Int#>=#Int#0#)->Integer -> Int# -> IntegershiftLIntegerIntegermInt#e#Integer -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integer1|Int# -> BoolisTrue#((Integer -> Word#integerToWordIntegermWord# -> Word# -> Word#`and#`Word#1##)Word# -> Word# -> Int#`eqWord#`Word#0##)->caseInteger -> Int# -> (# Integer, Int# #)elimZerosIntegerIntegerm(Int# -> Int#negateInt#Int#e#)of(#Integern,Int#d##)->IntegernInteger -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integer -> Int# -> IntegershiftLIntegerInteger1Int#d#|Boolotherwise->IntegermInteger -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integer -> Int# -> IntegershiftLIntegerInteger1(Int# -> Int#negateInt#Int#e#)-- | @since 2.01-- Note that due to the presence of @NaN@, not all elements of 'Double' have an-- multiplicative inverse.---- >>> 0/0 * (recip 0/0 :: Double)-- NaNinstanceFractionalDoublewhere/ :: Double -> Double -> Double(/)DoublexDoubley=Double -> Double -> DoubledivideDoubleDoublexDoubley{-# INLINEfromRational#-}fromRational :: Rational -> DoublefromRational(Integern:%Integerd)=Integer -> Integer -> DoublerationalToDoubleIntegernIntegerdrecip :: Double -> DoublerecipDoublex=Double1.0Double -> Double -> Doubleforall a. Fractional a => a -> a -> a/DoublexrationalToDouble::Integer->Integer->Double{-# NOINLINE[1]rationalToDouble#-}rationalToDouble :: Integer -> Integer -> DoublerationalToDoubleIntegernInteger0|IntegernInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer0=Double0Double -> Double -> Doubleforall a. Fractional a => a -> a -> a/Double0|IntegernInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool<Integer0=(-Double1)Double -> Double -> Doubleforall a. Fractional a => a -> a -> a/Double0|Boolotherwise=Double1Double -> Double -> Doubleforall a. Fractional a => a -> a -> a/Double0rationalToDoubleIntegernIntegerd|IntegernInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer0=Integer -> Int -> Doubleforall a. RealFloat a => Integer -> Int -> aencodeFloatInteger0Int0|IntegernInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool<Integer0=-(Int -> Int -> Integer -> Integer -> Doubleforall a. RealFloat a => Int -> Int -> Integer -> Integer -> afromRat''IntminExIntmantDigs(-Integern)Integerd)|Boolotherwise=Int -> Int -> Integer -> Integer -> Doubleforall a. RealFloat a => Int -> Int -> Integer -> Integer -> afromRat''IntminExIntmantDigsIntegernIntegerdwhereminEx :: IntminEx=DBL_MIN_EXPmantDigs :: IntmantDigs=DBL_MANT_DIG-- | @since 2.01instanceFloatingDoublewherepi :: Doublepi=Double3.141592653589793238exp :: Double -> DoubleexpDoublex=Double -> DoubleexpDoubleDoublexlog :: Double -> DoublelogDoublex=Double -> DoublelogDoubleDoublexsqrt :: Double -> DoublesqrtDoublex=Double -> DoublesqrtDoubleDoublexsin :: Double -> DoublesinDoublex=Double -> DoublesinDoubleDoublexcos :: Double -> DoublecosDoublex=Double -> DoublecosDoubleDoublextan :: Double -> DoubletanDoublex=Double -> DoubletanDoubleDoublexasin :: Double -> DoubleasinDoublex=Double -> DoubleasinDoubleDoublexacos :: Double -> DoubleacosDoublex=Double -> DoubleacosDoubleDoublexatan :: Double -> DoubleatanDoublex=Double -> DoubleatanDoubleDoublexsinh :: Double -> DoublesinhDoublex=Double -> DoublesinhDoubleDoublexcosh :: Double -> DoublecoshDoublex=Double -> DoublecoshDoubleDoublextanh :: Double -> DoubletanhDoublex=Double -> DoubletanhDoubleDoublex** :: Double -> Double -> Double(**)DoublexDoubley=Double -> Double -> DoublepowerDoubleDoublexDoubleylogBase :: Double -> Double -> DoublelogBaseDoublexDoubley=Double -> Doubleforall a. Floating a => a -> alogDoubleyDouble -> Double -> Doubleforall a. Fractional a => a -> a -> a/Double -> Doubleforall a. Floating a => a -> alogDoublexasinh :: Double -> DoubleasinhDoublex=Double -> DoubleasinhDoubleDoublexacosh :: Double -> DoubleacoshDoublex=Double -> DoubleacoshDoubleDoublexatanh :: Double -> DoubleatanhDoublex=Double -> DoubleatanhDoubleDoublexlog1p :: Double -> Doublelog1p=Double -> Doublelog1pDoubleexpm1 :: Double -> Doubleexpm1=Double -> Doubleexpm1Doublelog1mexp :: Double -> Doublelog1mexpDoublex=Double -> Doubleforall a. (Ord a, Floating a) => a -> alog1mexpOrdDoublex{-# INLINElog1mexp#-}log1pexp :: Double -> Doublelog1pexpDoublea|DoubleaDouble -> Double -> Boolforall a. Ord a => a -> a -> Bool<=Double18=Double -> Doublelog1pDouble(Double -> Doubleforall a. Floating a => a -> aexpDoublea)|DoubleaDouble -> Double -> Boolforall a. Ord a => a -> a -> Bool<=Double100=DoubleaDouble -> Double -> Doubleforall a. Num a => a -> a -> a+Double -> Doubleforall a. Floating a => a -> aexp(Double -> Doubleforall a. Num a => a -> anegateDoublea)|Boolotherwise=Doublea{-# INLINElog1pexp#-}-- RULES for Integer and Int{-# RULES"properFraction/Double->Integer"properFraction=properFractionDoubleInteger"truncate/Double->Integer"truncate=truncateDoubleInteger"floor/Double->Integer"floor=floorDoubleInteger"ceiling/Double->Integer"ceiling=ceilingDoubleInteger"round/Double->Integer"round=roundDoubleInteger"properFraction/Double->Int"properFraction=properFractionDoubleInt"truncate/Double->Int"truncate=double2Int"floor/Double->Int"floor=floorDoubleInt"ceiling/Double->Int"ceiling=ceilingDoubleInt"round/Double->Int"round=roundDoubleInt#-}-- | @since 2.01instanceRealFracDoublewhere-- ceiling, floor, and truncate are all small{-# INLINE[1]ceiling#-}{-# INLINE[1]floor#-}{-# INLINE[1]truncate#-}properFraction :: Double -> (b, Double)properFractionDoublex=case(Double -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloatDoublex)of{(Integerm,Intn)->ifIntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0then(Integer -> bforall a. Num a => Integer -> afromIntegerIntegermb -> b -> bforall a. Num a => a -> a -> a*b2b -> Int -> bforall a b. (Num a, Integral b) => a -> b -> a^Intn,Double0.0)elsecase(Integer -> Integer -> (Integer, Integer)forall a. Integral a => a -> a -> (a, a)quotRemIntegerm(Integer2Integer -> Int -> Integerforall a b. (Num a, Integral b) => a -> b -> a^(Int -> Intforall a. Num a => a -> anegateIntn)))of{(Integerw,Integerr)->(Integer -> bforall a. Num a => Integer -> afromIntegerIntegerw,Integer -> Int -> Doubleforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegerrIntn)}}truncate :: Double -> btruncateDoublex=caseDouble -> (b, Double)forall a b. (RealFrac a, Integral b) => a -> (b, a)properFractionDoublexof(bn,Double_)->bnround :: Double -> broundDoublex=caseDouble -> (b, Double)forall a b. (RealFrac a, Integral b) => a -> (b, a)properFractionDoublexof(bn,Doubler)->letm :: bm=ifDoublerDouble -> Double -> Boolforall a. Ord a => a -> a -> Bool<Double0.0thenbnb -> b -> bforall a. Num a => a -> a -> a-b1elsebnb -> b -> bforall a. Num a => a -> a -> a+b1half_down :: Doublehalf_down=Double -> Doubleforall a. Num a => a -> aabsDoublerDouble -> Double -> Doubleforall a. Num a => a -> a -> a-Double0.5incase(Double -> Double -> Orderingforall a. Ord a => a -> a -> OrderingcompareDoublehalf_downDouble0.0)ofOrderingLT->bnOrderingEQ->ifb -> Boolforall a. Integral a => a -> BoolevenbnthenbnelsebmOrderingGT->bmceiling :: Double -> bceilingDoublex=caseDouble -> (b, Double)forall a b. (RealFrac a, Integral b) => a -> (b, a)properFractionDoublexof(bn,Doubler)->ifDoublerDouble -> Double -> Boolforall a. Ord a => a -> a -> Bool>Double0.0thenbnb -> b -> bforall a. Num a => a -> a -> a+b1elsebnfloor :: Double -> bfloorDoublex=caseDouble -> (b, Double)forall a b. (RealFrac a, Integral b) => a -> (b, a)properFractionDoublexof(bn,Doubler)->ifDoublerDouble -> Double -> Boolforall a. Ord a => a -> a -> Bool<Double0.0thenbnb -> b -> bforall a. Num a => a -> a -> a-b1elsebn-- | @since 2.01instanceRealFloatDoublewherefloatRadix :: Double -> IntegerfloatRadixDouble_=FLT_RADIX-- from float.hfloatDigits :: Double -> IntfloatDigitsDouble_=DBL_MANT_DIG-- dittofloatRange :: Double -> (Int, Int)floatRangeDouble_=(DBL_MIN_EXP,DBL_MAX_EXP)-- dittodecodeFloat :: Double -> (Integer, Int)decodeFloat(D#Double#x#)=caseDouble# -> (# Integer, Int# #)decodeDoubleIntegerDouble#x#of(#Integeri,Int#j#)->(Integeri,Int# -> IntI#Int#j)encodeFloat :: Integer -> Int -> DoubleencodeFloatIntegeri(I#Int#j)=Double# -> DoubleD#(Integer -> Int# -> Double#encodeDoubleIntegerIntegeriInt#j)exponent :: Double -> IntexponentDoublex=caseDouble -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloatDoublexof(Integerm,Intn)->ifIntegermInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer0thenInt0elseIntnInt -> Int -> Intforall a. Num a => a -> a -> a+Double -> Intforall a. RealFloat a => a -> IntfloatDigitsDoublexsignificand :: Double -> DoublesignificandDoublex=caseDouble -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloatDoublexof(Integerm,Int_)->Integer -> Int -> Doubleforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegerm(Int -> Intforall a. Num a => a -> anegate(Double -> Intforall a. RealFloat a => a -> IntfloatDigitsDoublex))scaleFloat :: Int -> Double -> DoublescaleFloatInt0Doublex=DoublexscaleFloatIntkDoublex|BoolisFix=Doublex|Boolotherwise=caseDouble -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloatDoublexof(Integerm,Intn)->Integer -> Int -> Doubleforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegerm(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int -> Int -> IntclampIntbdIntk)wherebd :: Intbd=DBL_MAX_EXP-(DBL_MIN_EXP)+4*DBL_MANT_DIGisFix :: BoolisFix=DoublexDouble -> Double -> Boolforall a. Eq a => a -> a -> Bool==Double0Bool -> Bool -> Bool||Double -> IntisDoubleFiniteDoublexInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0isNaN :: Double -> BoolisNaNDoublex=Int0Int -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Double -> IntisDoubleNaNDoublexisInfinite :: Double -> BoolisInfiniteDoublex=Int0Int -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Double -> IntisDoubleInfiniteDoublexisDenormalized :: Double -> BoolisDenormalizedDoublex=Int0Int -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Double -> IntisDoubleDenormalizedDoublexisNegativeZero :: Double -> BoolisNegativeZeroDoublex=Int0Int -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Double -> IntisDoubleNegativeZeroDoublexisIEEE :: Double -> BoolisIEEEDouble_=BoolTrue-- | @since 2.01instanceShowDoublewhereshowsPrec :: Int -> Double -> ShowSshowsPrecIntx=(Double -> ShowS) -> Int -> Double -> ShowSforall a. RealFloat a => (a -> ShowS) -> Int -> a -> ShowSshowSignedFloatDouble -> ShowSforall a. RealFloat a => a -> ShowSshowFloatIntxshowList :: [Double] -> ShowSshowList=(Double -> ShowS) -> [Double] -> ShowSforall a. (a -> ShowS) -> [a] -> ShowSshowList__(Int -> Double -> ShowSforall a. Show a => Int -> a -> ShowSshowsPrecInt0)-------------------------------------------------------------------------- Enum instances------------------------------------------------------------------------{-The @Enum@ instances for Floats and Doubles are slightly unusual.The @toEnum@ function truncates numbers to Int. The definitionsof @enumFrom@ and @enumFromThen@ allow floats to be used in arithmeticseries: [0,0.1 .. 1.0]. However, roundoff errors make these somewhatdubious. This example may have either 10 or 11 elements, depending onhow 0.1 is represented.NOTE: The instances for Float and Double do not make use of the defaultmethods for @enumFromTo@ and @enumFromThenTo@, as these rely on there beinga `non-lossy' conversion to and from Ints. Instead we make use of the1.2 default methods (back in the days when Enum had Ord as a superclass)for these (@numericEnumFromTo@ and @numericEnumFromThenTo@ below.)-}-- | @since 2.01instanceEnumFloatwheresucc :: Float -> FloatsuccFloatx=FloatxFloat -> Float -> Floatforall a. Num a => a -> a -> a+Float1pred :: Float -> FloatpredFloatx=FloatxFloat -> Float -> Floatforall a. Num a => a -> a -> a-Float1toEnum :: Int -> FloattoEnum=Int -> Floatint2FloatfromEnum :: Float -> IntfromEnum=Integer -> Intforall a. Num a => Integer -> afromInteger(Integer -> Int) -> (Float -> Integer) -> Float -> Intforall b c a. (b -> c) -> (a -> b) -> a -> c.Float -> Integerforall a b. (RealFrac a, Integral b) => a -> btruncate-- may overflowenumFrom :: Float -> [Float]enumFrom=Float -> [Float]forall a. Fractional a => a -> [a]numericEnumFromenumFromTo :: Float -> Float -> [Float]enumFromTo=Float -> Float -> [Float]forall a. (Ord a, Fractional a) => a -> a -> [a]numericEnumFromToenumFromThen :: Float -> Float -> [Float]enumFromThen=Float -> Float -> [Float]forall a. Fractional a => a -> a -> [a]numericEnumFromThenenumFromThenTo :: Float -> Float -> Float -> [Float]enumFromThenTo=Float -> Float -> Float -> [Float]forall a. (Ord a, Fractional a) => a -> a -> a -> [a]numericEnumFromThenTo-- | @since 2.01instanceEnumDoublewheresucc :: Double -> DoublesuccDoublex=DoublexDouble -> Double -> Doubleforall a. Num a => a -> a -> a+Double1pred :: Double -> DoublepredDoublex=DoublexDouble -> Double -> Doubleforall a. Num a => a -> a -> a-Double1toEnum :: Int -> DoubletoEnum=Int -> Doubleint2DoublefromEnum :: Double -> IntfromEnum=Integer -> Intforall a. Num a => Integer -> afromInteger(Integer -> Int) -> (Double -> Integer) -> Double -> Intforall b c a. (b -> c) -> (a -> b) -> a -> c.Double -> Integerforall a b. (RealFrac a, Integral b) => a -> btruncate-- may overflowenumFrom :: Double -> [Double]enumFrom=Double -> [Double]forall a. Fractional a => a -> [a]numericEnumFromenumFromTo :: Double -> Double -> [Double]enumFromTo=Double -> Double -> [Double]forall a. (Ord a, Fractional a) => a -> a -> [a]numericEnumFromToenumFromThen :: Double -> Double -> [Double]enumFromThen=Double -> Double -> [Double]forall a. Fractional a => a -> a -> [a]numericEnumFromThenenumFromThenTo :: Double -> Double -> Double -> [Double]enumFromThenTo=Double -> Double -> Double -> [Double]forall a. (Ord a, Fractional a) => a -> a -> a -> [a]numericEnumFromThenTo-------------------------------------------------------------------------- Printing floating point-------------------------------------------------------------------------- | Show a signed 'RealFloat' value to full precision-- using standard decimal notation for arguments whose absolute value lies-- between @0.1@ and @9,999,999@, and scientific notation otherwise.showFloat::(RealFloata)=>a->ShowSshowFloat :: a -> ShowSshowFloatax=String -> ShowSshowString(FFFormat -> Maybe Int -> a -> Stringforall a. RealFloat a => FFFormat -> Maybe Int -> a -> StringformatRealFloatFFFormatFFGenericMaybe Intforall a. Maybe aNothingax)-- These are the format types. This type is not exported.dataFFFormat=FFExponent|FFFixed|FFGeneric-- This is just a compatibility stub, as the "alt" argument formerly-- didn't exist.formatRealFloat::(RealFloata)=>FFFormat->MaybeInt->a->StringformatRealFloat :: FFFormat -> Maybe Int -> a -> StringformatRealFloatFFFormatfmtMaybe Intdecsax=FFFormat -> Maybe Int -> Bool -> a -> Stringforall a.RealFloat a =>FFFormat -> Maybe Int -> Bool -> a -> StringformatRealFloatAltFFFormatfmtMaybe IntdecsBoolFalseaxformatRealFloatAlt::(RealFloata)=>FFFormat->MaybeInt->Bool->a->StringformatRealFloatAlt :: FFFormat -> Maybe Int -> Bool -> a -> StringformatRealFloatAltFFFormatfmtMaybe IntdecsBoolaltax|a -> Boolforall a. RealFloat a => a -> BoolisNaNax=String"NaN"|a -> Boolforall a. RealFloat a => a -> BoolisInfiniteax=ifaxa -> a -> Boolforall a. Ord a => a -> a -> Bool<a0thenString"-Infinity"elseString"Infinity"|axa -> a -> Boolforall a. Ord a => a -> a -> Bool<a0Bool -> Bool -> Bool||a -> Boolforall a. RealFloat a => a -> BoolisNegativeZeroax=Char'-'Char -> ShowSforall a. a -> [a] -> [a]:FFFormat -> ([Int], Int) -> StringdoFmtFFFormatfmt(Integer -> a -> ([Int], Int)forall a. RealFloat a => Integer -> a -> ([Int], Int)floatToDigits(Int -> Integerforall a. Integral a => a -> IntegertoIntegerIntbase)(-ax))|Boolotherwise=FFFormat -> ([Int], Int) -> StringdoFmtFFFormatfmt(Integer -> a -> ([Int], Int)forall a. RealFloat a => Integer -> a -> ([Int], Int)floatToDigits(Int -> Integerforall a. Integral a => a -> IntegertoIntegerIntbase)ax)wherebase :: Intbase=Int10doFmt :: FFFormat -> ([Int], Int) -> StringdoFmtFFFormatformat([Int]is,Inte)=letds :: Stringds=(Int -> Char) -> [Int] -> Stringforall a b. (a -> b) -> [a] -> [b]mapInt -> CharintToDigit[Int]isincaseFFFormatformatofFFFormatFFGeneric->FFFormat -> ([Int], Int) -> StringdoFmt(ifInteInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0Bool -> Bool -> Bool||InteInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int7thenFFFormatFFExponentelseFFFormatFFFixed)([Int]is,Inte)FFFormatFFExponent->caseMaybe IntdecsofMaybe IntNothing->letshow_e' :: Stringshow_e'=Int -> Stringforall a. Show a => a -> Stringshow(InteInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)incaseStringdsofString"0"->String"0.0e0"[Chard]->ChardChar -> ShowSforall a. a -> [a] -> [a]:String".0e"String -> ShowSforall a. [a] -> [a] -> [a]++Stringshow_e'(Chard:Stringds')->ChardChar -> ShowSforall a. a -> [a] -> [a]:Char'.'Char -> ShowSforall a. a -> [a] -> [a]:Stringds'String -> ShowSforall a. [a] -> [a] -> [a]++String"e"String -> ShowSforall a. [a] -> [a] -> [a]++Stringshow_e'[]->ShowSforall a. String -> aerrorWithoutStackTraceString"formatRealFloat/doFmt/FFExponent: []"JustIntd|IntdInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0->-- handle this case specifically since we need to omit the-- decimal point as well (#15115).-- Note that this handles negative precisions as well for consistency-- (see #15509).case[Int]isof[Int0]->String"0e0"[Int]_->let(Intei,[Int]is')=Int -> Int -> [Int] -> (Int, [Int])roundToIntbaseInt1[Int]isCharn:String_=(Int -> Char) -> [Int] -> Stringforall a b. (a -> b) -> [a] -> [b]mapInt -> CharintToDigit(ifInteiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0then[Int] -> [Int]forall a. [a] -> [a]init[Int]is'else[Int]is')inCharnChar -> ShowSforall a. a -> [a] -> [a]:Char'e'Char -> ShowSforall a. a -> [a] -> [a]:Int -> Stringforall a. Show a => a -> Stringshow(InteInt -> Int -> Intforall a. Num a => a -> a -> a-Int1Int -> Int -> Intforall a. Num a => a -> a -> a+Intei)JustIntdec->letdec' :: Intdec'=Int -> Int -> Intforall a. Ord a => a -> a -> amaxIntdecInt1incase[Int]isof[Int0]->Char'0'Char -> ShowSforall a. a -> [a] -> [a]:Char'.'Char -> ShowSforall a. a -> [a] -> [a]:Int -> ShowSforall a. Int -> [a] -> [a]takeIntdec'(Char -> Stringforall a. a -> [a]repeatChar'0')String -> ShowSforall a. [a] -> [a] -> [a]++String"e0"[Int]_->let(Intei,[Int]is')=Int -> Int -> [Int] -> (Int, [Int])roundToIntbase(Intdec'Int -> Int -> Intforall a. Num a => a -> a -> a+Int1)[Int]is(Chard:Stringds')=(Int -> Char) -> [Int] -> Stringforall a b. (a -> b) -> [a] -> [b]mapInt -> CharintToDigit(ifInteiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0then[Int] -> [Int]forall a. [a] -> [a]init[Int]is'else[Int]is')inChardChar -> ShowSforall a. a -> [a] -> [a]:Char'.'Char -> ShowSforall a. a -> [a] -> [a]:Stringds'String -> ShowSforall a. [a] -> [a] -> [a]++Char'e'Char -> ShowSforall a. a -> [a] -> [a]:Int -> Stringforall a. Show a => a -> Stringshow(InteInt -> Int -> Intforall a. Num a => a -> a -> a-Int1Int -> Int -> Intforall a. Num a => a -> a -> a+Intei)FFFormatFFFixed->letmk0 :: ShowSmk0Stringls=caseStringlsof{String""->String"0";String_->Stringls}incaseMaybe IntdecsofMaybe IntNothing|InteInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0->String"0."String -> ShowSforall a. [a] -> [a] -> [a]++Int -> Char -> Stringforall a. Int -> a -> [a]replicate(-Inte)Char'0'String -> ShowSforall a. [a] -> [a] -> [a]++Stringds|Boolotherwise->letf :: a -> String -> ShowSfa0StringsStringrs=ShowSmk0(ShowSforall a. [a] -> [a]reverseStrings)String -> ShowSforall a. [a] -> [a] -> [a]++Char'.'Char -> ShowSforall a. a -> [a] -> [a]:ShowSmk0StringrsfanStringsString""=a -> String -> ShowSf(ana -> a -> aforall a. Num a => a -> a -> a-a1)(Char'0'Char -> ShowSforall a. a -> [a] -> [a]:Strings)String""fanStrings(Charr:Stringrs)=a -> String -> ShowSf(ana -> a -> aforall a. Num a => a -> a -> a-a1)(CharrChar -> ShowSforall a. a -> [a] -> [a]:Strings)StringrsinInt -> String -> ShowSforall a. (Eq a, Num a) => a -> String -> ShowSfInteString""StringdsJustIntdec->letdec' :: Intdec'=Int -> Int -> Intforall a. Ord a => a -> a -> amaxIntdecInt0inifInteInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0thenlet(Intei,[Int]is')=Int -> Int -> [Int] -> (Int, [Int])roundToIntbase(Intdec'Int -> Int -> Intforall a. Num a => a -> a -> a+Inte)[Int]is(Stringls,Stringrs)=Int -> String -> (String, String)forall a. Int -> [a] -> ([a], [a])splitAt(InteInt -> Int -> Intforall a. Num a => a -> a -> a+Intei)((Int -> Char) -> [Int] -> Stringforall a b. (a -> b) -> [a] -> [b]mapInt -> CharintToDigit[Int]is')inShowSmk0StringlsString -> ShowSforall a. [a] -> [a] -> [a]++(ifString -> Boolforall a. [a] -> BoolnullStringrsBool -> Bool -> Bool&&Bool -> BoolnotBoolaltthenString""elseChar'.'Char -> ShowSforall a. a -> [a] -> [a]:Stringrs)elselet(Intei,[Int]is')=Int -> Int -> [Int] -> (Int, [Int])roundToIntbaseIntdec'(Int -> Int -> [Int]forall a. Int -> a -> [a]replicate(-Inte)Int0[Int] -> [Int] -> [Int]forall a. [a] -> [a] -> [a]++[Int]is)Chard:Stringds'=(Int -> Char) -> [Int] -> Stringforall a b. (a -> b) -> [a] -> [b]mapInt -> CharintToDigit(ifInteiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0then[Int]is'elseInt0Int -> [Int] -> [Int]forall a. a -> [a] -> [a]:[Int]is')inChardChar -> ShowSforall a. a -> [a] -> [a]:(ifString -> Boolforall a. [a] -> BoolnullStringds'Bool -> Bool -> Bool&&Bool -> BoolnotBoolaltthenString""elseChar'.'Char -> ShowSforall a. a -> [a] -> [a]:Stringds')roundTo::Int->Int->[Int]->(Int,[Int])roundTo :: Int -> Int -> [Int] -> (Int, [Int])roundToIntbaseIntd[Int]is=caseInt -> Bool -> [Int] -> (Int, [Int])fIntdBoolTrue[Int]isofx :: (Int, [Int])x@(Int0,[Int]_)->(Int, [Int])x(Int1,[Int]xs)->(Int1,Int1Int -> [Int] -> [Int]forall a. a -> [a] -> [a]:[Int]xs)(Int, [Int])_->String -> (Int, [Int])forall a. String -> aerrorWithoutStackTraceString"roundTo: bad Value"whereb2 :: Intb2=IntbaseInt -> Int -> Intforall a. Integral a => a -> a -> a`quot`Int2f :: Int -> Bool -> [Int] -> (Int, [Int])fIntnBool_[]=(Int0,Int -> Int -> [Int]forall a. Int -> a -> [a]replicateIntnInt0)fInt0Boole(Intx:[Int]xs)|IntxInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Intb2Bool -> Bool -> Bool&&BooleBool -> Bool -> Bool&&(Int -> Bool) -> [Int] -> Boolforall a. (a -> Bool) -> [a] -> Boolall(Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0)[Int]xs=(Int0,[])-- Round to even when at exactly half the base|Boolotherwise=(ifIntxInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intb2thenInt1elseInt0,[])fIntnBool_(Inti:[Int]xs)|Inti'Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==Intbase=(Int1,Int0Int -> [Int] -> [Int]forall a. a -> [a] -> [a]:[Int]ds)|Boolotherwise=(Int0,Inti'Int -> [Int] -> [Int]forall a. a -> [a] -> [a]:[Int]ds)where(Intc,[Int]ds)=Int -> Bool -> [Int] -> (Int, [Int])f(IntnInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)(Int -> Boolforall a. Integral a => a -> BoolevenInti)[Int]xsi' :: Inti'=IntcInt -> Int -> Intforall a. Num a => a -> a -> a+Inti-- 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.-- | '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---- (1) @n >= 1@---- (2) @x = 0.d1d2...dn * (base**e)@---- (3) @0 <= di <= base-1@floatToDigits::(RealFloata)=>Integer->a->([Int],Int)floatToDigits :: Integer -> a -> ([Int], Int)floatToDigitsInteger_a0=([Int0],Int0)floatToDigitsIntegerbaseax=let(Integerf0,Inte0)=a -> (Integer, Int)forall a. RealFloat a => a -> (Integer, Int)decodeFloatax(IntminExp0,Int_)=a -> (Int, Int)forall a. RealFloat a => a -> (Int, Int)floatRangeaxp :: Intp=a -> Intforall a. RealFloat a => a -> IntfloatDigitsaxb :: Integerb=a -> Integerforall a. RealFloat a => a -> IntegerfloatRadixaxminExp :: IntminExp=IntminExp0Int -> Int -> Intforall a. Num a => a -> a -> a-Intp-- the real minimum exponent-- Haskell requires that f be adjusted so denormalized numbers-- will have an impossibly low exponent. Adjust for this.(Integerf,Inte)=letn :: Intn=IntminExpInt -> Int -> Intforall a. Num a => a -> a -> a-Inte0inifIntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0then(Integerf0Integer -> Integer -> Integerforall a. Integral a => a -> a -> a`quot`(Integer -> Int -> IntegerexptIntegerbIntn),Inte0Int -> Int -> Intforall a. Num a => a -> a -> a+Intn)else(Integerf0,Inte0)(Integerr,Integers,IntegermUp,IntegermDn)=ifInteInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0thenletbe :: Integerbe=Integer -> Int -> IntegerexptIntegerbInteinifIntegerfInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer -> Int -> IntegerexptIntegerb(IntpInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)then(IntegerfInteger -> Integer -> Integerforall a. Num a => a -> a -> a*IntegerbeInteger -> Integer -> Integerforall a. Num a => a -> a -> a*IntegerbInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integer2,Integer2Integer -> Integer -> Integerforall a. Num a => a -> a -> a*Integerb,IntegerbeInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integerb,Integerbe)-- according to Burger and Dybvigelse(IntegerfInteger -> Integer -> Integerforall a. Num a => a -> a -> a*IntegerbeInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integer2,Integer2,Integerbe,Integerbe)elseifInteInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>IntminExpBool -> Bool -> Bool&&IntegerfInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer -> Int -> IntegerexptIntegerb(IntpInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)then(IntegerfInteger -> Integer -> Integerforall a. Num a => a -> a -> a*IntegerbInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integer2,Integer -> Int -> IntegerexptIntegerb(-InteInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)Integer -> Integer -> Integerforall a. Num a => a -> a -> a*Integer2,Integerb,Integer1)else(IntegerfInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integer2,Integer -> Int -> IntegerexptIntegerb(-Inte)Integer -> Integer -> Integerforall a. Num a => a -> a -> a*Integer2,Integer1,Integer1)k::Intk :: Intk=letk0::Intk0 :: Intk0=ifIntegerbInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer2Bool -> Bool -> Bool&&IntegerbaseInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer10then-- logBase 10 2 is very slightly larger than 8651/28738-- (about 5.3558e-10), so if log x >= 0, the approximation-- k1 is too small, hence we add one and need one fixup step less.-- If log x < 0, the approximation errs rather on the high side.-- That is usually more than compensated for by ignoring the-- fractional part of logBase 2 x, but when x is a power of 1/2-- or slightly larger and the exponent is a multiple of the-- denominator of the rational approximation to logBase 10 2,-- k1 is larger than logBase 10 x. If k1 > 1 + logBase 10 x,-- we get a leading zero-digit we don't want.-- With the approximation 3/10, this happened for-- 0.5^1030, 0.5^1040, ..., 0.5^1070 and values close above.-- The approximation 8651/28738 guarantees k1 < 1 + logBase 10 x-- for IEEE-ish floating point types with exponent fields-- <= 17 bits and mantissae of several thousand bits, earlier-- convergents to logBase 10 2 would fail for long double.-- Using quot instead of div is a little faster and requires-- fewer fixup steps for negative lx.letlx :: Intlx=IntpInt -> Int -> Intforall a. Num a => a -> a -> a-Int1Int -> Int -> Intforall a. Num a => a -> a -> a+Inte0k1 :: Intk1=(IntlxInt -> Int -> Intforall a. Num a => a -> a -> a*Int8651)Int -> Int -> Intforall a. Integral a => a -> a -> a`quot`Int28738inifIntlxInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0thenIntk1Int -> Int -> Intforall a. Num a => a -> a -> a+Int1elseIntk1else-- f :: Integer, log :: Float -> Float,-- ceiling :: Float -> IntFloat -> Intforall a b. (RealFrac a, Integral b) => a -> bceiling((Float -> Floatforall a. Floating a => a -> alog(Integer -> Floatforall a. Num a => Integer -> afromInteger(IntegerfInteger -> Integer -> Integerforall a. Num a => a -> a -> a+Integer1)::Float)Float -> Float -> Floatforall a. Num a => a -> a -> a+Int -> Floatforall a b. (Integral a, Num b) => a -> bfromIntegralInteFloat -> Float -> Floatforall a. Num a => a -> a -> a*Float -> Floatforall a. Floating a => a -> alog(Integer -> Floatforall a. Num a => Integer -> afromIntegerIntegerb))Float -> Float -> Floatforall a. Fractional a => a -> a -> a/Float -> Floatforall a. Floating a => a -> alog(Integer -> Floatforall a. Num a => Integer -> afromIntegerIntegerbase))--WAS: fromInt e * log (fromInteger b))fixup :: Int -> IntfixupIntn=ifIntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0thenifIntegerrInteger -> Integer -> Integerforall a. Num a => a -> a -> a+IntegermUpInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool<=Integer -> Int -> IntegerexptIntegerbaseIntnInteger -> Integer -> Integerforall a. Num a => a -> a -> a*IntegersthenIntnelseInt -> Intfixup(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)elseifInteger -> Int -> IntegerexptIntegerbase(-Intn)Integer -> Integer -> Integerforall a. Num a => a -> a -> a*(IntegerrInteger -> Integer -> Integerforall a. Num a => a -> a -> a+IntegermUp)Integer -> Integer -> Boolforall a. Ord a => a -> a -> Bool<=IntegersthenIntnelseInt -> Intfixup(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)inInt -> IntfixupIntk0gen :: [Integer] -> Integer -> Integer -> Integer -> Integer -> [Integer]gen[Integer]dsIntegerrnIntegersNIntegermUpNIntegermDnN=let(Integerdn,Integerrn')=(IntegerrnInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integerbase)Integer -> Integer -> (Integer, Integer)forall a. Integral a => a -> a -> (a, a)`quotRem`IntegersNmUpN' :: IntegermUpN'=IntegermUpNInteger -> Integer -> Integerforall a. Num a => a -> a -> a*IntegerbasemDnN' :: IntegermDnN'=IntegermDnNInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integerbaseincase(Integerrn'Integer -> Integer -> Boolforall a. Ord a => a -> a -> Bool<IntegermDnN',Integerrn'Integer -> Integer -> Integerforall a. Num a => a -> a -> a+IntegermUpN'Integer -> Integer -> Boolforall a. Ord a => a -> a -> Bool>IntegersN)of(BoolTrue,BoolFalse)->IntegerdnInteger -> [Integer] -> [Integer]forall a. a -> [a] -> [a]:[Integer]ds(BoolFalse,BoolTrue)->IntegerdnInteger -> Integer -> Integerforall a. Num a => a -> a -> a+Integer1Integer -> [Integer] -> [Integer]forall a. a -> [a] -> [a]:[Integer]ds(BoolTrue,BoolTrue)->ifIntegerrn'Integer -> Integer -> Integerforall a. Num a => a -> a -> a*Integer2Integer -> Integer -> Boolforall a. Ord a => a -> a -> Bool<IntegersNthenIntegerdnInteger -> [Integer] -> [Integer]forall a. a -> [a] -> [a]:[Integer]dselseIntegerdnInteger -> Integer -> Integerforall a. Num a => a -> a -> a+Integer1Integer -> [Integer] -> [Integer]forall a. a -> [a] -> [a]:[Integer]ds(BoolFalse,BoolFalse)->[Integer] -> Integer -> Integer -> Integer -> Integer -> [Integer]gen(IntegerdnInteger -> [Integer] -> [Integer]forall a. a -> [a] -> [a]:[Integer]ds)Integerrn'IntegersNIntegermUpN'IntegermDnN'rds :: [Integer]rds=ifIntkInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0then[Integer] -> Integer -> Integer -> Integer -> Integer -> [Integer]gen[]Integerr(IntegersInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integer -> Int -> IntegerexptIntegerbaseIntk)IntegermUpIntegermDnelseletbk :: Integerbk=Integer -> Int -> IntegerexptIntegerbase(-Intk)in[Integer] -> Integer -> Integer -> Integer -> Integer -> [Integer]gen[](IntegerrInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integerbk)Integers(IntegermUpInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integerbk)(IntegermDnInteger -> Integer -> Integerforall a. Num a => a -> a -> a*Integerbk)in((Integer -> Int) -> [Integer] -> [Int]forall a b. (a -> b) -> [a] -> [b]mapInteger -> Intforall a b. (Integral a, Num b) => a -> bfromIntegral([Integer] -> [Integer]forall a. [a] -> [a]reverse[Integer]rds),Intk)-------------------------------------------------------------------------- Converting from a Rational to a RealFloa------------------------------------------------------------------------{-[In response to a request for documentation of how fromRational works,Joe Fasel writes:] A quite reasonable request! This code was added tothe Prelude just before the 1.2 release, when Lennart, working with anearly version of hbi, noticed that (read . show) was not the identityfor floating-point numbers. (There was a one-bit error about half thetime.) The original version of the conversion function was in factsimply a floating-point divide, as you suggest above. The new versionis, I grant you, somewhat denser.Unfortunately, Joe's code doesn't work! Here's an example:main = putStr (shows (1.82173691287639817263897126389712638972163e-300::Double) "\n")This program prints 0.0000000000000000instead of 1.8217369128763981e-300Here's Joe's code:\begin{pseudocode}fromRat :: (RealFloat a) => Rational -> afromRat x = x' where x' = f e-- If the exponent of the nearest floating-point number to x-- is e, then the significand is the integer nearest xb^(-e),-- where b is the floating-point radix. We start with a good-- guess for e, and if it is correct, the exponent of the-- floating-point number we construct will again be e. If-- not, one more iteration is needed. f e = if e' == e then y else f e' where y = encodeFloat (round (x * (1 % b)^^e)) e (_,e') = decodeFloat y b = floatRadix x'-- We obtain a trial exponent by doing a floating-point-- division of x's numerator by its denominator. The-- result of this division may not itself be the ultimate-- result, because of an accumulation of three rounding-- errors. (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x' / fromInteger (denominator x))\end{pseudocode}Now, here's Lennart's code (which works):-}-- | Converts a 'Rational' value into any type in class 'RealFloat'.{-# RULES"fromRat/Float"fromRat=(fromRational::Rational->Float)"fromRat/Double"fromRat=(fromRational::Rational->Double)#-}{-# NOINLINE[1]fromRat#-}fromRat::(RealFloata)=>Rational->a-- Deal with special cases first, delegating the real work to fromRat'fromRat :: Rational -> afromRat(Integern:%Integer0)|IntegernInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool>Integer0=a1a -> a -> aforall a. Fractional a => a -> a -> a/a0-- +Infinity|IntegernInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool<Integer0=-a1a -> a -> aforall a. Fractional a => a -> a -> a/a0-- -Infinity|Boolotherwise=a0a -> a -> aforall a. Fractional a => a -> a -> a/a0-- NaNfromRat(Integern:%Integerd)|IntegernInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool>Integer0=Rational -> aforall a. RealFloat a => Rational -> afromRat'(IntegernInteger -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integerd)|IntegernInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool<Integer0=-Rational -> aforall a. RealFloat a => Rational -> afromRat'((-Integern)Integer -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integerd)|Boolotherwise=Integer -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatInteger0Int0-- Zero-- Conversion process:-- Scale the rational number by the RealFloat base until-- it lies in the range of the mantissa (as used by decodeFloat/encodeFloat).-- Then round the rational to an Integer and encode it with the exponent-- that we got from the scaling.-- To speed up the scaling process we compute the log2 of the number to get-- a first guess of the exponent.fromRat'::(RealFloata)=>Rational->a-- Invariant: argument is strictly positivefromRat' :: Rational -> afromRat'Rationalx=arwhereb :: Integerb=a -> Integerforall a. RealFloat a => a -> IntegerfloatRadixarp :: Intp=a -> Intforall a. RealFloat a => a -> IntfloatDigitsar(IntminExp0,Int_)=a -> (Int, Int)forall a. RealFloat a => a -> (Int, Int)floatRangearminExp :: IntminExp=IntminExp0Int -> Int -> Intforall a. Num a => a -> a -> a-Intp-- the real minimum exponentxMax :: RationalxMax=Integer -> Rationalforall a. Real a => a -> RationaltoRational(Integer -> Int -> IntegerexptIntegerbIntp)p0 :: Intp0=(Integer -> Integer -> IntintegerLogBaseIntegerb(Rational -> Integerforall a. Ratio a -> anumeratorRationalx)Int -> Int -> Intforall a. Num a => a -> a -> a-Integer -> Integer -> IntintegerLogBaseIntegerb(Rational -> Integerforall a. Ratio a -> adenominatorRationalx)Int -> Int -> Intforall a. Num a => a -> a -> a-Intp)Int -> Int -> Intforall a. Ord a => a -> a -> a`max`IntminExp-- if x = n/d and ln = integerLogBase b n, ld = integerLogBase b d,-- then b^(ln-ld-1) < x < b^(ln-ld+1)f :: Rationalf=ifIntp0Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0thenInteger1Integer -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integer -> Int -> IntegerexptIntegerb(-Intp0)elseInteger -> Int -> IntegerexptIntegerbIntp0Integer -> Integer -> Rationalforall a. a -> a -> Ratio a:%Integer1x0 :: Rationalx0=RationalxRational -> Rational -> Rationalforall a. Fractional a => a -> a -> a/Rationalf-- if ln - ld >= minExp0, then b^(p-1) < x0 < b^(p+1), so there's at most-- one scaling step needed, otherwise, x0 < b^p and no scaling is needed(Rationalx',Intp')=ifRationalx0Rational -> Rational -> Boolforall a. Ord a => a -> a -> Bool>=RationalxMaxthen(Rationalx0Rational -> Rational -> Rationalforall a. Fractional a => a -> a -> a/Integer -> Rationalforall a. Real a => a -> RationaltoRationalIntegerb,Intp0Int -> Int -> Intforall a. Num a => a -> a -> a+Int1)else(Rationalx0,Intp0)r :: ar=Integer -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloat(Rational -> Integerforall a b. (RealFrac a, Integral b) => a -> broundRationalx')Intp'-- Exponentiation with a cache for the most common numbers.minExpt,maxExpt::IntminExpt :: IntminExpt=Int0maxExpt :: IntmaxExpt=Int1100expt::Integer->Int->Integerexpt :: Integer -> Int -> IntegerexptIntegerbaseIntn=ifIntegerbaseInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer2Bool -> Bool -> Bool&&IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=IntminExptBool -> Bool -> Bool&&IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntmaxExptthenArray Int IntegerexptsArray Int Integer -> Int -> Integerforall i e. Ix i => Array i e -> i -> e!IntnelseifIntegerbaseInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer10Bool -> Bool -> Bool&&IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntmaxExpt10thenArray Int Integerexpts10Array Int Integer -> Int -> Integerforall i e. Ix i => Array i e -> i -> e!IntnelseIntegerbaseInteger -> Int -> Integerforall a b. (Num a, Integral b) => a -> b -> a^Intnexpts::ArrayIntIntegerexpts :: Array Int Integerexpts=(Int, Int) -> [(Int, Integer)] -> Array Int Integerforall i e. Ix i => (i, i) -> [(i, e)] -> Array i earray(IntminExpt,IntmaxExpt)[(Intn,Integer2Integer -> Int -> Integerforall a b. (Num a, Integral b) => a -> b -> a^Intn)|Intn<-[IntminExpt..IntmaxExpt]]maxExpt10::IntmaxExpt10 :: IntmaxExpt10=Int324expts10::ArrayIntIntegerexpts10 :: Array Int Integerexpts10=(Int, Int) -> [(Int, Integer)] -> Array Int Integerforall i e. Ix i => (i, i) -> [(i, e)] -> Array i earray(IntminExpt,IntmaxExpt10)[(Intn,Integer10Integer -> Int -> Integerforall a b. (Num a, Integral b) => a -> b -> a^Intn)|Intn<-[IntminExpt..IntmaxExpt10]]-- Compute the (floor of the) log of i in base b.-- Simplest way would be just divide i by b until it's smaller then b, but that would-- be very slow! We are just slightly more clever, except for base 2, where-- we take advantage of the representation of Integers.-- The general case could be improved by a lookup table for-- approximating the result by integerLog2 i / integerLog2 b.integerLogBase::Integer->Integer->IntintegerLogBase :: Integer -> Integer -> IntintegerLogBaseIntegerbIntegeri|IntegeriInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool<Integerb=Int0|IntegerbInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Integer2=Int# -> IntI#(Integer -> Int#integerLog2#Integeri)|Boolotherwise=Int# -> IntI#(Integer -> Integer -> Int#integerLogBase#IntegerbIntegeri){-Unfortunately, the old conversion code was awfully slow due toa) a slow integer logarithmb) repeated calculation of gcd'sFor the case of Rational's coming from a Float or Double via toRational,we can exploit the fact that the denominator is a power of two, which forthese brings a huge speedup since we need only shift and add insteadof division.The below is an adaption of fromRat' for the conversion toFloat or Double exploiting the known floatRadix and avoidingdivisions as much as possible.-}{-# SPECIALISEfromRat''::Int->Int->Integer->Integer->Float,Int->Int->Integer->Integer->Double#-}fromRat''::RealFloata=>Int->Int->Integer->Integer->a-- Invariant: n and d strictly positivefromRat'' :: Int -> Int -> Integer -> Integer -> afromRat''minEx :: IntminEx@(I#Int#me#)mantDigs :: IntmantDigs@(I#Int#md#)IntegernIntegerd=caseInteger -> (# Int#, Int# #)integerLog2IsPowerOf2#Integerdof(#Int#ld#,Int#pw##)|Int# -> BoolisTrue#(Int#pw#Int# -> Int# -> Int#==#Int#0#)->caseInteger -> Int#integerLog2#IntegernofInt#ln#|Int# -> BoolisTrue#(Int#ln#Int# -> Int# -> Int#>=#(Int#ld#Int# -> Int# -> Int#+#Int#me#Int# -> Int# -> Int#-#Int#1#))->-- this means n/d >= 2^(minEx-1), i.e. we are guaranteed to get-- a normalised number, round to mantDigs bitsifInt# -> BoolisTrue#(Int#ln#Int# -> Int# -> Int#<#Int#md#)thenInteger -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegern(Int# -> IntI#(Int# -> Int#negateInt#Int#ld#))elseletn' :: Integern'=IntegernInteger -> Int -> Integerforall a. Bits a => a -> Int -> a`shiftR`(Int# -> IntI#(Int#ln#Int# -> Int# -> Int#+#Int#1#Int# -> Int# -> Int#-#Int#md#))n'' :: Integern''=caseInteger -> Int# -> Int#roundingMode#Integern(Int#ln#Int# -> Int# -> Int#-#Int#md#)ofInt#0#->Integern'Int#2#->Integern'Integer -> Integer -> Integerforall a. Num a => a -> a -> a+Integer1Int#_->caseInteger -> Intforall a. Num a => Integer -> afromIntegerIntegern'Int -> Int -> Intforall a. Bits a => a -> a -> a.&.(Int1::Int)ofInt0->Integern'Int_->Integern'Integer -> Integer -> Integerforall a. Num a => a -> a -> a+Integer1inInteger -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegern''(Int# -> IntI#(Int#ln#Int# -> Int# -> Int#-#Int#ld#Int# -> Int# -> Int#+#Int#1#Int# -> Int# -> Int#-#Int#md#))|Boolotherwise->-- n/d < 2^(minEx-1), a denorm or rounded to 2^(minEx-1)-- the exponent for encoding is always minEx-mantDigs-- so we must shift right by (minEx-mantDigs) - (-ld)caseInt#ld#Int# -> Int# -> Int#+#(Int#me#Int# -> Int# -> Int#-#Int#md#)ofInt#ld'#|Int# -> BoolisTrue#(Int#ld'#Int# -> Int# -> Int#<=#Int#0#)->-- we would shift left, so we don't shiftInteger -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegern(Int# -> IntI#((Int#me#Int# -> Int# -> Int#-#Int#md#)Int# -> Int# -> Int#-#Int#ld'#))|Int# -> BoolisTrue#(Int#ld'#Int# -> Int# -> Int#<=#Int#ln#)->letn' :: Integern'=IntegernInteger -> Int -> Integerforall a. Bits a => a -> Int -> a`shiftR`(Int# -> IntI#Int#ld'#)incaseInteger -> Int# -> Int#roundingMode#Integern(Int#ld'#Int# -> Int# -> Int#-#Int#1#)ofInt#0#->Integer -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegern'(IntminExInt -> Int -> Intforall a. Num a => a -> a -> a-IntmantDigs)Int#1#->ifInteger -> Intforall a. Num a => Integer -> afromIntegerIntegern'Int -> Int -> Intforall a. Bits a => a -> a -> a.&.(Int1::Int)Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0thenInteger -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegern'(IntminExInt -> Int -> Intforall a. Num a => a -> a -> a-IntmantDigs)elseInteger -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloat(Integern'Integer -> Integer -> Integerforall a. Num a => a -> a -> a+Integer1)(IntminExInt -> Int -> Intforall a. Num a => a -> a -> a-IntmantDigs)Int#_->Integer -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloat(Integern'Integer -> Integer -> Integerforall a. Num a => a -> a -> a+Integer1)(IntminExInt -> Int -> Intforall a. Num a => a -> a -> a-IntmantDigs)|Int# -> BoolisTrue#(Int#ld'#Int# -> Int# -> Int#>#(Int#ln#Int# -> Int# -> Int#+#Int#1#))->Integer -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatInteger0Int0-- result of shift < 0.5|Boolotherwise->-- first bit of n shifted to 0.5 placecaseInteger -> (# Int#, Int# #)integerLog2IsPowerOf2#Integernof(#Int#_,Int#0##)->Integer -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatInteger0Int0-- round to even(#Int#_,Int#_#)->Integer -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatInteger1(IntminExInt -> Int -> Intforall a. Num a => a -> a -> a-IntmantDigs)|Boolotherwise->letln :: Intln=Int# -> IntI#(Integer -> Int#integerLog2#Integern)ld :: Intld=Int# -> IntI#Int#ld#-- 2^(ln-ld-1) < n/d < 2^(ln-ld+1)p0 :: Intp0=Int -> Int -> Intforall a. Ord a => a -> a -> amaxIntminEx(IntlnInt -> Int -> Intforall a. Num a => a -> a -> a-Intld)(Integern',Integerd')|Intp0Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<IntmantDigs=(IntegernInteger -> Int -> Integerforall a. Bits a => a -> Int -> a`shiftL`(IntmantDigsInt -> Int -> Intforall a. Num a => a -> a -> a-Intp0),Integerd)|Intp0Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==IntmantDigs=(Integern,Integerd)|Boolotherwise=(Integern,IntegerdInteger -> Int -> Integerforall a. Bits a => a -> Int -> a`shiftL`(Intp0Int -> Int -> Intforall a. Num a => a -> a -> a-IntmantDigs))-- if ln-ld < minEx, then n'/d' < 2^mantDigs, else-- 2^(mantDigs-1) < n'/d' < 2^(mantDigs+1) and we-- may need one scaling stepscale :: a -> c -> c -> (a, c, c)scaleapcacb|(cbc -> Int -> cforall a. Bits a => a -> Int -> a`shiftL`IntmantDigs)c -> c -> Boolforall a. Ord a => a -> a -> Bool<=ca=(apa -> a -> aforall a. Num a => a -> a -> a+a1,ca,cbc -> Int -> cforall a. Bits a => a -> Int -> a`shiftL`Int1)|Boolotherwise=(ap,ca,cb)(Intp',Integern'',Integerd'')=Int -> Integer -> Integer -> (Int, Integer, Integer)forall c a. (Ord c, Bits c, Num a) => a -> c -> c -> (a, c, c)scale(Intp0Int -> Int -> Intforall a. Num a => a -> a -> a-IntmantDigs)Integern'Integerd'-- n''/d'' < 2^mantDigs and p' == minEx-mantDigs or n''/d'' >= 2^(mantDigs-1)rdq :: Integerrdq=caseIntegern''Integer -> Integer -> (Integer, Integer)forall a. Integral a => a -> a -> (a, a)`quotRem`Integerd''of(Integerq,Integerr)->caseInteger -> Integer -> Orderingforall a. Ord a => a -> a -> Orderingcompare(IntegerrInteger -> Int -> Integerforall a. Bits a => a -> Int -> a`shiftL`Int1)Integerd''ofOrderingLT->IntegerqOrderingEQ->ifInteger -> Intforall a. Num a => Integer -> afromIntegerIntegerqInt -> Int -> Intforall a. Bits a => a -> a -> a.&.(Int1::Int)Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0thenIntegerqelseIntegerqInteger -> Integer -> Integerforall a. Num a => a -> a -> a+Integer1OrderingGT->IntegerqInteger -> Integer -> Integerforall a. Num a => a -> a -> a+Integer1inInteger -> Int -> aforall a. RealFloat a => Integer -> Int -> aencodeFloatIntegerrdqIntp'-------------------------------------------------------------------------- Floating point numeric primops-------------------------------------------------------------------------- Definitions of the boxed PrimOps; these will be-- used in the case of partial applications, etc.plusFloat,minusFloat,timesFloat,divideFloat::Float->Float->FloatplusFloat :: Float -> Float -> FloatplusFloat(F#Float#x)(F#Float#y)=Float# -> FloatF#(Float# -> Float# -> Float#plusFloat#Float#xFloat#y)minusFloat :: Float -> Float -> FloatminusFloat(F#Float#x)(F#Float#y)=Float# -> FloatF#(Float# -> Float# -> Float#minusFloat#Float#xFloat#y)timesFloat :: Float -> Float -> FloattimesFloat(F#Float#x)(F#Float#y)=Float# -> FloatF#(Float# -> Float# -> Float#timesFloat#Float#xFloat#y)divideFloat :: Float -> Float -> FloatdivideFloat(F#Float#x)(F#Float#y)=Float# -> FloatF#(Float# -> Float# -> Float#divideFloat#Float#xFloat#y)negateFloat::Float->FloatnegateFloat :: Float -> FloatnegateFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#negateFloat#Float#x)gtFloat,geFloat,ltFloat,leFloat::Float->Float->BoolgtFloat :: Float -> Float -> BoolgtFloat(F#Float#x)(F#Float#y)=Int# -> BoolisTrue#(Float# -> Float# -> Int#gtFloat#Float#xFloat#y)geFloat :: Float -> Float -> BoolgeFloat(F#Float#x)(F#Float#y)=Int# -> BoolisTrue#(Float# -> Float# -> Int#geFloat#Float#xFloat#y)ltFloat :: Float -> Float -> BoolltFloat(F#Float#x)(F#Float#y)=Int# -> BoolisTrue#(Float# -> Float# -> Int#ltFloat#Float#xFloat#y)leFloat :: Float -> Float -> BoolleFloat(F#Float#x)(F#Float#y)=Int# -> BoolisTrue#(Float# -> Float# -> Int#leFloat#Float#xFloat#y)expFloat,expm1Float::Float->FloatlogFloat,log1pFloat,sqrtFloat,fabsFloat::Float->FloatsinFloat,cosFloat,tanFloat::Float->FloatasinFloat,acosFloat,atanFloat::Float->FloatsinhFloat,coshFloat,tanhFloat::Float->FloatasinhFloat,acoshFloat,atanhFloat::Float->FloatexpFloat :: Float -> FloatexpFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#expFloat#Float#x)expm1Float :: Float -> Floatexpm1Float(F#Float#x)=Float# -> FloatF#(Float# -> Float#expm1Float#Float#x)logFloat :: Float -> FloatlogFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#logFloat#Float#x)log1pFloat :: Float -> Floatlog1pFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#log1pFloat#Float#x)sqrtFloat :: Float -> FloatsqrtFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#sqrtFloat#Float#x)fabsFloat :: Float -> FloatfabsFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#fabsFloat#Float#x)sinFloat :: Float -> FloatsinFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#sinFloat#Float#x)cosFloat :: Float -> FloatcosFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#cosFloat#Float#x)tanFloat :: Float -> FloattanFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#tanFloat#Float#x)asinFloat :: Float -> FloatasinFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#asinFloat#Float#x)acosFloat :: Float -> FloatacosFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#acosFloat#Float#x)atanFloat :: Float -> FloatatanFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#atanFloat#Float#x)sinhFloat :: Float -> FloatsinhFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#sinhFloat#Float#x)coshFloat :: Float -> FloatcoshFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#coshFloat#Float#x)tanhFloat :: Float -> FloattanhFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#tanhFloat#Float#x)asinhFloat :: Float -> FloatasinhFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#asinhFloat#Float#x)acoshFloat :: Float -> FloatacoshFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#acoshFloat#Float#x)atanhFloat :: Float -> FloatatanhFloat(F#Float#x)=Float# -> FloatF#(Float# -> Float#atanhFloat#Float#x)powerFloat::Float->Float->FloatpowerFloat :: Float -> Float -> FloatpowerFloat(F#Float#x)(F#Float#y)=Float# -> FloatF#(Float# -> Float# -> Float#powerFloat#Float#xFloat#y)-- definitions of the boxed PrimOps; these will be-- used in the case of partial applications, etc.plusDouble,minusDouble,timesDouble,divideDouble::Double->Double->DoubleplusDouble :: Double -> Double -> DoubleplusDouble(D#Double#x)(D#Double#y)=Double# -> DoubleD#(Double#xDouble# -> Double# -> Double#+##Double#y)minusDouble :: Double -> Double -> DoubleminusDouble(D#Double#x)(D#Double#y)=Double# -> DoubleD#(Double#xDouble# -> Double# -> Double#-##Double#y)timesDouble :: Double -> Double -> DoubletimesDouble(D#Double#x)(D#Double#y)=Double# -> DoubleD#(Double#xDouble# -> Double# -> Double#*##Double#y)divideDouble :: Double -> Double -> DoubledivideDouble(D#Double#x)(D#Double#y)=Double# -> DoubleD#(Double#xDouble# -> Double# -> Double#/##Double#y)negateDouble::Double->DoublenegateDouble :: Double -> DoublenegateDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#negateDouble#Double#x)gtDouble,geDouble,leDouble,ltDouble::Double->Double->BoolgtDouble :: Double -> Double -> BoolgtDouble(D#Double#x)(D#Double#y)=Int# -> BoolisTrue#(Double#xDouble# -> Double# -> Int#>##Double#y)geDouble :: Double -> Double -> BoolgeDouble(D#Double#x)(D#Double#y)=Int# -> BoolisTrue#(Double#xDouble# -> Double# -> Int#>=##Double#y)ltDouble :: Double -> Double -> BoolltDouble(D#Double#x)(D#Double#y)=Int# -> BoolisTrue#(Double#xDouble# -> Double# -> Int#<##Double#y)leDouble :: Double -> Double -> BoolleDouble(D#Double#x)(D#Double#y)=Int# -> BoolisTrue#(Double#xDouble# -> Double# -> Int#<=##Double#y)double2Float::Double->Floatdouble2Float :: Double -> Floatdouble2Float(D#Double#x)=Float# -> FloatF#(Double# -> Float#double2Float#Double#x)float2Double::Float->Doublefloat2Double :: Float -> Doublefloat2Double(F#Float#x)=Double# -> DoubleD#(Float# -> Double#float2Double#Float#x)expDouble,expm1Double::Double->DoublelogDouble,log1pDouble,sqrtDouble,fabsDouble::Double->DoublesinDouble,cosDouble,tanDouble::Double->DoubleasinDouble,acosDouble,atanDouble::Double->DoublesinhDouble,coshDouble,tanhDouble::Double->DoubleasinhDouble,acoshDouble,atanhDouble::Double->DoubleexpDouble :: Double -> DoubleexpDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#expDouble#Double#x)expm1Double :: Double -> Doubleexpm1Double(D#Double#x)=Double# -> DoubleD#(Double# -> Double#expm1Double#Double#x)logDouble :: Double -> DoublelogDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#logDouble#Double#x)log1pDouble :: Double -> Doublelog1pDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#log1pDouble#Double#x)sqrtDouble :: Double -> DoublesqrtDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#sqrtDouble#Double#x)fabsDouble :: Double -> DoublefabsDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#fabsDouble#Double#x)sinDouble :: Double -> DoublesinDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#sinDouble#Double#x)cosDouble :: Double -> DoublecosDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#cosDouble#Double#x)tanDouble :: Double -> DoubletanDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#tanDouble#Double#x)asinDouble :: Double -> DoubleasinDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#asinDouble#Double#x)acosDouble :: Double -> DoubleacosDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#acosDouble#Double#x)atanDouble :: Double -> DoubleatanDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#atanDouble#Double#x)sinhDouble :: Double -> DoublesinhDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#sinhDouble#Double#x)coshDouble :: Double -> DoublecoshDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#coshDouble#Double#x)tanhDouble :: Double -> DoubletanhDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#tanhDouble#Double#x)asinhDouble :: Double -> DoubleasinhDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#asinhDouble#Double#x)acoshDouble :: Double -> DoubleacoshDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#acoshDouble#Double#x)atanhDouble :: Double -> DoubleatanhDouble(D#Double#x)=Double# -> DoubleD#(Double# -> Double#atanhDouble#Double#x)powerDouble::Double->Double->DoublepowerDouble :: Double -> Double -> DoublepowerDouble(D#Double#x)(D#Double#y)=Double# -> DoubleD#(Double#xDouble# -> Double# -> Double#**##Double#y)foreignimportccallunsafe"isFloatNaN"isFloatNaN::Float->Intforeignimportccallunsafe"isFloatInfinite"isFloatInfinite::Float->Intforeignimportccallunsafe"isFloatDenormalized"isFloatDenormalized::Float->Intforeignimportccallunsafe"isFloatNegativeZero"isFloatNegativeZero::Float->Intforeignimportccallunsafe"isFloatFinite"isFloatFinite::Float->Intforeignimportccallunsafe"isDoubleNaN"isDoubleNaN::Double->Intforeignimportccallunsafe"isDoubleInfinite"isDoubleInfinite::Double->Intforeignimportccallunsafe"isDoubleDenormalized"isDoubleDenormalized::Double->Intforeignimportccallunsafe"isDoubleNegativeZero"isDoubleNegativeZero::Double->Intforeignimportccallunsafe"isDoubleFinite"isDoubleFinite::Double->Int-------------------------------------------------------------------------- Coercion rules------------------------------------------------------------------------word2Double::Word->Doubleword2Double :: Word -> Doubleword2Double(W#Word#w)=Double# -> DoubleD#(Word# -> Double#word2Double#Word#w)word2Float::Word->Floatword2Float :: Word -> Floatword2Float(W#Word#w)=Float# -> FloatF#(Word# -> Float#word2Float#Word#w){-# RULES"fromIntegral/Int->Float"fromIntegral=int2Float"fromIntegral/Int->Double"fromIntegral=int2Double"fromIntegral/Word->Float"fromIntegral=word2Float"fromIntegral/Word->Double"fromIntegral=word2Double"realToFrac/Float->Float"realToFrac=id::Float->Float"realToFrac/Float->Double"realToFrac=float2Double"realToFrac/Double->Float"realToFrac=double2Float"realToFrac/Double->Double"realToFrac=id::Double->Double"realToFrac/Int->Double"realToFrac=int2Double-- See Note [realToFrac int-to-float]"realToFrac/Int->Float"realToFrac=int2Float-- ..ditto#-}{-Note [realToFrac int-to-float]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Don found that the RULES for realToFrac/Int->Double and simliarlyFloat made a huge difference to some stream-fusion programs. Here'san example import Data.Array.Vector n = 40000000 main = do let c = replicateU n (2::Double) a = mapU realToFrac (enumFromToU 0 (n-1) ) :: UArr Double print (sumU (zipWithU (*) c a))Without the RULE we get this loop body: case $wtoRational sc_sY4 of ww_aM7 { (# ww1_aM9, ww2_aMa #) -> case $wfromRat ww1_aM9 ww2_aMa of tpl_X1P { D# ipv_sW3 -> Main.$s$wfold (+# sc_sY4 1) (+# wild_X1i 1) (+## sc2_sY6 (*## 2.0 ipv_sW3))And with the rule: Main.$s$wfold (+# sc_sXT 1) (+# wild_X1h 1) (+## sc2_sXV (*## 2.0 (int2Double# sc_sXT)))The running time of the program goes from 120 seconds to 0.198 secondswith the native backend, and 0.143 seconds with the C backend.A few more details in #2251, and the patch message"Add RULES for realToFrac from Int".-}-- UtilsshowSignedFloat::(RealFloata)=>(a->ShowS)-- ^ a function that can show unsigned values->Int-- ^ the precedence of the enclosing context->a-- ^ the value to show->ShowSshowSignedFloat :: (a -> ShowS) -> Int -> a -> ShowSshowSignedFloata -> ShowSshowPosIntpax|axa -> a -> Boolforall a. Ord a => a -> a -> Bool<a0Bool -> Bool -> Bool||a -> Boolforall a. RealFloat a => a -> BoolisNegativeZeroax=Bool -> ShowS -> ShowSshowParen(IntpInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int6)(Char -> ShowSshowCharChar'-'ShowS -> ShowS -> ShowSforall b c a. (b -> c) -> (a -> b) -> a -> c.a -> ShowSshowPos(-ax))|Boolotherwise=a -> ShowSshowPosax{-We need to prevent over/underflow of the exponent in encodeFloat whencalled from scaleFloat, hence we clamp the scaling parameter.We must have a large enough range to cover the maximum difference ofexponents returned by decodeFloat.-}clamp::Int->Int->Intclamp :: Int -> Int -> IntclampIntbdIntk=Int -> Int -> Intforall a. Ord a => a -> a -> amax(-Intbd)(Int -> Int -> Intforall a. Ord a => a -> a -> aminIntbdIntk){-Note [Casting from integral to floating point types]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~To implement something like `reinterpret_cast` from C++ to go from afloating-point type to an integral type one might naively think that thefollowing should work: cast :: Float -> Word32 cast (F# f#) = W32# (unsafeCoerce# f#)Unfortunately that is not the case, because all the `unsafeCoerce#` does is tellthe compiler that the types have changed. When one does the above cast andtries to operate on the resulting `Word32` the code generator will generate codethat performs an integer/word operation on a floating-point register, whichresults in a compile error.The correct way of implementing `reinterpret_cast` to implement a primpop, butthat requires a unique implementation for all supported archetectures. The nextbest solution is to write the value from the source register to memory and thenread it from memory into the destination register and the best way to do thatis using CMM.-}-- | @'castWord32ToFloat' w@ does a bit-for-bit copy from an integral value-- to a floating-point value.---- @since 4.10.0.0{-# INLINEcastWord32ToFloat#-}castWord32ToFloat::Word32->FloatcastWord32ToFloat :: Word32 -> FloatcastWord32ToFloat(W32#Word#w#)=Float# -> FloatF#(Word# -> Float#stgWord32ToFloatWord#w#)foreignimportprim"stg_word32ToFloatzh"stgWord32ToFloat::Word#->Float#-- | @'castFloatToWord32' f@ does a bit-for-bit copy from a floating-point value-- to an integral value.---- @since 4.10.0.0{-# INLINEcastFloatToWord32#-}castFloatToWord32::Float->Word32castFloatToWord32 :: Float -> Word32castFloatToWord32(F#Float#f#)=Word# -> Word32W32#(Float# -> Word#stgFloatToWord32Float#f#)foreignimportprim"stg_floatToWord32zh"stgFloatToWord32::Float#->Word#-- | @'castWord64ToDouble' w@ does a bit-for-bit copy from an integral value-- to a floating-point value.---- @since 4.10.0.0{-# INLINEcastWord64ToDouble#-}castWord64ToDouble::Word64->DoublecastWord64ToDouble :: Word64 -> DoublecastWord64ToDouble(W64#Word#w)=Double# -> DoubleD#(Word# -> Double#stgWord64ToDoubleWord#w)foreignimportprim"stg_word64ToDoublezh"#if WORD_SIZE_IN_BITS == 64stgWord64ToDouble::Word#->Double##elsestgWord64ToDouble::Word64#->Double##endif-- | @'castFloatToWord64' f@ does a bit-for-bit copy from a floating-point value-- to an integral value.---- @since 4.10.0.0{-# INLINEcastDoubleToWord64#-}castDoubleToWord64::Double->Word64castDoubleToWord64 :: Double -> Word64castDoubleToWord64(D#Double#d#)=Word# -> Word64W64#(Double# -> Word#stgDoubleToWord64Double#d#)foreignimportprim"stg_doubleToWord64zh"#if WORD_SIZE_IN_BITS == 64stgDoubleToWord64::Double#->Word##elsestgDoubleToWord64::Double#->Word64##endif
[8]ページ先頭