Skip to content

Commit b5cd67e

Browse files
authored
backport: isnegative in intpow (#591)
* backport: isnegative in intpow * Bump version to v0.8.58 * Add UInt test
1 parent e25ab77 commit b5cd67e

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.8.57"
3+
version = "0.8.58"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/Fun.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ end
594594

595595
\(c::Number, f::Fun) = Fun(f.space, c \ f.coefficients)
596596

597+
# eliminate the type-unstable 1/t branch by using an unsigned integer exponent
598+
isnegative(x) = x < zero(x)
599+
isnegative(::Unsigned) = false
600+
597601
@static if VERSION >= v"1.8"
598602
Base.@constprop :aggressive intpow(f::Fun, k::Integer) = _intpow(f, k)
599603
else
@@ -612,10 +616,10 @@ end
612616
f * f * f * f
613617
else
614618
t = foldl(*, fill(f, abs(k)-1), init=f)
615-
if k > 0
616-
return t
617-
else
619+
if isnegative(k)
618620
return 1/t
621+
else
622+
return t
619623
end
620624
end
621625
end

test/SpacesTest.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ using LinearAlgebra
7070
@test ApproxFunBase.intpow(f, n) == f^n == reduce(*, fill(f, n))
7171
end
7272
@test ApproxFunBase.intpow(f,-2) == f^-2 == 1/(f*f)
73+
@test f^2 == f^UInt(2)
7374

7475
if VERSION >= v"1.8"
7576
@test (@inferred (x -> x^1)(sp)) == sp

0 commit comments

Comments
 (0)