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

Avoid convert in ^ for literal powers #619

Merged
merged 2 commits into from
Jan 31, 2024
Merged

Avoid convert in ^ for literal powers #619

merged 2 commits into from
Jan 31, 2024

Conversation

lbenet
Copy link
Member

@lbenet lbenet commented Jan 18, 2024

Fix #618

@lbenet
Copy link
Member Author

lbenet commented Jan 18, 2024

This is related to the test that fails:

julia> f(x)  = interval(2)^x
f (generic function with 1 method)

julia> f′(x) = log(interval(2)) * f(x)
f′ (generic function with 1 method)

julia> df(t) = ForwardDiff.derivative(f, t)
df (generic function with 1 method)

julia> f′(0) === df(0)
false

# But
julia> isequal_interval(f′(0), df(0))
true

The subtlety in === is that df(0) is NG (because of ForwardDiff).

@codecov-commenter
Copy link

codecov-commenter commented Jan 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (21e84ff) 83.56% compared to head (255d9b2) 83.41%.
Report is 1 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #619      +/-   ##
==========================================
- Coverage   83.56%   83.41%   -0.16%     
==========================================
  Files          25       25              
  Lines        2142     2146       +4     
==========================================
  Hits         1790     1790              
- Misses        352      356       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lbenet
Copy link
Member Author

lbenet commented Jan 19, 2024

I will need to add tests. Perhaps it is also good time to increase the tests related to all the power functions and power modes.

@lbenet lbenet changed the title Avoid convert in ^ Avoid convert in ^ for literal powers Jan 26, 2024
I have reverted previous changes too
@lbenet
Copy link
Member Author

lbenet commented Jan 26, 2024

Followed @OlivierHnt suggestion here; it seems to work nicely, distinguishing literal powers from whatever. (All previous changes have been reverted.)

@OlivierHnt
Copy link
Member

Looks good to me!

Note (just as a memo): while Val{n} can have n to be a float or a bool, the call to Base.literal_pow only occurs when n is a literal integer which makes this implementation correct.
Also, the issue is not entirely fixed for large literal integers:

julia> interval(1)^2305843009213693951
[1.0, 1.0]_com

julia> interval(1)^2305843009213693952 # any integer larger than this one will also have the NG flag
[1.0, 1.0]_com_NG

Whether or not this is a bug, it is on Julia's side.

@OlivierHnt
Copy link
Member

OlivierHnt commented Jan 26, 2024

I am on board with merging this PR.

@Kolaru any thoughts?

@lbenet
Copy link
Member Author

lbenet commented Jan 27, 2024

Note (just as a memo): while Val{n} can have n to be a float or a bool, the call to Base.literal_pow only occurs when n is a literal integer which makes this implementation correct. Also, the issue is not entirely fixed for large literal integers:

julia> interval(1)^2305843009213693951
[1.0, 1.0]_com

julia> interval(1)^2305843009213693952 # any integer larger than this one will also have the NG flag
[1.0, 1.0]_com_NG

Whether or not this is a bug, it is on Julia's side.

Good catch! I will check if I can find what's going on, but I guess this is some sort of rounding issue (due perhaps to the conversion to Float64), as commented here.

@Kolaru
Copy link
Collaborator

Kolaru commented Jan 29, 2024

For me it can be merged once the tests are there :)

@OlivierHnt
Copy link
Member

At the end of test/interval_tests/power.jl, we can just add something like

@testset "Literal powers" begin
    x = interval(1)
    n = 2
    @test isguaranteed(x ^ 2)
    @test !isguaranteed(x ^ n)
    @test_broken isguaranteed(x ^ 2.0)
    @test_broken isguaranteed(x ^ 2305843009213693952)
end

Co-autored by:  Olivier Hénot
<[email protected]>
@OlivierHnt OlivierHnt merged commit d44f607 into master Jan 31, 2024
32 checks passed
@OlivierHnt OlivierHnt deleted the lb/avoid_convert branch January 31, 2024 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid convert to Interval in ^
4 participants