Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 14 additions & 38 deletions src/Factorial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,54 +122,30 @@ const INVERSE_FACTORIALS = (

# returns n! for n >= 0
function fac(n::Integer, ::Type{T})::T where T
if issimplefloat(T)
convert(T, fac_f64(n))
else
fac_big(n)
end
end

function fac_f64(n::Integer)::Float64
if n < 0
throw(DomainError(n, "fac not implemented for n < 0"))
elseif n + 1 <= length(FACTORIALS)
FACTORIALS[n + 1]
else
Inf
end
end

function fac_big(n::Integer)::BigFloat
if n < 0
throw(DomainError(n, "fac not implemented for n < 0"))
elseif issimplefloat(T)
if n + 1 <= length(FACTORIALS)
convert(T, FACTORIALS[n + 1])
else
T(Inf)
end
else
BigFloat(factorial(big(n)))
float(factorial(big(n)))
end
end

# returns 1/n! for n >= 0
function inv_fac(n::Integer, ::Type{T})::T where T
if issimplefloat(T)
convert(T, inv_fac_f64(n))
else
inv_fac_big(n)
end
end

function inv_fac_f64(n::Integer)::Float64
if n < 0
throw(DomainError(n, "inv_fac not implemented for n < 0"))
elseif n + 1 <= length(INVERSE_FACTORIALS)
INVERSE_FACTORIALS[n + 1]
else
0.0
end
end

function inv_fac_big(n::Integer)::BigFloat
if n < 0
throw(DomainError(n, "inv_fac not implemented for n < 0"))
elseif issimplefloat(T)
if n + 1 <= length(INVERSE_FACTORIALS)
convert(T, INVERSE_FACTORIALS[n + 1])
else
zero(T)
end
else
BigFloat(inv(factorial(big(n))))
float(inv(factorial(big(n))))
end
end
Loading