Skip to content

Commit

Permalink
Fix factorial of 0
Browse files Browse the repository at this point in the history
closes #186
  • Loading branch information
sadellie committed Feb 11, 2024
1 parent 5a7fd41 commit ebb5fc5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ internal fun BigDecimal.factorial(): BigDecimal {
if (this < BigDecimal.ZERO) throw ExpressionException.FactorialCalculation()
if (this > maxFactorial) throw ExpressionException.TooBig()

if (this.compareTo(BigDecimal.ZERO) == 0) return BigDecimal.ONE

var expr = this
for (i in 1 until this.toInt()) {
expr *= BigDecimal(i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,7 @@ class ExpressionSimpleTest {

@Test
fun expression28() = assertExpr("e", "2.7182818285")

@Test
fun expression29() = assertExpr("0!", "1")
}

0 comments on commit ebb5fc5

Please sign in to comment.