I need a good factorial function. The one I have written here works entirely, except when n gets far too large. This is for a calculator app, and I can return 0 / 0 for values that cannot be factorialed because I have an error checker that will declare that as impossible. However performing the function on a very large number crashes the app. I can't use a range operator because my types are doubles.
func factorial(n: Double) -> Double {
if n >= 0 {
return n == 0 ? 1 : n * self.factorial(n - 1)
} else {
return 0 / 0
}
}
What is the best way to do this?