Skip to content

Commit

Permalink
Merge d670ea9 into 450c43d
Browse files Browse the repository at this point in the history
  • Loading branch information
hurak committed Sep 11, 2019
2 parents 450c43d + d670ea9 commit dae56e7
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -51,14 +51,14 @@ Or can create Decimal objects from either strings or numbers using `decimal`:
julia> decimal("-2.5e6")
Decimal(1,25,5)

To convert back to a string or a float:
To convert back to a string or a number (float in this case):

julia> x = decimal("0.2");

julia> string(x)
"0.2"

julia> float(x)
julia> number(x)
0.2

It is also possible to call the Decimal constructor directly, by specifying the sign (`s`), coefficient (`c`), and exponent (`q`):
Expand Down
9 changes: 2 additions & 7 deletions src/arithmetic.jl
Expand Up @@ -30,13 +30,8 @@ end

# Inversion
function Base.inv(x::Decimal)
str = string(x)
if str[1] == '-'
str = str[2:end]
end
b = ('.' in str) ? length(split(str, '.')[1]) : 0
c = round(BigInt(10)^(-x.q + DIGITS) / x.c)
q = (x.q < 0) ? 1 - b - DIGITS : -b - DIGITS
c = round(BigInt(10)^(-x.q + DIGITS) / x.c) # the decimal point of 1/x.c is shifted by -x.q so that the integer part of the result is correct and then it is shifted further by DIGITS to also cover some digits from the fractional part.
q = -DIGITS # we only need to remember that there are these digits after the decimal point
normalize(Decimal(x.s, c, q))
end

Expand Down
1 change: 1 addition & 0 deletions test/test_arithmetic.jl
Expand Up @@ -46,6 +46,7 @@ end
@test inv(Decimal(0, 5, 1)) == Decimal(0, 2, -2)
@test inv(Decimal(1, 4, -1)) == Decimal(1, 25, -1)
@test inv(Decimal(1, 25, -1)) == Decimal(1, 4, -1)
@test inv(Decimal(0, 123, -1)) == Decimal(0, 813008130081300813, -19) # 1/12.3 ≈ 0.08
end

@testset "Division" begin
Expand Down

0 comments on commit dae56e7

Please sign in to comment.