Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rational.toDecimalString doesn't return the correct sign
Since -1 // 3 = 0 the sign is lost. I needed to capture the sign
and display it separately.
  • Loading branch information
dwayne committed Jul 11, 2019
1 parent 375b155 commit 87c4978
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Rational.elm
Expand Up @@ -94,13 +94,22 @@ toDecimalString (Rational n d) =
String.fromInt n
else
let
sign =
if n < 0 then
"-"
else
""

m =
abs n

quotient =
n // d
m // d

remainder =
modBy d (abs n)
modBy d m
in
String.fromInt quotient ++ "." ++ (decimalRep remainder d)
sign ++ String.fromInt quotient ++ "." ++ (decimalRep remainder d)


decimalRep : Int -> Int -> String
Expand Down
5 changes: 5 additions & 0 deletions tests/Test/Rational.elm
Expand Up @@ -117,6 +117,11 @@ terminatingDecimalSuite =
Rational.new 1 2
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.5")
, test "-1/2" <|
\_ ->
Rational.new -1 2
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "-0.5")
, test "5/2" <|
\_ ->
Rational.new 5 2
Expand Down

0 comments on commit 87c4978

Please sign in to comment.