Skip to content
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

Merged
merged 5 commits into from
Jan 31, 2024
Merged

Conversation

NHDaly
Copy link
Member

@NHDaly NHDaly commented Jan 10, 2024

Fixes #52262.

Performs ^(x, y) but throws OverflowError on overflow.

Example:

julia> 2^62
4611686018427387904

julia> 2^63
-9223372036854775808

julia> checked_pow(2, 63)
ERROR: OverflowError: 2147483648 * 4294967296 overflowed for type Int64

@NHDaly NHDaly force-pushed the nhd-checked_pow branch 2 times, most recently from 678d00a to a945b7d Compare January 10, 2024 18:06
@NHDaly NHDaly force-pushed the nhd-checked_pow branch 2 times, most recently from 5e9dc9a to d23da92 Compare January 10, 2024 18:12
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)
Copy link
Sponsor Member

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?

Copy link
Member Author

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 ^?

Copy link
Sponsor Member

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).

Copy link
Member

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?

Copy link
Member Author

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

Copy link
Sponsor Member

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

Copy link
Sponsor Member

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.

Copy link
Sponsor Member

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 *.

Copy link
Member

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

Copy link
Member Author

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

base/checked.jl Outdated Show resolved Hide resolved
Copy link
Sponsor Member

@inkydragon inkydragon left a 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.

julia/doc/src/base/math.md

Lines 153 to 154 in cf44b53

Base.Checked.checked_cld
Base.Checked.add_with_overflow

@vtjnash vtjnash added maths Mathematical functions merge me PR is reviewed. Merge when all tests are passing labels Jan 30, 2024
@inkydragon
Copy link
Sponsor Member

Test compiler/ssair failed on test aarch64-apple-darwin

Looks rarely seen, not sure if it's related.

test log
The global RNG seed was 0xaa6cce87493099399c5d4c23b781e1.
Error in testset compiler/ssair:
Test Failed at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-XG3Q6T6R70.0/build/default-honeycrisp-XG3Q6T6R70-0/julialang/julia-master/julia-ad6f0489c3/share/julia/test/compiler/ssair.jl:552
  Expression: length(ir.stmts) == 2
   Evaluated: 3 == 2
Error in testset compiler/ssair:
Test Failed at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-XG3Q6T6R70.0/build/default-honeycrisp-XG3Q6T6R70-0/julialang/julia-master/julia-ad6f0489c3/share/julia/test/compiler/ssair.jl:553
  Expression: Meta.isexpr((ir.stmts[1])[:stmt], :invoke)
Error in testset compiler/ssair:
Test Failed at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-XG3Q6T6R70.0/build/default-honeycrisp-XG3Q6T6R70-0/julialang/julia-master/julia-ad6f0489c3/share/julia/test/compiler/ssair.jl:559
  Expression: length(ir.stmts) == 4
   Evaluated: 5 == 4
Error in testset compiler/ssair:
Test Failed at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-XG3Q6T6R70.0/build/default-honeycrisp-XG3Q6T6R70-0/julialang/julia-master/julia-ad6f0489c3/share/julia/test/compiler/ssair.jl:560
  Expression: Meta.isexpr((ir.stmts[1])[:stmt], :invoke)
Error in testset compiler/ssair:
Test Failed at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-XG3Q6T6R70.0/build/default-honeycrisp-XG3Q6T6R70-0/julialang/julia-master/julia-ad6f0489c3/share/julia/test/compiler/ssair.jl:632
  Expression: new_call_idx !== nothing
   Evaluated: nothing !== nothing
Error in testset compiler/ssair:
Test Failed at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-XG3Q6T6R70.0/build/default-honeycrisp-XG3Q6T6R70-0/julialang/julia-master/julia-ad6f0489c3/share/julia/test/compiler/ssair.jl:633
  Expression: new_call_idx == new_invoke_idx + 1
   Evaluated: nothing == 4

@vtjnash vtjnash removed the merge me PR is reviewed. Merge when all tests are passing label Jan 31, 2024
@vtjnash
Copy link
Sponsor Member

vtjnash commented Jan 31, 2024

That seems definitely related, since it is a test for inferability of ^ that this changes. Maybe @aviatesk can take a quick look and see if something is going wrong there, or if the test is wrong?

@aviatesk aviatesk self-assigned this Jan 31, 2024
@aviatesk aviatesk removed their assignment Jan 31, 2024
@aviatesk
Copy link
Sponsor Member

The modifications in this PR seem to be reasonable. I resolved test failures by improving the robustness of compiler/ssair.

@inkydragon
Copy link
Sponsor Member

REPL test failure not related, fixed by #53134. pr need rebase.

@vtjnash vtjnash merged commit 8304111 into master Jan 31, 2024
3 of 7 checks passed
@vtjnash vtjnash deleted the nhd-checked_pow branch January 31, 2024 16:50
d-netto pushed a commit to RelationalAI/julia that referenced this pull request Mar 19, 2024
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]>
d-netto pushed a commit to RelationalAI/julia that referenced this pull request Mar 19, 2024
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]>
d-netto added a commit to RelationalAI/julia that referenced this pull request Mar 19, 2024
… (#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]>
d-netto added a commit to RelationalAI/julia that referenced this pull request Apr 16, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request Apr 23, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request Apr 24, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request Apr 30, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request Apr 30, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request May 2, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request May 9, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request May 19, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request May 26, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request May 28, 2024
… (#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]>
DelveCI pushed a commit to RelationalAI/julia that referenced this pull request May 29, 2024
… (#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]>
Drvi added a commit to RelationalAI/julia that referenced this pull request Jun 7, 2024
… (#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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No Base.Checked.checked_pow function
7 participants