From 07193071138fbe81253c4e2a46fe1efda4ffe45f Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Mon, 27 Nov 2023 04:44:31 +0100 Subject: [PATCH] implement `abs` for `BigInt` and `Rational` Tests coming in linked PRs. --- src/implementations/BigInt.jl | 14 ++++++++++++++ src/implementations/Rational.jl | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/implementations/BigInt.jl b/src/implementations/BigInt.jl index 36aa8c45..e196c2ef 100644 --- a/src/implementations/BigInt.jl +++ b/src/implementations/BigInt.jl @@ -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 + +function operate!(::typeof(abs), n::BigInt) + n.size = abs(n.size) + return n +end + +function operate_to!(o::BigInt, ::typeof(abs), n::BigInt) + operate_to!(o, copy, n) + return operate!(abs, o) +end + # + promote_operation(::typeof(+), ::Type{BigInt}, ::Type{BigInt}) = BigInt diff --git a/src/implementations/Rational.jl b/src/implementations/Rational.jl index 2c293138..02acd5b0 100644 --- a/src/implementations/Rational.jl +++ b/src/implementations/Rational.jl @@ -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 + +function operate!(::typeof(abs), x::Rational) + operate!(abs, x.num) + return x +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 +end + # + and - function promote_operation(