-
-
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
promote fallback for division #19714
Comments
I would love to have a go at this one. However I'm not quite following. Julia already contains that fallback: https://github.com/JuliaLang/julia/blob/master/base/promotion.jl#L193 The problem is that The following seems to work:
Does this look decent? I am not confident it is robust against some awkward scenario, e.g. |
The conversion to float kicks in first because that functioj is defined for two |
How about we simply replace
This means if the parameters are of different types we will promote them to a common type Can anyone see a problem with this? If not I'll wire it in. |
The only potential problem would be if the integer promotion was potentially lossy, e.g. Int with UInt. So it might be a bit trickier than it initially seemed to get speed in the fast cases and correctness in the others. |
mmm...
So maybe
< 64 bits is no problem as e.g.
Explicit overloads could be provided in the same way for 128-bit. I don't feel confident to patch this through, I think it needs someone with a better perspective. |
#19645 ? |
The principle here is that if there's an implementation of a promoted operator for some type and promotions for that type, unless other methods are defined, the operation should always be "funnelled" through the core operator definition for that type. The method for `/(::Integer, ::Integer)` violated that principle since it would take precedence in mixed-integer-type division cases even in the presence of a same-type method definition for a custom integer type and the appropriate promotion rules.
The principle here is that if there's an implementation of a promoted operator for some type and promotions for that type, unless other methods are defined, the operation should always be "funnelled" through the core operator definition for that type. The method for `/(::Integer, ::Integer)` violated that principle since it would take precedence in mixed-integer-type division cases even in the presence of a same-type method definition for a custom integer type and the appropriate promotion rules.
Integer /: restrict fallback to same-type case (fix #19714)
Leaving this here as a breadcrumb, but if someone else wants to go for it, feel free:
https://discourse.julialang.org/t/playing-around-with-intmod/980/10
I think we should probably have a
/(a::Number, b::Number) = /(promote(a,b)...)
fallback. This would allow directly defining division for an integer type and promotion with e.g.Int
to make1/n
to work, whereas currently it won't because the rule to convert to float first kicks in.The text was updated successfully, but these errors were encountered: