From 801e06318fc3a5149fce666cc86ea95527490443 Mon Sep 17 00:00:00 2001 From: Kriyszig Date: Thu, 4 Jul 2019 10:51:04 +0530 Subject: [PATCH] Factorial doesn't accept negative numbers Previously factorial could accept negative number which isn't defined --- vlib/math/math.v | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vlib/math/math.v b/vlib/math/math.v index 5d45576073..543109c57a 100644 --- a/vlib/math/math.v +++ b/vlib/math/math.v @@ -223,9 +223,12 @@ pub fn trunc(a f64) f64 { // factorial calculates the factorial of the provided value. pub fn factorial(a int) i64 { + if a < 0 { + panic('factorial: Cannot find factorial of negative number') + } mut prod := 1 for i:= 0; i < a; i++ { - prod = prod * (i+1) + prod *= (i+1) } return prod }