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 25 commits into
base: master
Choose a base branch
from

Conversation

Priynsh
Copy link
Contributor

@Priynsh Priynsh commented Nov 6, 2024

Fixes #53435 Fixes #56245. I have added conditions for division by zero and infinite handling in the case of complex numbers and overflow handling in the case of integer-based complex numbers. Further, I have added a few tests for the same :)

@giordano giordano added rationals The Rational type and values thereof complex Complex numbers labels Nov 6, 2024
base/rational.jl Outdated Show resolved Hide resolved
base/rational.jl Outdated Show resolved Hide resolved
applied scaling while calculating absolute values in order to prevent overflow at any step
@Priynsh Priynsh requested a review from nhz2 November 7, 2024 10:14
@nsajko
Copy link
Contributor

nsajko commented Nov 7, 2024

doctest fails:

│ Subexpression:
│ 
│ 3//2 / (1 + 2im)
│ 
│ Evaluated output:
│ 
│ ERROR: OverflowError: 422212465065984 * 281474976710656 overflowed for type Int64
│ Stacktrace:
│  [1] throw_overflowerr_binaryop(op::Symbol, x::Int64, y::Int64)
│    @ Base.Checked ./checked.jl:163
│  [2] checked_mul
│    @ ./checked.jl:297 [inlined]
│  [3] //
│    @ ./rational.jl:97 [inlined]
│  [4] //(x::Rational{Int64}, y::Complex{Int64})
│    @ Base ./rational.jl:124
│  [5] /(x::Rational{Int64}, y::Complex{Int64})
│    @ Base ./rational.jl:436
│  [6] top-level scope
│    @ none:1
│ 
│ Expected output:
│ 
│ 3//10 - 3//5*im

base/rational.jl Outdated Show resolved Hide resolved
fixed the overflow issue in my solution, implemented iszero and isinf wherever possible for lighter implementation
@Priynsh
Copy link
Contributor Author

Priynsh commented Nov 7, 2024

Hey, the overflow was due to converting complex floats to rationals, I have instead come up with a solution requiring no rational conversions :)

base/rational.jl Outdated Show resolved Hide resolved
base/rational.jl Outdated Show resolved Hide resolved
test/rational.jl Show resolved Hide resolved
@Priynsh
Copy link
Contributor Author

Priynsh commented Nov 13, 2024

okay, so I have added tests from #53453 (they work fine), removed the redundancy related to isinf(y) being true only for rational y, added a divide error for division by complex(0), and removed type instability from function //(x::Number, y::Complex{<:Integer}).

@Priynsh Priynsh requested a review from nhz2 November 13, 2024 12:32
@Priynsh Priynsh marked this pull request as draft November 29, 2024 12:09
attempt to remove ambiguity which seems to be affecting build tests
fixes ambiguity by  merging functions
@Priynsh Priynsh marked this pull request as ready for review November 30, 2024 23:47
@Priynsh
Copy link
Contributor Author

Priynsh commented Dec 5, 2024

Bump

base/rational.jl Outdated Show resolved Hide resolved
@Priynsh Priynsh requested a review from nhz2 December 9, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers rationals The Rational type and values thereof
Projects
None yet
5 participants