Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* New TH splices: `Data.ByteString.literalFromOctetString` and `Data.ByteString.literalFromHex`
* These validate input strings prior to generating corresponding
compile-time literal ByteStrings.
* Bug fixes:
* `formatFloat` and `formatDouble` with `standard n` now show zero with `n` digits after the decimal, like `showFFloat`, instead of always as `0.0` ([#729](https://github.com/haskell/bytestring/pull/729))

<!--
* Bug fixes:
* Deprecations:
Expand Down
16 changes: 14 additions & 2 deletions Data/ByteString/Builder/RealFloat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ formatFloat (MkFloatFormat fmt prec) = \f ->
FScientific -> RF.f2s f
FStandard ->
case specialStr f of
Just b -> b
Just b
| f == 0, Just p <- prec -> showStandardZero f p
| otherwise -> b
Nothing -> sign f `mappend` showStandard (R.word32ToWord64 m) e' prec

-- TODO: support precision argument for FGeneric and FScientific
Expand Down Expand Up @@ -218,7 +220,9 @@ formatDouble (MkFloatFormat fmt prec) = \f ->
FScientific -> RD.d2s f
FStandard ->
case specialStr f of
Just b -> b
Just b
| f == 0, Just p <- prec -> showStandardZero f p
| otherwise -> b
Nothing -> sign f `mappend` showStandard m e' prec

-- | Char7 encode a 'Char'.
Expand All @@ -245,6 +249,14 @@ specialStr f
| f == 0 = Just $ string7 "0.0"
| otherwise = Nothing

-- | Show zero in standard notation with @p@ digits after the decimal, like
-- `Numeric.showFFloat`: `specialStr` always gives one digit (#641).
showStandardZero :: RealFloat a => a -> Int -> Builder
showStandardZero f p =
(if isNegativeZero f then char7 '-' else mempty)
`mappend` char7 '0'
`mappend` (if p > 0 then char7 '.' `mappend` string7 (replicate p '0') else mempty)

-- | Returns a list of decimal digits in a Word64
digits :: Word64 -> [Int]
digits w = go [] w
Expand Down
6 changes: 6 additions & 0 deletions tests/builder/Data/ByteString/Builder/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,12 @@ testsFloating =
, singleMatches (formatDouble (standard 2)) (flip (showFFloat (Just 2)) []) ( 0.0050 , "0.00" )
, singleMatches (formatDouble (standard 2)) (flip (showFFloat (Just 2)) []) ( 0.0051 , "0.01" )
, singleMatches (formatDouble (standard 5)) (flip (showFFloat (Just 5)) []) ( 12.345 , "12.34500" )
, singleMatches (formatDouble (standard 0)) (flip (showFFloat (Just 0)) []) ( 0.0 , "0" )
, singleMatches (formatDouble (standard 2)) (flip (showFFloat (Just 2)) []) ( 0.0 , "0.00" )
, singleMatches (formatDouble (standard 0)) (flip (showFFloat (Just 0)) []) ( -0.0 , "-0" )
, singleMatches (formatDouble (standard 3)) (flip (showFFloat (Just 3)) []) ( -0.0 , "-0.000" )
, singleMatches (formatFloat (standard 0)) (flip (showFFloat (Just 0)) []) ( 0.0 , "0" )
, singleMatches (formatFloat (standard 2)) (flip (showFFloat (Just 2)) []) ( -0.0 , "-0.00" )
]
, testMatches "d2sLooksLikePowerOf5" doubleDec show
[ ( (coerceWord64ToDouble 0x4830F0CF064DD592) , "5.764607523034235e39" )
Expand Down