-
-
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 non-zero denominator in iszero
for Rational
#54874
base: master
Are you sure you want to change the base?
Conversation
How much does this degrade performance? |
I imagine the performance difference is negligible, but it seems strange to go out of the way to handle unconstructable values.
Even with this (particular) fix, It would seem that this PR starts the slippery slope toward checking I would prefer that this PR simply delete this method (and the matching
|
Viewpoint to consider: |
Yes! It follows a lot of its properties |
I think discussion of |
I'm intrigued by this: is there a motivating example where one does want to treat 0//0 as 0? This seems to counter the properties that one may expect from such a number. |
What I attempted and failed to allude to is the difference between the following two lines, either of which a sane person could write (if x >= 0 ? sqrt(x) : error() # look for what I want
x < 0 ? error() : sqrt(x) # reject what I don't want These two appear somewhat equivalent at first glance, but they are not equivalent. The only difference here (for most For example, a responsible coder might assume that When the invariants of the type are violated, it becomes very situational what the "correct" fallback behavior should be. I find the "look for what I want" pattern to be slightly more robust to this, which is why I prefer it. But there are no answers that are "best" or "most convenient" in all situations. In any case, I will still advocate for one of my counter-proposals that fulfill the invariant that |
Alright, thanks, I'm starting to get convinced that this might not be the right approach |
I still think |
Should the test be updated as well? Although, I can't think of cases other than invalids where the denominator check would be necessary. |
iszero
for Rational
Maybe just check for a few "prototypical" ones? iszero(zero(Rational{Int})) == true
iszero(reinterpret(Rational{Int}, (0, typemin(Int)))) == false
iszero(reinterpret(Rational{Int}, (0, -1))) == false
iszero(reinterpret(Rational{Int}, (0, 0))) == false
iszero(reinterpret(Rational{Int}, (0, 2))) == false
iszero(reinterpret(Rational{Int}, (0, typemax(Int)))) == false |
Typically, one can't construct
0//0
, and it's not a validRational
, so this issue doesn't show up in standard use cases. However,0//0
pops up occasionally in arrays constructed withsimilar
, and having this treated as0
messes with structured matrix detection logic. I am not convinced that this should beiszero
, since this isn't a valid number in the first place.Currently,
This PR would change this to