-- ----------------------------------------------------------------------------- |-- Module : Text.Show.ByteString.Float-- Copyright : (c) 2008 Dan Doel-- Maintainer : Dan Doel-- Stability : Experimental-- Portability : Non-portable (magic hash)---- Putting floating point values.---- The code in this module is heavily based on GHC.FloatmoduleText.Show.ByteString.FloatwhereimportGHC.FloatimportControl.MonadimportData.BinaryimportText.Show.ByteString.UtilimportText.Show.ByteString.Int-- | Show a signed RealFloat value using decimal notation when the-- absolute value lies between 0.1 and 9,999,999, and scientific-- notation otherwise. The optional integer can be used to specify-- precision.showpGFloat::RealFloata=>MaybeInt->a->PutshowpGFloat=putFormattedFloatFFGeneric-- | Show a signed RealFloat value using decimal notation. The optional-- integer can be used to specify precision.showpFFloat::RealFloata=>MaybeInt->a->PutshowpFFloat=putFormattedFloatFFFixed-- | Show a signed RealFloat value using scientific (exponential) notation.-- The optional integer can be used to specify precision.showpEFloat::RealFloata=>MaybeInt->a->PutshowpEFloat=putFormattedFloatFFExponentputFormattedFloat::RealFloata=>FFFormat->MaybeInt->a->PutputFormattedFloatfmtdecsf|isNaNf=putAscii'N'>>putAscii'a'>>putAscii'N'|isInfinitef=putAsciiStr(iff<0then"-Infinity"else"Infinity")|f<0||isNegativeZerof=putAscii'-'>>gofmt(floatToDigits(toIntegerbase)(-f))|otherwise=gofmt(floatToDigits(toIntegerbase)f)wherebase=10goFFGenericp@(_,e)|e<0||e>7=goFFExponentp|otherwise=goFFFixedpgoFFExponent(is,e)=casedecsofNothing->caseisof[]->error"putFormattedFloat"[0]->putAsciiStr"0.0e0"[d]->unsafePutDigitd>>putAsciiStr".0e">>showpInt(e-1)(d:ds)->unsafePutDigitd>>putAscii'.'>>mapM_unsafePutDigitds>>putAscii'e'>>showpInt(e-1)Justdec->letdec'=maxdec1incaseisof[0]->putAscii'0'>>putAscii'.'>>replicateM_dec'(putAscii'0')>>putAscii'e'>>putAscii'0'_->let(ei,is')=roundTobase(dec'+1)is(d:ds)=ifei>0theninitis'elseis'inunsafePutDigitd>>putAscii'.'>>mapM_unsafePutDigitds>>putAscii'e'>>showpInt(e-1+ei)goFFFixed(is,e)=casedecsofNothing|e<=0->putAscii'0'>>putAscii'.'>>replicateM_(-e)(putAscii'0')>>mapM_unsafePutDigitis|otherwise->letg0rs=putAscii'.'>>mk0rsgn[]=putAscii'0'>>g(n-1)[]gn(r:rs)=unsafePutDigitr>>g(n-1)rsingeisJustdec->letdec'=maxdec0inife>=0thenlet(ei,is')=roundTobase(dec'+e)is(ls,rs)=splitAt(e+ei)is'inmk0ls>>when(not$nullrs)(putAscii'.'>>mapM_unsafePutDigitrs)elselet(ei,is')=roundTobasedec'(replicate(-e)0++is)d:ds=ifei>0thenis'else0:is'inunsafePutDigitd>>when(not$nullds)(putAscii'.'>>mapM_unsafePutDigitds)mk0[]=putAscii'0'mk0rs=mapM_unsafePutDigitrs