Skip to content

Commit fb1c8c4

Browse files
authored
isnegative in intpow (#590)
* isnegative in intpow * Add UInt test
1 parent c5225a7 commit fb1c8c4

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.9.13"
3+
version = "0.9.14"
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
Base.@constprop :aggressive function intpow(f, k)
598602
if k == 0
599603
ones(cfstype(f), space(f))
@@ -607,10 +611,10 @@ Base.@constprop :aggressive function intpow(f, k)
607611
f * f * f * f
608612
else
609613
t = foldl(*, fill(f, abs(k)-1), init=f)
610-
if k > 0
611-
return t
612-
else
614+
if isnegative(k)
613615
return 1/t
616+
else
617+
return t
614618
end
615619
end
616620
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)