From 4454d8f233e813c3eb10d6ad1ffb823c112c9752 Mon Sep 17 00:00:00 2001 From: Alexander Voigt Date: Thu, 5 Oct 2023 14:54:48 +0200 Subject: [PATCH] stable return type --- src/Factorial.jl | 12 ++++++------ test/Factorial.jl | 10 ++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Factorial.jl b/src/Factorial.jl index 883d0cd..68851f7 100644 --- a/src/Factorial.jl +++ b/src/Factorial.jl @@ -124,11 +124,11 @@ const INVERSE_FACTORIALS = ( function fac(n::Integer, T) if n < 0 throw(DomainError(n, "fac not implemented for n < 0")) - elseif T == Float64 + elseif issimplefloat(T) if n + 1 <= length(FACTORIALS) - FACTORIALS[n + 1] + convert(T, FACTORIALS[n + 1]) else - Inf + convert(T, Inf) end else BigFloat(factorial(big(n))) @@ -139,11 +139,11 @@ end function inv_fac(n::Integer, T) if n < 0 throw(DomainError(n, "inv_fac not implemented for n < 0")) - elseif T == Float64 + elseif issimplefloat(T) if n + 1 <= length(INVERSE_FACTORIALS) - INVERSE_FACTORIALS[n + 1] + convert(T, INVERSE_FACTORIALS[n + 1]) else - 0.0 + zero(T) end else BigFloat(inv(factorial(big(n)))) diff --git a/test/Factorial.jl b/test/Factorial.jl index 4217612..5235d03 100644 --- a/test/Factorial.jl +++ b/test/Factorial.jl @@ -2,6 +2,11 @@ @test_throws DomainError PolyLog.fac(-1, Float64) @test_throws DomainError PolyLog.fac(-1, BigFloat) + @test typeof(PolyLog.fac(1, Float16)) == Float16 + @test typeof(PolyLog.fac(1, Float32)) == Float32 + @test typeof(PolyLog.fac(1, Float64)) == Float64 + @test typeof(PolyLog.fac(1, BigFloat)) == BigFloat + @test PolyLog.fac( 0, Float64) == 1.0 @test PolyLog.fac( 1, Float64) == 1.0 @test PolyLog.fac( 2, Float64) == 2.0 @@ -22,6 +27,11 @@ end @test_throws DomainError PolyLog.inv_fac(-1, Float64) @test_throws DomainError PolyLog.inv_fac(-1, BigFloat) + @test typeof(PolyLog.inv_fac(1, Float16)) == Float16 + @test typeof(PolyLog.inv_fac(1, Float32)) == Float32 + @test typeof(PolyLog.inv_fac(1, Float64)) == Float64 + @test typeof(PolyLog.inv_fac(1, BigFloat)) == BigFloat + @test PolyLog.inv_fac( 0, Float64) == 1.0 @test PolyLog.inv_fac( 1, Float64) == 1.0 @test PolyLog.inv_fac( 2, Float64) == 1/2