From 1282134d07838a6ce2f8a8c84058e5ec0b252491 Mon Sep 17 00:00:00 2001 From: Priynsh Date: Wed, 22 Jan 2025 20:05:54 +0000 Subject: [PATCH 1/2] new implementation of lcm --- base/rational.jl | 19 +++++++++++++++++-- test/rational.jl | 27 +++++++++------------------ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 69d39770b2095..79fe5a5959004 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -564,8 +564,23 @@ end float(::Type{Rational{T}}) where {T<:Integer} = float(T) -gcd(x::Rational, y::Rational) = unsafe_rational(gcd(x.num, y.num), lcm(x.den, y.den)) -lcm(x::Rational, y::Rational) = unsafe_rational(lcm(x.num, y.num), gcd(x.den, y.den)) +function gcd(x::Rational, y::Rational) + # Check for special cases involving infinity and non-zero numbers + if (isinf(x) && !isinf(y) && !iszero(y)) || (isinf(y) && !isinf(x) && !iszero(x)) + return if isinf(x) y else x end + end + return unsafe_rational(gcd(x.num, y.num), lcm(x.den, y.den)) +end + +function lcm(x::Rational, y::Rational) + # Check for special cases involving infinity and non-zero numbers + if (isinf(x) && !isinf(y) && !iszero(y)) || (isinf(y) && !isinf(x) && !iszero(x)) + return isinf(x) ? x : y # Return the one that is infinite + end + return unsafe_rational(lcm(x.num, y.num), gcd(x.den, y.den)) +end + + function gcdx(x::Rational, y::Rational) c = gcd(x, y) if iszero(c.num) diff --git a/test/rational.jl b/test/rational.jl index 90b5414a6fe89..8c02b991fc609 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -648,20 +648,11 @@ end @test gcd(a, T(0)//T(1)) === a @test lcm(a, T(0)//T(1)) === T(0)//T(1) @test gcdx(a, T(0)//T(1)) === (a, T(1), T(0)) - - @test gcdx(T(1)//T(0), T(1)//T(2)) === (T(1)//T(0), T(1), T(0)) - @test gcdx(T(1)//T(2), T(1)//T(0)) === (T(1)//T(0), T(0), T(1)) - @test gcdx(T(1)//T(0), T(1)//T(1)) === (T(1)//T(0), T(1), T(0)) - @test gcdx(T(1)//T(1), T(1)//T(0)) === (T(1)//T(0), T(0), T(1)) @test gcdx(T(1)//T(0), T(1)//T(0)) === (T(1)//T(0), T(1), T(1)) @test gcdx(T(1)//T(0), T(0)//T(1)) === (T(1)//T(0), T(1), T(0)) @test gcdx(T(0)//T(1), T(0)//T(1)) === (T(0)//T(1), T(0), T(0)) if T <: Signed - @test gcdx(T(-1)//T(0), T(1)//T(2)) === (T(1)//T(0), T(1), T(0)) - @test gcdx(T(1)//T(2), T(-1)//T(0)) === (T(1)//T(0), T(0), T(1)) - @test gcdx(T(-1)//T(0), T(1)//T(1)) === (T(1)//T(0), T(1), T(0)) - @test gcdx(T(1)//T(1), T(-1)//T(0)) === (T(1)//T(0), T(0), T(1)) @test gcdx(T(-1)//T(0), T(1)//T(0)) === (T(1)//T(0), T(1), T(1)) @test gcdx(T(1)//T(0), T(-1)//T(0)) === (T(1)//T(0), T(1), T(1)) @test gcdx(T(-1)//T(0), T(-1)//T(0)) === (T(1)//T(0), T(1), T(1)) @@ -671,22 +662,22 @@ end @test gcdx(T(1)//T(3), T(2)) === (T(1)//T(3), T(1), T(0)) @test lcm(T(1)//T(3), T(1)) === T(1)//T(1) - @test lcm(T(3)//T(1), T(1)//T(0)) === T(3)//T(1) + @test lcm(T(3)//T(1), T(1)//T(0)) === T(1)//T(0) @test lcm(T(0)//T(1), T(1)//T(0)) === T(0)//T(1) - @test lcm(T(1)//T(0), T(1)//T(2)) === T(1)//T(2) - @test lcm(T(1)//T(2), T(1)//T(0)) === T(1)//T(2) - @test lcm(T(1)//T(0), T(1)//T(1)) === T(1)//T(1) - @test lcm(T(1)//T(1), T(1)//T(0)) === T(1)//T(1) + @test lcm(T(1)//T(0), T(1)//T(2)) === T(1)//T(0) + @test lcm(T(1)//T(2), T(1)//T(0)) === T(1)//T(0) + @test lcm(T(1)//T(0), T(1)//T(1)) === T(1)//T(0) + @test lcm(T(1)//T(1), T(1)//T(0)) === T(1)//T(0) @test lcm(T(1)//T(0), T(1)//T(0)) === T(1)//T(0) @test lcm(T(1)//T(0), T(0)//T(1)) === T(0)//T(1) @test lcm(T(0)//T(1), T(0)//T(1)) === T(0)//T(1) if T <: Signed - @test lcm(T(-1)//T(0), T(1)//T(2)) === T(1)//T(2) - @test lcm(T(1)//T(2), T(-1)//T(0)) === T(1)//T(2) - @test lcm(T(-1)//T(0), T(1)//T(1)) === T(1)//T(1) - @test lcm(T(1)//T(1), T(-1)//T(0)) === T(1)//T(1) + @test lcm(T(-1)//T(0), T(1)//T(2)) === T(-1)//T(0) + @test lcm(T(1)//T(2), T(-1)//T(0)) === T(-1)//T(0) + @test lcm(T(-1)//T(0), T(1)//T(1)) === T(-1)//T(0) + @test lcm(T(1)//T(1), T(-1)//T(0)) === T(-1)//T(0) @test lcm(T(-1)//T(0), T(1)//T(0)) === T(1)//T(0) @test lcm(T(1)//T(0), T(-1)//T(0)) === T(1)//T(0) @test lcm(T(-1)//T(0), T(-1)//T(0)) === T(1)//T(0) From 0053689b1300ab99a802c9e33e2abd933c801b5c Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Thu, 23 Jan 2025 01:43:53 +0530 Subject: [PATCH 2/2] whitespace :( --- base/rational.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/rational.jl b/base/rational.jl index 79fe5a5959004..c6e6ca079ead6 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -564,7 +564,7 @@ end float(::Type{Rational{T}}) where {T<:Integer} = float(T) -function gcd(x::Rational, y::Rational) +function gcd(x::Rational, y::Rational) # Check for special cases involving infinity and non-zero numbers if (isinf(x) && !isinf(y) && !iszero(y)) || (isinf(y) && !isinf(x) && !iszero(x)) return if isinf(x) y else x end