-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
a10ccb1
b3bb6c5
87e2284
3879186
c8ac71c
27876d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,9 +98,34 @@ 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 | ||
Comment on lines
+115
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If y is a complex integer isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
real_y = real(y) | ||
imag_y = imag(y) | ||
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 | ||
|
||
function show(io::IO, x::Rational) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comparing with
iszero
,isinf
, etc., should be cheaper than with==
.