-
-
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
Add Base.checked_pow(x,y)
to Base.Checked
library
#52849
Conversation
678d00a
to
a945b7d
Compare
5e9dc9a
to
d23da92
Compare
base/intfuncs.jl
Outdated
@@ -309,14 +309,16 @@ to_power_type(x) = convert(Base._return_type(*, Tuple{typeof(x), typeof(x)}), x) | |||
"\nMake x a float matrix by adding a zero decimal ", | |||
"(e.g., [2.0 1.0;1.0 0.0]^", p, " instead of [2 1;1 0]^", p, ")", | |||
"or write float(x)^", p, " or Rational.(x)^", p, "."))) | |||
@assume_effects :terminates_locally function power_by_squaring(x_, p::Integer) | |||
power_by_squaring(x_, p::Integer) = power_by_squaring_with_op(*, x_, p) |
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.
How about just changing this to power_by_squaring(x_, p::Integer; *=*)
and then the rest of the function stays the same?
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.
Clever! Sure that sounds fine to me. Are there any perf issues with kwargs these days for a tight mathematical op like ^
?
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.
No, keyword args have been fast for a very long time. You could also make it an optional third arg, but this seems like a good place for a keyword in case we want to be able to override specific other operations in the future (although I can't imagine what).
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.
don't you have to do something to make it specialize on the mul
operation?
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 think as long as you call the function, we do specialize on function args, oscar
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's totally allowed as a variable name:
julia> * = "Hello"
"Hello"
... and it works if you use spaces:
julia> f(x; * = *) = x*x
f (generic function with 1 method)
julia> f(3)
9
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.
The parsing issue is that *=
is an operator, which is why you need a space.
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.
The parser error even indicates this:
julia> f(x; *=*) = x*x
ERROR: ParseError:
# Error @ REPL[3]:1:6
f(x; *=*) = x*x
# └┘ ── invalid identifier
If you look closely, the highlighted part is *=
not just *
.
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.
(*) = (*)
is also an interesting face
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.
hah, i missed that -- i just assumed i was misreading the parser's highlighting. I haven't spent much time with the new parser yet and i'm not used to it - still working mostly in 1.9. Thanks all
ee8b2ff
to
fbf897b
Compare
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.
Don't forget to add the new function to the doc.
Lines 153 to 154 in cf44b53
Base.Checked.checked_cld | |
Base.Checked.add_with_overflow |
efa7e96
to
ad6f048
Compare
Test Looks rarely seen, not sure if it's related. test log
|
That seems definitely related, since it is a test for inferability of |
ad6f048
to
046c3c5
Compare
The modifications in this PR seem to be reasonable. I resolved test failures by improving the robustness of |
REPL test failure not related, fixed by #53134. pr need rebase. |
046c3c5
to
c80808d
Compare
Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]>
Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
… (#134) Fixes JuliaLang#52262. Performs `^(x, y)` but throws OverflowError on overflow. Example: ```julia julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> checked_pow(2, 63) ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64 ``` Co-authored-by: Nathan Daly <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
Fixes #52262.
Performs
^(x, y)
but throws OverflowError on overflow.Example: