Skip to content

Commit

Permalink
fixed precison printing of zero and neg zero for FStandard
Browse files Browse the repository at this point in the history
  • Loading branch information
BebeSparkelSparkel committed Jan 15, 2024
1 parent 3612878 commit 072ade0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Data/ByteString/Builder/RealFloat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE NamedFieldPuns #-}
-- |
-- Module : Data.ByteString.Builder.RealFloat
-- Copyright : (c) Lawrence Wu 2021
Expand Down Expand Up @@ -81,6 +82,7 @@ module Data.ByteString.Builder.RealFloat

import Data.ByteString.Builder.Internal (Builder)
import qualified Data.ByteString.Builder.RealFloat.Internal as R
import Data.ByteString.Builder.RealFloat.Internal (positiveZero, negativeZero)
import qualified Data.ByteString.Builder.RealFloat.F2S as RF
import qualified Data.ByteString.Builder.RealFloat.D2S as RD
import qualified Data.ByteString.Builder.Prim as BP
Expand Down Expand Up @@ -132,7 +134,12 @@ fGeneric eE = FGeneric (R.asciiRaw $ ord eE)
--
-- @since 0.11.2.0
standard :: Int -> FloatFormat
standard n = FStandard (Just n) standardSpecialStrings
standard n = FStandard (Just n) standardSpecialStrings {positiveZero, negativeZero}
where
positiveZero = if n == 0
then "0"
else "0." <> replicate n '0'
negativeZero = "-" <> positiveZero

-- | Standard notation with the \'default precision\' (decimal places matching `show`)
--
Expand Down
3 changes: 2 additions & 1 deletion tests/builder/Data/ByteString/Builder/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,8 @@ testsFloating = testGroup "RealFloat"
singleMatches (formatDouble (standard 3)) (flip (showFFloat (Just 3)) []) ( 0.0096 , "0.010" )
singleMatches (formatDouble (standard 5)) (flip (showFFloat (Just 5)) []) ( 12.345 , "12.34500" )
singleMatches (formatDouble (standard 3)) (flip (showFFloat (Just 3)) []) ( 0.0 , "0.000" )
, testProperty "standard N" \(NonNegative p, d :: Double) -> (LC.unpack . toLazyByteString) (formatDouble (standard p) d) === showFFloat (Just p) d ""
, testProperty "standard N" \(NonNegative p, d :: Double) -> (LC.unpack . toLazyByteString)
(formatDouble (standard p) d) === showFFloat (Just p) d ""
]
, testMatches "d2sLooksLikePowerOf5" doubleDec show
[ ( (coerceWord64ToDouble 0x4830F0CF064DD592) , "5.764607523034235e39" )
Expand Down

0 comments on commit 072ade0

Please sign in to comment.