Skip to content

Commit

Permalink
implement abs for BigInt and Rational
Browse files Browse the repository at this point in the history
Tests coming in linked PRs.
  • Loading branch information
nsajko committed Nov 27, 2023
1 parent 6e6a854 commit 0719307
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/implementations/BigInt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ promote_operation(::typeof(one), ::Type{BigInt}) = BigInt

operate!(::typeof(one), x::BigInt) = Base.GMP.MPZ.set_si!(x, 1)

# abs

promote_operation(::typeof(abs), ::Type{BigInt}) = BigInt

Check warning on line 30 in src/implementations/BigInt.jl

View check run for this annotation

Codecov / codecov/patch

src/implementations/BigInt.jl#L30

Added line #L30 was not covered by tests

function operate!(::typeof(abs), n::BigInt)
n.size = abs(n.size)
return n

Check warning on line 34 in src/implementations/BigInt.jl

View check run for this annotation

Codecov / codecov/patch

src/implementations/BigInt.jl#L32-L34

Added lines #L32 - L34 were not covered by tests
end

function operate_to!(o::BigInt, ::typeof(abs), n::BigInt)
operate_to!(o, copy, n)
return operate!(abs, o)

Check warning on line 39 in src/implementations/BigInt.jl

View check run for this annotation

Codecov / codecov/patch

src/implementations/BigInt.jl#L37-L39

Added lines #L37 - L39 were not covered by tests
end

# +

promote_operation(::typeof(+), ::Type{BigInt}, ::Type{BigInt}) = BigInt
Expand Down
15 changes: 15 additions & 0 deletions src/implementations/Rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ end

_buffered_simplify(buffer, x::Rational) = _buffered_divgcd(buffer, x.num, x.den)

# abs

promote_operation(::typeof(abs), ::Type{Q}) where {Q<:Rational} = Q

Check warning on line 56 in src/implementations/Rational.jl

View check run for this annotation

Codecov / codecov/patch

src/implementations/Rational.jl#L56

Added line #L56 was not covered by tests

function operate!(::typeof(abs), x::Rational)
operate!(abs, x.num)
return x

Check warning on line 60 in src/implementations/Rational.jl

View check run for this annotation

Codecov / codecov/patch

src/implementations/Rational.jl#L58-L60

Added lines #L58 - L60 were not covered by tests
end

function operate_to!(o::Q, ::typeof(abs), x::Q) where {Q<:Rational}
operate_to!(o.num, abs, x.num)
operate_to!(o.den, copy, x.den)
return o

Check warning on line 66 in src/implementations/Rational.jl

View check run for this annotation

Codecov / codecov/patch

src/implementations/Rational.jl#L63-L66

Added lines #L63 - L66 were not covered by tests
end

# + and -

function promote_operation(
Expand Down

0 comments on commit 0719307

Please sign in to comment.