{-# OPTIONS -fno-implicit-prelude #-}moduleAlgebra.RealFieldwhereimportqualifiedAlgebra.FieldasFieldimportqualifiedAlgebra.PrincipalIdealDomainasPIDimportqualifiedAlgebra.RealasRealimportqualifiedAlgebra.RingasRingimportqualifiedAlgebra.ToRationalasToRationalimportqualifiedAlgebra.ToIntegerasToIntegerimportAlgebra.Field((/))importAlgebra.RealIntegral(quotRem,)importAlgebra.IntegralDomain(divMod,even,)importAlgebra.Ring((*),fromInteger,)importAlgebra.Additive((+),(-),negate,)importAlgebra.ZeroTestable(isZero,)importAlgebra.ToInteger(fromIntegral,)importqualifiedNumber.RatioasRatioimportNumber.Ratio(T((:%)),Rational)importqualifiedGHC.FloatasGHCimportPrelude(Int,Float,Double)importqualifiedPreludeasPimportPreludeBase{- |
Minimal complete definition:
'splitFraction' or 'floor'
There are probably more laws, but some laws are
> (fromInteger.fst.splitFraction) a + (snd.splitFraction) a === a
> ceiling (toRational x) === ceiling x :: Integer
> truncate (toRational x) === truncate x :: Integer
> floor (toRational x) === floor x :: Integer
If there wouldn't be @Real.C a@ and @ToInteger.C b@ constraints,
we could also use this class for splitting ratios of polynomials.
As an aside, let me note the similarities
between @splitFraction x@ and @x divMod 1@ (if that were defined).
In particular, it might make sense to unify the rounding modes somehow.
IEEEFloat-specific calls are removed here (cf. 'Prelude.RealFloat')
so probably nobody will actually use this default definition.
Henning:
New function 'fraction' doesn't return the integer part of the number.
This also removes a type ambiguity if the integer part is not needed.
The new methods 'fraction' and 'splitFraction'
differ from 'Prelude.properFraction' semantics.
They always round to 'floor'.
This means that the fraction is always non-negative and
is always smaller than 1.
This is more useful in practice and
can be generalised to more than real numbers.
Since every 'Number.Ratio.T' denominator type supports 'Algebra.IntegralDomain.divMod',
every 'Number.Ratio.T' can provide 'fraction' and 'splitFraction',
e.g. fractions of polynomials.
However the ''integral'' part would not be of type class 'ToInteger.C'.
Can there be a separate class for
'fraction', 'splitFraction', 'floor' and 'ceiling'
since they do not need reals and their ordering?
-}class(Real.Ca,Field.Ca)=>CawheresplitFraction::(ToInteger.Cb)=>a->(b,a)fraction::a->aceiling,floor::(ToInteger.Cb)=>a->btruncate,round::(ToInteger.Cb)=>a->bsplitFractionx=(floorx,fractionx)fractionx=x-fromInteger(floorx)floorx=fromInteger(fst(splitFractionx))ceilingx=-floor(-x)-- truncate x = signum x * floor (abs x)truncatex=ifx>=0thenfloorxelseceilingxroundx=let(n,r)=splitFractionxincasecomparer(1/2)ofLT->nEQ->ifevennthennelsen+1GT->n+1instance(ToInteger.Ca,PID.Ca)=>C(Ratio.Ta)wheresplitFraction(x:%y)=(fromIntegralq,r:%y)where(q,r)=divModxyinstanceCFloatwheresplitFraction=preludeSplitFractionfraction=fractionTrunc(GHC.int2Float.GHC.float2Int)-- preludeFractionfloor=fromInteger.P.floorceiling=fromInteger.P.ceilinground=fromInteger.P.roundtruncate=fromInteger.P.truncateinstanceCDoublewheresplitFraction=preludeSplitFractionfraction=fractionTrunc(GHC.int2Double.GHC.double2Int)floor=fromInteger.P.floorceiling=fromInteger.P.ceilinground=fromInteger.P.roundtruncate=fromInteger.P.truncatepreludeSplitFraction::(P.RealFraca,Ring.Ca,ToInteger.Cb)=>a->(b,a)preludeSplitFractionx=let(n,f)=P.properFractionx-- if x>=0 || f==0iniff>=0then(fromIntegern,f)else(fromIntegern-1,f+1)preludeFraction::(P.RealFraca,Ring.Ca)=>a->apreludeFractionx=letsecond::(Int,a)->asecond=sndinfixFraction(second(P.properFractionx))fractionTrunc::(Ring.Ca,Orda)=>(a->a)->a->afractionTrunctruncx=fixFraction(x-truncx)fixFraction::(Ring.Ca,Orda)=>a->afixFractiony=ify>=0thenyelsey+1{- | TODO: Should be moved to a continued fraction module. -}approxRational::(ToRational.Ca,Ca)=>a->a->RationalapproxRationalrateps=simplest(rat-eps)(rat+eps)wheresimplestxy|y<x=simplestyx|x==y=xr|x>0=simplest'ndn'd'|y<0=-simplest'(-n')d'(-n)d|otherwise=0:%1wherexr@(n:%d)=ToRational.toRationalx(n':%d')=ToRational.toRationalysimplest'ndn'd'-- assumes 0 < n%d < n'%d'|isZeror=q:%1|q/=q'=(q+1):%1|otherwise=(q*n''+d''):%n''where(q,r)=quotRemnd(q',r')=quotRemn'd'(n'':%d'')=simplest'd'r'dr