From a10ccb123f4c948808eeb4b9f1f9ec4cdf9962a7 Mon Sep 17 00:00:00 2001 From: Priynsh Date: Wed, 6 Nov 2024 19:06:51 +0000 Subject: [PATCH 01/13] 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 02/13] 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 03/13] 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 From 915a5afc37b25e56f606c0306fb72601ee06303e Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:52:49 +0530 Subject: [PATCH 04/13] Update rational.jl added tests --- test/rational.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/rational.jl b/test/rational.jl index a0f69746c82c9..4e4e73828a734 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -19,6 +19,17 @@ using Test @test -7//0 == -1//0 @test (-1//2) // (-2//5) == 5//4 @testset "Complex//Number Overflow" begin + @test (true//true) // complex(false, true) === 0//1 - 1//1*im + @test (false//true) // complex(false, true) === 0//1 + 0//1*im + @test (false//true) // complex(true, false) === 0//1 + 0//1*im + @test (false//true) // complex(true, true) === 0//1 + 0//1*im + @test (true//true) // complex(true, true) === 1//2 - 1//2*im + @test (false//true) // complex(true//true, true//true) === 0//1 + 0//1*im + @test (true//true) // complex(true//true, true//true) === 1//2 - 1//2*im + @test (false//true) // complex(true//false, false//true) === 0//1 + 0//1*im + @test (true//true) // complex(true//true, true//false) === 0//1 + 0//1*im + @test_throws DivideError (0//1) // complex(0, 0) + @test_throws DivideError (1//1) // complex(0, 0) for y ∈ (1 // 0, -1 // 0) @test (7 // complex(y)) == (7 // y) end From d9cea910721716760d0c3e7c80e8d4f7f14eef9d Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:53:21 +0530 Subject: [PATCH 05/13] Update rational.jl --- base/rational.jl | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index b99acb13bba6f..f9b4b4bff3d44 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -49,13 +49,6 @@ Rational(n::T, d::T) where {T<:Integer} = Rational{T}(n, d) Rational(n::Integer, d::Integer) = Rational(promote(n, d)...) Rational(n::Integer) = unsafe_rational(n, one(n)) -""" - divgcd(x::Integer, y::Integer) - -Returns `(x÷gcd(x,y), y÷gcd(x,y))`. - -See also [`div`](@ref), [`gcd`](@ref). -""" function divgcd(x::TX, y::TY)::Tuple{TX, TY} where {TX<:Integer, TY<:Integer} g = gcd(uabs(x), uabs(y)) div(x,g), div(y,g) @@ -106,32 +99,31 @@ end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) function //(x::Number, y::Complex) - if(iszero((x//abs2(y))) || isinf((x//abs2(y))==1//0)) - return (x//abs2(y)) + if(iszero(abs2(y))) + throw(DivideError()) + end + if(iszero((x//abs2(y))) ) + return 0//1 + 0//1*im 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) m=max(abs(real_y),abs(imag_y)) if(m==0) - return 1//0 + throw(DivideError()) end 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)//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 function show(io::IO, x::Rational) From 8869bc28517bb740d8062da58db06bf67dc9bf47 Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Mon, 25 Nov 2024 22:37:19 +0530 Subject: [PATCH 06/13] Update rational.jl added comments that were deleted unintentionally --- base/rational.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/base/rational.jl b/base/rational.jl index f9b4b4bff3d44..e1a8df34f9867 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -49,6 +49,13 @@ Rational(n::T, d::T) where {T<:Integer} = Rational{T}(n, d) Rational(n::Integer, d::Integer) = Rational(promote(n, d)...) Rational(n::Integer) = unsafe_rational(n, one(n)) +""" + divgcd(x::Integer, y::Integer) + +Returns `(x÷gcd(x,y), y÷gcd(x,y))`. + +See also [`div`](@ref), [`gcd`](@ref). +""" function divgcd(x::TX, y::TY)::Tuple{TX, TY} where {TX<:Integer, TY<:Integer} g = gcd(uabs(x), uabs(y)) div(x,g), div(y,g) From f1cafbe32a9565f8a6b44db82a82d468915e7981 Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Tue, 26 Nov 2024 22:36:28 +0530 Subject: [PATCH 07/13] Update rational.jl fixed uint test which failed before --- base/rational.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index e1a8df34f9867..908e348571064 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -124,9 +124,9 @@ function //(x::Number, y::Complex{<:Integer}) scaled_a = real_y // m scaled_b = imag_y // m denom = (scaled_a^2 + scaled_b^2) - real_part = (x * (((real_y//denom)//m)//m)) - imag_part = (-x * (((imag_y// denom)//m)//m)) - ans = (real_part + imag_part * im) + real_part = ((((real_y//denom)//m)//m)) + imag_part = ((((imag_y// denom)//m)//m)) + ans = (x*real_part - x*imag_part * im) return ans end From 56560c7c88973668fdd71c15f736aa86647cafe8 Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Fri, 29 Nov 2024 21:15:45 +0530 Subject: [PATCH 08/13] Update rational.jl attempt to remove ambiguity which seems to be affecting build tests --- base/rational.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 908e348571064..7d5c69435318f 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -105,14 +105,17 @@ function //(x::Rational, y::Rational) end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) -function //(x::Number, y::Complex) - if(iszero(abs2(y))) +function //(x::Number, y::Complex{T}) where T + if T <: Integer + return invoke(//, Tuple{Number, Complex{<:Integer}}, x, y) + end + if iszero(abs2(y)) throw(DivideError()) end - if(iszero((x//abs2(y))) ) + if iszero((x // abs2(y))) return 0//1 + 0//1*im end - return (x//abs2(y))*conj(y) + return (x // abs2(y)) * conj(y) end function //(x::Number, y::Complex{<:Integer}) real_y = real(y) From fe7165a8875aded2fb116e536e02614ab81fc6e1 Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Fri, 29 Nov 2024 23:08:22 +0530 Subject: [PATCH 09/13] Update rational.jl fixes ambiguity by merging functions --- base/rational.jl | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 7d5c69435318f..0a205f605e4c4 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -105,31 +105,22 @@ function //(x::Rational, y::Rational) end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) -function //(x::Number, y::Complex{T}) where T - if T <: Integer - return invoke(//, Tuple{Number, Complex{<:Integer}}, x, y) - end - if iszero(abs2(y)) - throw(DivideError()) - end +function //(x::Number, y::Complex) if iszero((x // abs2(y))) return 0//1 + 0//1*im end - return (x // abs2(y)) * conj(y) -end -function //(x::Number, y::Complex{<:Integer}) real_y = real(y) imag_y = imag(y) - m=max(abs(real_y),abs(imag_y)) - if(m==0) + m = max(abs(real_y), abs(imag_y)) + if m == 0 throw(DivideError()) end scaled_a = real_y // m scaled_b = imag_y // m - denom = (scaled_a^2 + scaled_b^2) - real_part = ((((real_y//denom)//m)//m)) - imag_part = ((((imag_y// denom)//m)//m)) - ans = (x*real_part - x*imag_part * im) + denom = (scaled_a^2 + scaled_b^2) + real_part = ((((real_y // denom) // m) // m)) + imag_part = ((((imag_y // denom) // m) // m)) + ans = (x * real_part - x * imag_part * im) return ans end From 5914d0b167aed06ff23adbbc740fcfa69298d81f Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Sat, 30 Nov 2024 03:26:52 +0530 Subject: [PATCH 10/13] Update rational.jl type instability fixed --- base/rational.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/rational.jl b/base/rational.jl index 0a205f605e4c4..476821ed977b1 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -107,7 +107,7 @@ end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) function //(x::Number, y::Complex) if iszero((x // abs2(y))) - return 0//1 + 0//1*im + return Rational{Int}(0) + Rational{Int}(0)*im end real_y = real(y) imag_y = imag(y) From f7b220a4f851704e4300f99dafcbbcc4aa3562fc Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Sat, 30 Nov 2024 17:12:07 +0530 Subject: [PATCH 11/13] Update rational.jl typ instability fix attempt --- base/rational.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/rational.jl b/base/rational.jl index 476821ed977b1..38db773605baa 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -107,7 +107,7 @@ end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) function //(x::Number, y::Complex) if iszero((x // abs2(y))) - return Rational{Int}(0) + Rational{Int}(0)*im + return complex(x // abs2(y)) end real_y = real(y) imag_y = imag(y) From 8bdb1ce540cb59c6ebf4a94344d9693924c123cb Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:22:52 +0530 Subject: [PATCH 12/13] Update rational.jl --- base/rational.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 38db773605baa..9f9ee5394443e 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -106,8 +106,8 @@ end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) function //(x::Number, y::Complex) - if iszero((x // abs2(y))) - return complex(x // abs2(y)) + if isinf(y) + return complex(x*0) end real_y = real(y) imag_y = imag(y) From c05bab51192950b98667cd0697d60d5e6ea6526d Mon Sep 17 00:00:00 2001 From: Priynsh <119518987+Priynsh@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:29:09 +0530 Subject: [PATCH 13/13] Update rational.jl --- base/rational.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 9f9ee5394443e..0effe7d4e521f 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -107,14 +107,14 @@ end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) function //(x::Number, y::Complex) if isinf(y) - return complex(x*0) + return (x*(0//1)-x*(0//1)*im) + end + if iszero(y) + throw(DivideError()) end real_y = real(y) imag_y = imag(y) m = max(abs(real_y), abs(imag_y)) - if m == 0 - throw(DivideError()) - end scaled_a = real_y // m scaled_b = imag_y // m denom = (scaled_a^2 + scaled_b^2)