diff --git a/src/scattering.jl b/src/scattering.jl index 0b4ed1a..efe0615 100644 --- a/src/scattering.jl +++ b/src/scattering.jl @@ -97,8 +97,8 @@ end Einstein-Smoluchowsky Scattering PDF. """ function es_scattering(cos_theta::T, b::T) where {T<:Real} - a = 1/(4*pi) * 1 / (1+b/3) - return a*(1+b*cos_theta^2) + a::T = 1/(4*pi) * 1 / (1+b/3) + return T(a*(1+b*cos_theta^2)) end """ @@ -107,8 +107,8 @@ end Anti-derivative of the ES scattering function. """ function es_scattering_integral(cos_theta::T, b::T) where {T<:Real} - a = 1/(4*pi) * 1 / (1+b/3) - return a*cos_theta*(1 + (b*cos_theta^2)/3) * 2*pi + a::T = 1/(4*T(pi)) * 1 / (1+b/3) + return T(a*cos_theta*(1 + (b*cos_theta^2)/3) * 2*pi) end """ @@ -117,7 +117,7 @@ end Integral of ES scattering function from -1 to cos_theta """ function es_scattering_cumulative(cos_theta::T, b::T) where {T<:Real} - return es_scattering_integral(cos_theta, b) - es_scattering_integral(-1., b) + return es_scattering_integral(cos_theta, b) - es_scattering_integral(-one(T), b) end """ diff --git a/test/runtests.jl b/test/runtests.jl index 1d91fa6..5b597b4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,5 @@ using CherenkovMediumBase +using CherenkovMediumBase: es_scattering, es_scattering_integral, es_scattering_cumulative using Test using Random @@ -115,8 +116,29 @@ end @testset "EinsteinSmoluchowsky" begin scattering_function = EinsteinSmoluchowsky(0.835) @test rand(scattering_function) ≈ -0.41788 atol=1e-5 + @test @inferred rand(scattering_function) isa Float64 end - + + @testset "Type Stability" begin + + let b = 0.835, cos_theta = 0.5 + + @test @inferred es_scattering(cos_theta, b) isa Float64 + @test @inferred es_scattering_integral(cos_theta, b) isa Float64 + @test @inferred es_scattering_cumulative(cos_theta, b) isa Float64 + end + + let b = 0.835f0, cos_theta = 0.5f0 + + @test @inferred es_scattering(cos_theta, b) isa Float32 + @test @inferred es_scattering_integral(cos_theta, b) isa Float32 + @test @inferred es_scattering_cumulative(cos_theta, b) isa Float32 + end + + + + + end end @testset "KopelevichScatteringModel" begin