# Factorial(big(-1))==0, bug or expected behaviour?

**URL:** <https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 27, 2023, 8:00am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298 "2023-09-27T08:00:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [September 27, 2023, 8:00am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298/1 "2023-09-27T08:00:12Z")

</div>

I noticed the following behaviour on my system (julia 1.8): the `factorial` of negative integers `x` disagrees when `x` is `Int` or `BigInt`. Is this a bug or expected behaviour? I also compare to the `gamma` function below (from `SpecialFunctions`).

```julia
x = -1
factorial(big(x)) # gives 0 as result (???)
factorial(x) # ERROR: DomainError with -1

using SpecialFunctions
gamma(x+1) # Inf
gamma(big(x+1)) # Inf

# expected behaviour for a positive integer
x = 5
factorial(big(x)) # 120
factorial(x) # 120
gamma(x+1) # 120.0
gamma(big(x+1)) # 120.0

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 27, 2023, 8:27am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298/2 "2023-09-27T08:27:30Z")

</div>

It definitely seems intentional given the source code `factorial(x::BigInt) = isneg(x) ? BigInt(0) : MPZ.fac_ui(x)` but I can’t imagine why.

The code as it is was written in [this commit](https://github.com/JuliaLang/julia/pull/21654) to move things into a MPZ module and first shows up in the 0.7 release. However, the step of returning `BigInt(0)` for negative integers predates that, seems to go back as far as 0.1.

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [September 27, 2023, 9:45am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298/3 "2023-09-27T09:45:16Z")

</div>

One can view this factorial as an empty product, in which case it makes sense to return 1.

_ **EDIT** _: Ah sorry, it returns 0. In this case, a way to make sense is to consider this is the product of the integers from 1 to n=-1, which is indeed 0.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 27, 2023, 9:50am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298/4 "2023-09-27T09:50:29Z")

</div>

```julia
julia> factorial(-1)
ERROR: DomainError with -1:
`n` must not be negative.
Stacktrace:
 [1] factorial_lookup
   @ ./combinatorics.jl:18 [inlined]
 [2] factorial(n::Int64)
   @ Base ./combinatorics.jl:27
 [3] top-level scope
   @ REPL[1]:1

julia> factorial(big(-1))
0

```

Surely, the same logic should apply to both

---

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [September 27, 2023, 10:12am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298/5 "2023-09-27T10:12:39Z")

</div>

hmm yes, it could, but in this definition `factorial(0)` should evaluate to `0`? This is in contrast to the common definition `factorial(0) = 1` which is also the current implementation.

In any case, I was surprised by `factorial(big(-1)) = 0` and would have expected the same error message as for `factorial(-1)`.

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [September 27, 2023, 10:15am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298/6 "2023-09-27T10:15:24Z")

</div>

@mlanghinrichs You’re right. Well seen. The result should be `NaN` or something like that.

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [September 27, 2023, 10:37am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298/7 "2023-09-27T10:37:03Z")

</div>

Very interesting discussion: [https://mathoverflow.net/questions/10124/the-factorials-of-1-2-3](https://mathoverflow.net/questions/10124/the-factorials-of-1-2-3)

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 27, 2023, 11:17am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298/8 "2023-09-27T11:17:36Z")

</div>

It seem like a bug, and someone should likely make a PR for it to error.

It contradicts what you get from the Roman factorial, one extension for integers I never heard of:

> **[Roman Factorial -- from Wolfram MathWorld](https://mathworld.wolfram.com/RomanFactorial.html)**
>
> |\_n\]!={n! for n\>=0; ((-1)^(-n-1))/((-n-1)!) for n\<0. (1) The Roman factorial arises in the definition of the harmonic logarithm and Roman coefficient. It obeys the identities |\_n\]!=|\_n\]|\_n-1\]! (2) ...

It also contradicts the gamma function (for nonintegers), that i.e. also contradicts Roman factorials.

Since we have the gamma function available (not in Base), there’s no need to have that extension, and the output of the Roman factorial are no longer integers for n \<= -2 so it’s not a type stable function, and it seems odd to add that extension just down to -2, i.e. for only two more values -1 and -2…

---

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [September 28, 2023, 8:50am UTC](https://discourse.julialang.org/t/factorial-big-1-0-bug-or-expected-behaviour/104298/9 "2023-09-28T08:50:23Z")

</div>

I reported this issue on GitHub as a potential bug (see [here](https://github.com/JuliaLang/julia/issues/51488)).
