Skip to content

Commit

Permalink
stable return type
Browse files Browse the repository at this point in the history
  • Loading branch information
Expander committed Oct 5, 2023
1 parent 654e6c6 commit 4454d8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Factorial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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))))
Expand Down
10 changes: 10 additions & 0 deletions test/Factorial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4454d8f

Please sign in to comment.