-
-
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
check for typemin(T)
in denominator of rational
#32572
Conversation
I am not happy with your use of The former makes use of the reflection interface and produces runtime code like:
while the proposed call evaluates essentially to a constant at run time:
|
Are you suggesting that I implement a |
Yes |
The implementation: BTW: |
But it's not valid in general, the definitions are tiny, let's not try to save duplication here at the cost of an approximation |
Bump. |
bump! |
base/rational.jl
Outdated
num == den == zero(T) && __throw_rational_argerror(T) | ||
num == den == zero(T) && __throw_rational_argerror_zero(T) | ||
if T <: Signed && hastypemin(T) | ||
den == typemin(T) && __throw_rational_argerror_typemin(T) |
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.
I'm sorry, in my previous review I was just looking at the typemin
stuff, but actually I think this error logic here is not appropriate in general. A type could have a typemin
without the current definition being an error. The real problem occurs when typemin(T) == -typemin(T)
(which is typically true for bit types which use 2-complement arithmetic). So I suggest instead moving the error checking in the below line, something like: T <: Signed && signbit(den) && signbit(-den) && throw(...)
.
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.
Yes, makes sense. So just delete all the hastypemin
stuff?
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.
I don't think, this check is very good, because it requires construction of -den
, which is expensive in the case of BigInt
.
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.
It can be constructed only when necessary.
I think the previous commit was better, because it does not require to construct the negative of a
|
base/rational.jl
Outdated
# issue #32569 | ||
if T <: Signed | ||
signbit(den) && signbit(-den) && __throw_rational_argerror_typemin(T) | ||
end | ||
num2, den2 = signbit(den) ? divgcd(-num, -den) : divgcd(num, den) |
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.
As @KlausC suggested, this constructs unnecessarily -den
once more than necessary. You can rearrange the code to fix that, e.g.
num2, den2 = if signbit(den)
den = -den
T <: Signed && signbit(den) && __throw_rational_argerror_typemin(T)
divgcd(-num, den)
else
divgcd(num, den)
end
a little polished:
|
@StefanKarpinski , what do you think? |
base/rational.jl
Outdated
num2, den2 = signbit(den) ? divgcd(-num, -den) : divgcd(num, den) | ||
new(num2, den2) | ||
num == den == zero(T) && __throw_rational_argerror_zero(T) | ||
# issue #32569 |
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.
Usually we don't insert references to issues in code (as opposed to tests), and here I think we can understand the reason of this test by following the thrown error, faster than by looking up github. No really a problem though, and probably not necessary to update your PR.
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.
That sounds reasonable. Went ahead and deleted it now.
Additional issue? Are these test cases ok now?
maybe the |
@simeonschaub, would you mind to insert the changes to fix - last comment? |
I am not sure, if the The test cases are only valid if Do you run |
Oh, sorry. My bad! I pushed a bit too prematurely. |
Looks like good to go, I will merge in one day or two if no other review comments (does it look good for you now @KlausC ?) |
Thanks @simeonschaub for the fix and @KlausC for the review! |
fix #32569. This is my first PR here, I hope, I didn't miss anything.