From a10ccb123f4c948808eeb4b9f1f9ec4cdf9962a7 Mon Sep 17 00:00:00 2001 From: Priynsh Date: Wed, 6 Nov 2024 19:06:51 +0000 Subject: [PATCH 1/3] Add two test files --- base/rational.jl | 23 ++++++++++++++++++++--- test/rational.jl | 7 ++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index b4e450fd73abc..c488aca4832c0 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -98,9 +98,26 @@ function //(x::Rational, y::Rational) end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) -//(x::Number, y::Complex) = x*conj(y)//abs2(y) - - +function //(x::Number, y::Complex) + if((x//abs2(y))==0//1 || (x//abs2(y))==1//0) + return (x//abs2(y)) + end + return (x//abs2(y))*conj(y) +end +function //(x::Number, y::Complex{<:Integer}) + if isinf(real(y)) || isinf(imag(y)) + return 0//1 + end + real_y = real(y) + imag_y = imag(y) + denom = Int32(abs(real_y))^2 + Int32(abs(imag_y))^2 + if denom == 0//1 + return 1//0 + end + real_part = x * real_y // denom + imag_part = -x * imag_y // denom + return real_part + imag_part * im +end //(X::AbstractArray, y::Number) = X .// y function show(io::IO, x::Rational) diff --git a/test/rational.jl b/test/rational.jl index 90b5414a6fe89..a0f69746c82c9 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -18,7 +18,12 @@ using Test @test -1//0 == -1//0 @test -7//0 == -1//0 @test (-1//2) // (-2//5) == 5//4 - + @testset "Complex//Number Overflow" begin + for y ∈ (1 // 0, -1 // 0) + @test (7 // complex(y)) == (7 // y) + end + @test Int8(8) // Int8(100)im == 0//1 - 2//25*im + end @test_throws OverflowError -(0x01//0x0f) @test_throws OverflowError -(typemin(Int)//1) @test_throws OverflowError (typemax(Int)//3) + 1 From 87e2284db50f169ff2388443a86841bd75635766 Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:36:36 +0530 Subject: [PATCH 2/3] Update rational.jl applied scaling while calculating absolute values in order to prevent overflow at any step --- base/rational.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/base/rational.jl b/base/rational.jl index c488aca4832c0..80e1dfa1f131a 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -110,12 +110,20 @@ function //(x::Number, y::Complex{<:Integer}) end real_y = real(y) imag_y = imag(y) - denom = Int32(abs(real_y))^2 + Int32(abs(imag_y))^2 + m=max(abs(real_y),abs(imag_y)) + if(m==0) + return 1//0 + end + scaled_a = real_y / m + scaled_b = imag_y / m + denom = Rational(m * (scaled_a^2 + scaled_b^2)^0.5) if denom == 0//1 return 1//0 end real_part = x * real_y // denom + real_part = real_part // denom imag_part = -x * imag_y // denom + imag_part = imag_part // denom return real_part + imag_part * im end //(X::AbstractArray, y::Number) = X .// y From 387918681bcf8f5261c7432d726d7d5e7bc765c7 Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:42:53 +0530 Subject: [PATCH 3/3] Update rational.jl fixed the overflow issue in my solution, implemented iszero and isinf wherever possible for lighter implementation --- base/rational.jl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 80e1dfa1f131a..01943eecfb5ef 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -99,7 +99,7 @@ end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) function //(x::Number, y::Complex) - if((x//abs2(y))==0//1 || (x//abs2(y))==1//0) + if(iszero((x//abs2(y))) || isinf((x//abs2(y))==1//0)) return (x//abs2(y)) end return (x//abs2(y))*conj(y) @@ -114,17 +114,16 @@ function //(x::Number, y::Complex{<:Integer}) if(m==0) return 1//0 end - scaled_a = real_y / m - scaled_b = imag_y / m - denom = Rational(m * (scaled_a^2 + scaled_b^2)^0.5) - if denom == 0//1 + scaled_a = real_y // m + scaled_b = imag_y // m + denom = (scaled_a^2 + scaled_b^2) + if iszero(denom) return 1//0 end - real_part = x * real_y // denom - real_part = real_part // denom - imag_part = -x * imag_y // denom - imag_part = imag_part // denom - return real_part + imag_part * im + real_part = (x * (((real_y//denom)//m)//m)) + imag_part = (-x * (((imag_y// denom)//m)//m)) + ans = (real_part + imag_part * im) + return ans end //(X::AbstractArray, y::Number) = X .// y