Skip to content

Commit 072ade0

Browse files
fixed precison printing of zero and neg zero for FStandard
1 parent 3612878 commit 072ade0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Data/ByteString/Builder/RealFloat.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{-# LANGUAGE ScopedTypeVariables #-}
88
{-# LANGUAGE TypeApplications #-}
99
{-# LANGUAGE BlockArguments #-}
10+
{-# LANGUAGE NamedFieldPuns #-}
1011
-- |
1112
-- Module : Data.ByteString.Builder.RealFloat
1213
-- Copyright : (c) Lawrence Wu 2021
@@ -81,6 +82,7 @@ module Data.ByteString.Builder.RealFloat
8182

8283
import Data.ByteString.Builder.Internal (Builder)
8384
import qualified Data.ByteString.Builder.RealFloat.Internal as R
85+
import Data.ByteString.Builder.RealFloat.Internal (positiveZero, negativeZero)
8486
import qualified Data.ByteString.Builder.RealFloat.F2S as RF
8587
import qualified Data.ByteString.Builder.RealFloat.D2S as RD
8688
import qualified Data.ByteString.Builder.Prim as BP
@@ -132,7 +134,12 @@ fGeneric eE = FGeneric (R.asciiRaw $ ord eE)
132134
--
133135
-- @since 0.11.2.0
134136
standard :: Int -> FloatFormat
135-
standard n = FStandard (Just n) standardSpecialStrings
137+
standard n = FStandard (Just n) standardSpecialStrings {positiveZero, negativeZero}
138+
where
139+
positiveZero = if n == 0
140+
then "0"
141+
else "0." <> replicate n '0'
142+
negativeZero = "-" <> positiveZero
136143

137144
-- | Standard notation with the \'default precision\' (decimal places matching `show`)
138145
--

tests/builder/Data/ByteString/Builder/Tests.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,8 @@ testsFloating = testGroup "RealFloat"
799799
singleMatches (formatDouble (standard 3)) (flip (showFFloat (Just 3)) []) ( 0.0096 , "0.010" )
800800
singleMatches (formatDouble (standard 5)) (flip (showFFloat (Just 5)) []) ( 12.345 , "12.34500" )
801801
singleMatches (formatDouble (standard 3)) (flip (showFFloat (Just 3)) []) ( 0.0 , "0.000" )
802-
, testProperty "standard N" \(NonNegative p, d :: Double) -> (LC.unpack . toLazyByteString) (formatDouble (standard p) d) === showFFloat (Just p) d ""
802+
, testProperty "standard N" \(NonNegative p, d :: Double) -> (LC.unpack . toLazyByteString)
803+
(formatDouble (standard p) d) === showFFloat (Just p) d ""
803804
]
804805
, testMatches "d2sLooksLikePowerOf5" doubleDec show
805806
[ ( (coerceWord64ToDouble 0x4830F0CF064DD592) , "5.764607523034235e39" )

0 commit comments

Comments
 (0)