Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the //(x::Number, y::Complex) one liner to accomodate silent overflows and division by zero/infinity #56478

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a10ccb1
Add two test files
kokocha213 Nov 6, 2024
b3bb6c5
Merge branch 'JuliaLang:master' into rationaloverflow
Priynsh Nov 6, 2024
87e2284
Update rational.jl
Priynsh Nov 7, 2024
3879186
Update rational.jl
Priynsh Nov 7, 2024
c8ac71c
Merge branch 'master' into rationaloverflow
Priynsh Nov 7, 2024
27876d9
Merge branch 'master' into rationaloverflow
Priynsh Nov 7, 2024
915a5af
Update rational.jl
Priynsh Nov 13, 2024
d9cea91
Update rational.jl
Priynsh Nov 13, 2024
8f1cad2
Merge branch 'JuliaLang:master' into rationaloverflow
Priynsh Nov 13, 2024
28571a0
Merge branch 'master' into rationaloverflow
Priynsh Nov 15, 2024
8869bc2
Update rational.jl
Priynsh Nov 25, 2024
382b1ae
Merge branch 'master' into rationaloverflow
Priynsh Nov 25, 2024
fbdb584
Merge branch 'JuliaLang:master' into rationaloverflow
Priynsh Nov 26, 2024
f1cafbe
Update rational.jl
Priynsh Nov 26, 2024
b766759
Merge branch 'master' into rationaloverflow
Priynsh Nov 26, 2024
56560c7
Update rational.jl
Priynsh Nov 29, 2024
fe7165a
Update rational.jl
Priynsh Nov 29, 2024
5914d0b
Update rational.jl
Priynsh Nov 29, 2024
f7b220a
Update rational.jl
Priynsh Nov 30, 2024
953c046
Merge branch 'master' into rationaloverflow
Priynsh Nov 30, 2024
8bdb1ce
Update rational.jl
Priynsh Nov 30, 2024
f38aaab
Merge branch 'master' into rationaloverflow
Priynsh Nov 30, 2024
3d262a9
Merge branch 'master' into rationaloverflow
Priynsh Dec 1, 2024
c05bab5
Update rational.jl
Priynsh Dec 9, 2024
6d1adc3
Merge branch 'master' into rationaloverflow
Priynsh Dec 9, 2024
2481a19
Merge branch 'master' into rationaloverflow
Priynsh Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,33 @@ 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(iszero((x//abs2(y))) || isinf((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
Priynsh marked this conversation as resolved.
Show resolved Hide resolved
real_y = real(y)
imag_y = imag(y)
m=max(abs(real_y),abs(imag_y))
if(m==0)
return 1//0
Priynsh marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
7 changes: 6 additions & 1 deletion test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Priynsh marked this conversation as resolved.
Show resolved Hide resolved
@test_throws OverflowError -(0x01//0x0f)
@test_throws OverflowError -(typemin(Int)//1)
@test_throws OverflowError (typemax(Int)//3) + 1
Expand Down