Skip to content

Commit

Permalink
cleaned up function formatFloating
Browse files Browse the repository at this point in the history
  • Loading branch information
BebeSparkelSparkel committed Jan 12, 2024
1 parent 6fa0510 commit 0321b45
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Data/ByteString/Builder/RealFloat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE BlockArguments #-}
-- |
-- Module : Data.ByteString.Builder.RealFloat
-- Copyright : (c) Lawrence Wu 2021
Expand Down Expand Up @@ -246,18 +247,19 @@ formatFloating :: forall a mw ew ei.
, R.FromInt ei
) => FloatFormat -> a -> Builder
formatFloating fmt f = case fmt of
FGeneric eE prec (minExpo,maxExpo) ss -> flip fromMaybe (R.toCharsNonNumbersAndZero ss f) $
FGeneric eE prec (minExpo,maxExpo) ss -> specialsOr ss
if e' >= minExpo && e' <= maxExpo
then printSign f `mappend` showStandard (toWord64 m) e' prec
then std prec
else sci eE
FScientific eE ss -> fromMaybe (sci eE) (R.toCharsNonNumbersAndZero ss f)
FStandard prec ss -> flip fromMaybe (R.toCharsNonNumbersAndZero ss f) $
printSign f `mappend` showStandard (toWord64 m) e' prec
FScientific eE ss -> specialsOr ss $ sci eE
FStandard prec ss -> specialsOr ss $ std prec
where
sci eE = BP.primBounded (R.toCharsScientific @a Proxy eE sign m e) ()
std prec = printSign f `mappend` showStandard (toWord64 m) e' prec
e' = R.toInt e + R.decimalLength m
R.FloatingDecimal m e = toD @a mantissa expo
(sign, mantissa, expo) = R.breakdown f
specialsOr ss = flip fromMaybe $ R.toCharsNonNumbersAndZero ss f

class ToWord64 a where toWord64 :: a -> Word64
instance ToWord64 Word32 where toWord64 = R.word32ToWord64
Expand Down

0 comments on commit 0321b45

Please sign in to comment.