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 3 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
Comment on lines +108 to +110
if isinf(real(y)) || isinf(imag(y))
return 0//1
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If y is a complex integer isn't isinf here always false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In issue #56245 the errors were thrown for "incorrectly assumes y is finite and nonzero" so I added conditions for for infinite and 0.

base/rational.jl Outdated
end
real_y = real(y)
imag_y = imag(y)
denom = Int32(abs(real_y))^2 + Int32(abs(imag_y))^2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Int32 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added that to account for any overflow, but int16 would work perfectly fine as well!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I’ve implemented scaling to prevent any overflow in calculations (removing any need for INT32. Could you take a look at my approach

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
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
3 participants