From 795ffaf7f893e02788e7098fe0940bee0bbb5342 Mon Sep 17 00:00:00 2001 From: Laksh Agarwal <184763813+Lotion-3@users.noreply.github.com> Date: Tue, 24 Nov 2026 01:42:13 -0500 Subject: [PATCH] Leave out the period in formatRealFloat Exponent (Just 0) With no decimals, formatRealFloat Exponent showed one decimal anyway, so (Just 0) gave the same output as (Just 1). showEFloat leaves out the decimals and the period since base-4.12 (GHC#15115). This does the same, and removes the special cases for gh-231 from the tests. --- src/Data/Text/Lazy/Builder/RealFloat.hs | 9 +++++++++ tests/Tests/Properties/Builder.hs | 10 ++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Data/Text/Lazy/Builder/RealFloat.hs b/src/Data/Text/Lazy/Builder/RealFloat.hs index d1882570..5f0c08c8 100644 --- a/src/Data/Text/Lazy/Builder/RealFloat.hs +++ b/src/Data/Text/Lazy/Builder/RealFloat.hs @@ -81,6 +81,15 @@ formatRealFloat fmt decs x [d] -> singleton d <> ".0e" <> show_e' (d:ds') -> singleton d <> singleton '.' <> fromString ds' <> singleton 'e' <> show_e' [] -> error "formatRealFloat/doFmt/Exponent/Nothing: []" + Just dec | dec <= 0 -> + -- No decimals: leave out the period too, as showEFloat does (gh-231). + case is of + [0] -> "0e0" + _ -> + let (ei,is') = roundTo 1 is + in case map i2d (if ei > 0 then init is' else is') of + [] -> error "formatRealFloat/doFmt/Exponent/Just 0: []" + (d:_) -> singleton d <> singleton 'e' <> decimal (e-1+ei) Just dec -> let dec' = max dec 1 in case is of diff --git a/tests/Tests/Properties/Builder.hs b/tests/Tests/Properties/Builder.hs index ba4cfe06..f2965baf 100644 --- a/tests/Tests/Properties/Builder.hs +++ b/tests/Tests/Properties/Builder.hs @@ -81,7 +81,6 @@ tb_realfloat_float (a::Float) = tb_realfloat a tb_realfloat_double (a::Double) = tb_realfloat a showFloat :: (RealFloat a) => TB.FPFormat -> Maybe Int -> a -> ShowS -showFloat TB.Exponent (Just 0) = showEFloat (Just 1) -- see gh-231 showFloat TB.Exponent p = showEFloat p showFloat TB.Fixed p = showFFloat p showFloat TB.Generic p = showGFloat p @@ -93,10 +92,13 @@ tb_formatRealFloat a fmt prec = cond ==> TB.fromString (showFloat fmt p a "") where p = precision a prec cond = case (p,fmt) of -#if MIN_VERSION_base(4,12,0) - (Just 0, TB.Generic) -> False -- skipping due to gh-231 +#if !MIN_VERSION_base(4,12,0) + -- Before base-4.12, showEFloat showed one decimal for + -- Just 0 (GHC#15115, gh-231). + (Just 0, TB.Exponent) -> False + (Just 0, TB.Generic) -> False #endif - _ -> True + _ -> True tb_formatRealFloat_float (a::Float) = tb_formatRealFloat a tb_formatRealFloat_double (a::Double) = tb_formatRealFloat a