Skip to content

Commit

Permalink
Improve type stable in function expint (#466)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
songjhaha and songjhaha committed Feb 29, 2024
1 parent fdf95ab commit b27f0ad
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/expint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ En_safe_gamma_term(ν::Integer, z::Real) = (z ≥ 0 || isodd(ν) ? 1 : -1) * exp
# https://functions.wolfram.com/GammaBetaErf/ExpIntegralE/10/0005/
# returns the two terms from the above equation separately
function En_cf_gamma::Number, z::Number, n::Int=1000)
A = float(1 - ν)
B::typeof(A) = 1
Bprev::typeof(A) = 0
Aprev::typeof(A) = 1
A, z = map(float, promote(1 - ν, z))
B = oneunit(A)
Bprev = zero(B)
Aprev = oneunit(A)
ϵ = 10*eps(real(B))
scale = sqrt(floatmax(real(A)))

Expand Down
3 changes: 3 additions & 0 deletions test/expint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ using Base.MathConstants
Γ, cf, iter = SpecialFunctions.En_cf_gamma(1 / 2, x)
@test Γ + cf*exp(-x) y
end
# type stability
@test @inferred(SpecialFunctions.En_cf_gamma(1, 1.0 + 2.1im, 1000)) isa Tuple{ComplexF64,ComplexF64,Int}
@test @inferred(SpecialFunctions.En_cf_gamma(1, 1.0f0, 1000)) isa Tuple{Float32,Float32,Int}
end
@testset "En_expand_origin" begin
for (x, y) in zip(xs, ys)
Expand Down

0 comments on commit b27f0ad

Please sign in to comment.