Skip to content

Commit

Permalink
fix formatting of negative decimals (fixes #400)
Browse files Browse the repository at this point in the history
  • Loading branch information
byorgey committed Oct 31, 2024
1 parent 250077c commit aa951a4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
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.9
0
-0.1
-0.15
-0.0003
-1.0003
-1.1
-0.999
-1.3
-104.55
-104.0
-1.0
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

0 comments on commit aa951a4

Please sign in to comment.