Movatterモバイル変換
[0]ホーム
#ifndef __NHC__#endifmoduleData.Fixed(div',mod',divMod',Fixed,HasResolution(..),showFixed,E0,Uni,E1,Deci,E2,Centi,E3,Milli,E6,Micro,E9,Nano,E12,Pico)whereimportPreludeimportData.CharimportData.List#ifndef __NHC__importData.TypeableimportData.Data#endif#ifndef __NHC__default()#endifdiv'::(Reala,Integralb)=>a->a->bdiv'nd=floor((toRationaln)/(toRationald))divMod'::(Reala,Integralb)=>a->a->(b,a)divMod'nd=(f,n(fromIntegralf)*d)wheref=div'ndmod'::(Reala)=>a->a->amod'nd=n(fromIntegerf)*dwheref=div'ndnewtypeFixeda=MkFixedInteger#ifndef __NHC__deriving(Eq,Ord,Typeable)#elsederiving(Eq,Ord)#endif#ifndef __NHC__tyFixed::DataTypetyFixed=mkDataType"Data.Fixed.Fixed"[conMkFixed]conMkFixed::ConstrconMkFixed=mkConstrtyFixed"MkFixed"[]Prefixinstance(Typeablea)=>Data(Fixeda)wheregfoldlkz(MkFixeda)=k(zMkFixed)agunfoldkz_=k(zMkFixed)dataTypeOf_=tyFixedtoConstr_=conMkFixed#endifclassHasResolutionawhereresolution::pa->IntegerwithType::(pa->fa)->fawithTypefoo=fooundefinedwithResolution::(HasResolutiona)=>(Integer->fa)->fawithResolutionfoo=withType(foo.resolution)instanceEnum(Fixeda)wheresucc(MkFixeda)=MkFixed(succa)pred(MkFixeda)=MkFixed(preda)toEnum=MkFixed.toEnumfromEnum(MkFixeda)=fromEnumaenumFrom(MkFixeda)=fmapMkFixed(enumFroma)enumFromThen(MkFixeda)(MkFixedb)=fmapMkFixed(enumFromThenab)enumFromTo(MkFixeda)(MkFixedb)=fmapMkFixed(enumFromToab)enumFromThenTo(MkFixeda)(MkFixedb)(MkFixedc)=fmapMkFixed(enumFromThenToabc)instance(HasResolutiona)=>Num(Fixeda)where(MkFixeda)+(MkFixedb)=MkFixed(a+b)(MkFixeda)(MkFixedb)=MkFixed(ab)fa@(MkFixeda)*(MkFixedb)=MkFixed(div(a*b)(resolutionfa))negate(MkFixeda)=MkFixed(negatea)abs(MkFixeda)=MkFixed(absa)signum(MkFixeda)=fromInteger(signuma)fromIntegeri=withResolution(\res->MkFixed(i*res))instance(HasResolutiona)=>Real(Fixeda)wheretoRationalfa@(MkFixeda)=(toRationala)/(toRational(resolutionfa))instance(HasResolutiona)=>Fractional(Fixeda)wherefa@(MkFixeda)/(MkFixedb)=MkFixed(div(a*(resolutionfa))b)recipfa@(MkFixeda)=MkFixed(div(res*res)a)whereres=resolutionfafromRationalr=withResolution(\res->MkFixed(floor(r*(toRationalres))))instance(HasResolutiona)=>RealFrac(Fixeda)whereproperFractiona=(i,a(fromIntegrali))wherei=truncateatruncatef=truncate(toRationalf)roundf=round(toRationalf)ceilingf=ceiling(toRationalf)floorf=floor(toRationalf)chopZeros::Integer->StringchopZeros0=""chopZerosa|moda10==0=chopZeros(diva10)chopZerosa=showashowIntegerZeros::Bool->Int->Integer->StringshowIntegerZerosTrue_0=""showIntegerZeroschopTrailingZerosdigitsa=replicate(digitslengths)'0'++s'wheres=showas'=ifchopTrailingZerosthenchopZerosaelseswithDot::String->StringwithDot""=""withDots='.':sshowFixed::(HasResolutiona)=>Bool->Fixeda->StringshowFixedchopTrailingZerosfa@(MkFixeda)|a<0="-"++(showFixedchopTrailingZeros(asTypeOf(MkFixed(negatea))fa))showFixedchopTrailingZerosfa@(MkFixeda)=(showi)++(withDot(showIntegerZeroschopTrailingZerosdigitsfracNum))whereres=resolutionfa(i,d)=divModaresdigits=ceiling(logBase10(fromIntegerres)::Double)maxnum=10^digitsfracNum=div(d*maxnum)resreadsFixed::(HasResolutiona)=>ReadS(Fixeda)readsFixed=readsSignedwherereadsSigned('-':xs)=[(negatex,rest)|(x,rest)<-readsUnsignedxs]readsSignedxs=readsUnsignedxsreadsUnsignedxs=casespanisDigitxsof([],_)->[](is,xs')->leti=fromInteger(readis)incasexs'of'.':xs''->casespanisDigitxs''of([],_)->[](js,xs''')->letj=fromInteger(readjs)l=genericLengthjs::Integerin[(i+(j/(10^l)),xs''')]_->[(i,xs')]instance(HasResolutiona)=>Show(Fixeda)whereshow=showFixedFalseinstance(HasResolutiona)=>Read(Fixeda)wherereadsPrec_=readsFixeddataE0=E0#ifndef __NHC__deriving(Typeable)#endifinstanceHasResolutionE0whereresolution_=1typeUni=FixedE0dataE1=E1#ifndef __NHC__deriving(Typeable)#endifinstanceHasResolutionE1whereresolution_=10typeDeci=FixedE1dataE2=E2#ifndef __NHC__deriving(Typeable)#endifinstanceHasResolutionE2whereresolution_=100typeCenti=FixedE2dataE3=E3#ifndef __NHC__deriving(Typeable)#endifinstanceHasResolutionE3whereresolution_=1000typeMilli=FixedE3dataE6=E6#ifndef __NHC__deriving(Typeable)#endifinstanceHasResolutionE6whereresolution_=1000000typeMicro=FixedE6dataE9=E9#ifndef __NHC__deriving(Typeable)#endifinstanceHasResolutionE9whereresolution_=1000000000typeNano=FixedE9dataE12=E12#ifndef __NHC__deriving(Typeable)#endifinstanceHasResolutionE12whereresolution_=1000000000000typePico=FixedE12
[8]ページ先頭