Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting of negative decimals #408

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
15 changes: 9 additions & 6 deletions src/Disco/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/syntax-decimals/expected
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ T
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
-0.1
-1.2
-0.09
0
-0.1
-0.15
-0.0003
-1.0003
-1.1
-0.999
-1.3
-104.55
-104
-1
14 changes: 14 additions & 0 deletions test/syntax-decimals/input
Original file line number Diff line number Diff line change
Expand Up @@ -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