Skip to content

Commit b27f0ad

Browse files
songjhahasongjhaha
andauthored
Improve type stable in function expint (#466)
* improve type stable for expint * add infer test && improve promote * fix promote for Float32 case * improve inferred test --------- Co-authored-by: songjhaha <songjh96@foxmail.cc>
1 parent fdf95ab commit b27f0ad

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/expint.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ En_safe_gamma_term(ν::Integer, z::Real) = (z ≥ 0 || isodd(ν) ? 1 : -1) * exp
207207
# https://functions.wolfram.com/GammaBetaErf/ExpIntegralE/10/0005/
208208
# returns the two terms from the above equation separately
209209
function En_cf_gamma::Number, z::Number, n::Int=1000)
210-
A = float(1 - ν)
211-
B::typeof(A) = 1
212-
Bprev::typeof(A) = 0
213-
Aprev::typeof(A) = 1
210+
A, z = map(float, promote(1 - ν, z))
211+
B = oneunit(A)
212+
Bprev = zero(B)
213+
Aprev = oneunit(A)
214214
ϵ = 10*eps(real(B))
215215
scale = sqrt(floatmax(real(A)))
216216

test/expint.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ using Base.MathConstants
158158
Γ, cf, iter = SpecialFunctions.En_cf_gamma(1 / 2, x)
159159
@test Γ + cf*exp(-x) y
160160
end
161+
# type stability
162+
@test @inferred(SpecialFunctions.En_cf_gamma(1, 1.0 + 2.1im, 1000)) isa Tuple{ComplexF64,ComplexF64,Int}
163+
@test @inferred(SpecialFunctions.En_cf_gamma(1, 1.0f0, 1000)) isa Tuple{Float32,Float32,Int}
161164
end
162165
@testset "En_expand_origin" begin
163166
for (x, y) in zip(xs, ys)

0 commit comments

Comments
 (0)