Fix factorial of 0

closes #186
This commit is contained in:
Sad Ellie 2024-02-11 22:58:26 +03:00
parent 5a7fd41cf5
commit ebb5fc52e0
2 changed files with 5 additions and 0 deletions

View File

@ -93,6 +93,8 @@ internal fun BigDecimal.factorial(): BigDecimal {
if (this < BigDecimal.ZERO) throw ExpressionException.FactorialCalculation() if (this < BigDecimal.ZERO) throw ExpressionException.FactorialCalculation()
if (this > maxFactorial) throw ExpressionException.TooBig() if (this > maxFactorial) throw ExpressionException.TooBig()
if (this.compareTo(BigDecimal.ZERO) == 0) return BigDecimal.ONE
var expr = this var expr = this
for (i in 1 until this.toInt()) { for (i in 1 until this.toInt()) {
expr *= BigDecimal(i) expr *= BigDecimal(i)

View File

@ -108,4 +108,7 @@ class ExpressionSimpleTest {
@Test @Test
fun expression28() = assertExpr("e", "2.7182818285") fun expression28() = assertExpr("e", "2.7182818285")
@Test
fun expression29() = assertExpr("0!", "1")
} }