From e8e96242141db6335e81ba58e5c9035c0f62e656 Mon Sep 17 00:00:00 2001 From: Brent Yorgey Date: Thu, 31 Oct 2024 09:50:13 -0500 Subject: [PATCH] fix formatting of negative decimals (fixes #400) --- src/Disco/Pretty.hs | 15 +++++++++------ test/syntax-decimals/expected | 14 ++++++++++++++ test/syntax-decimals/input | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/Disco/Pretty.hs b/src/Disco/Pretty.hs index 22735bcb..f580428f 100644 --- a/src/Disco/Pretty.hs +++ b/src/Disco/Pretty.hs @@ -146,17 +146,20 @@ prettyRational r -- the format @nnn.prefix[rep]...@, with any repeating digits enclosed -- in square brackets. prettyDecimal :: Rational -> String -prettyDecimal r = printedDecimal +prettyDecimal r = wholePart ++ "." ++ decimalPart where - (n, d) = properFraction r :: (Integer, Rational) + (n', d') = properFraction r :: (Integer, Rational) + d = abs d' + n = abs n' (expan, len) = digitalExpansion 10 (numerator d) (denominator d) - printedDecimal + wholePart = (if d' < 0 then "-" else "") ++ show n + decimalPart | length first102 > 101 || length first102 == 101 && last first102 /= 0 = - show n ++ "." ++ concatMap show (take 100 expan) ++ "..." + concatMap show (take 100 expan) ++ "..." | rep == [0] = - show n ++ "." ++ (if null pre then "0" else concatMap show pre) + if null pre then "0" else concatMap show pre | otherwise = - show n ++ "." ++ concatMap show pre ++ "[" ++ concatMap show rep ++ "]" + concatMap show pre ++ "[" ++ concatMap show rep ++ "]" where (pre, rep) = splitAt len expan first102 = take 102 expan diff --git a/test/syntax-decimals/expected b/test/syntax-decimals/expected index a1480fd6..1cac0907 100644 --- a/test/syntax-decimals/expected +++ b/test/syntax-decimals/expected @@ -22,3 +22,17 @@ T 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000... +-0.1 +-1.2 +-0.9 +0 +-0.1 +-0.15 +-0.0003 +-1.0003 +-1.1 +-0.999 +-1.3 +-104.55 +-104.0 +-1.0 diff --git a/test/syntax-decimals/input b/test/syntax-decimals/input index 5f2a55e0..21bc828b 100644 --- a/test/syntax-decimals/input +++ b/test/syntax-decimals/input @@ -22,3 +22,17 @@ (1.0/10^99) (1.0/10^100) (1.0/10^101) +0.1 - 0.2 +0.2 * -6 +0.3 * (-0.3) +-0.0 +0.0 - 0.1 +0.0 - 0.15 +0.0 - 0.0003 +0.0 - 1.0003 +0.0 - 1.1 +0.0 - 0.999 +0.0 - 1.3 +0.0 - 104.55 +0.0 - 104.0 +0.0 - 1.0