Skip to content

Commit d44f607

Browse files
authored
Avoid convert in ^ for literal powers (#619)
* Fix `literal_pow` as suggested * Add tests for literal powers Co-autored by: Olivier Hénot <[email protected]>
1 parent 719bc91 commit d44f607

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/intervals/arithmetic/power.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ Base.:^(x::Rational, y::Interval) = ^(convert(Interval{typeof(x)}, x), y)
6666
Base.:^(x::Interval, y::Rational) = ^(x, convert(Interval{typeof(y)}, y))
6767

6868
# overwrite behaviour for small integer powers from https://github.com/JuliaLang/julia/pull/24240
69-
Base.literal_pow(::typeof(^), x::Interval, ::Val{n}) where {n} = x^n
69+
# Base.literal_pow(::typeof(^), x::Interval, ::Val{n}) where {n} = x^n
70+
Base.literal_pow(::typeof(^), x::Interval, ::Val{n}) where {n} = _select_pown(power_mode(), x, n)
7071

7172
# helper functions for power
7273

test/interval_tests/power.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,14 @@ end
9090
# @test real(b) == interval(-13.12878308146216, -13.128783081462153)
9191
# @test imag(b) == interval(-15.200784463067956, -15.20078446306795)
9292
# end
93+
94+
@testset "Literal powers" begin
95+
x = interval(-1,1)
96+
n = 2
97+
@test isguaranteed(x ^ 2)
98+
@test !isguaranteed(x ^ n)
99+
@test_broken isguaranteed(x ^ 2.0)
100+
@test_broken isguaranteed(x ^ 2305843009213693952)
101+
@test isequal_interval(x^2, interval(0,1))
102+
@test isequal_interval(x^3, x)
103+
end

0 commit comments

Comments
 (0)