[gnome-calculator] Bug708005-Fixed Decimal Factorial



commit 35ac5e95a871f95cc0659bd8cf2399bbbd03341d
Author: Rashid Khan <rashood khan gmail com>
Date:   Mon Dec 2 14:54:57 2013 +0530

    Bug708005-Fixed Decimal Factorial

 src/number.vala        |   18 +++++++++++++++---
 src/test-equation.vala |    6 +++++-
 2 files changed, 20 insertions(+), 4 deletions(-)
---
diff --git a/src/number.vala b/src/number.vala
index 4672e14..28ebc5c 100644
--- a/src/number.vala
+++ b/src/number.vala
@@ -1042,9 +1042,21 @@ public class Number
             return new Number.integer (1);
         if (!is_natural ())
         {
-            /* Translators: Error displayed when attempted take the factorial of a fractional number */
-            mperr (_("Factorial is only defined for natural numbers"));
-            return new Number.integer (0);
+
+             /* Factorial Not defined for Complex or for negative numbers */
+            if(is_negative () || is_complex ())
+            {
+                /* Translators: Error displayed when attempted take the factorial of a fractional number */
+                mperr (_("Factorial is not computed for given input"));
+                return new Number.integer (0);
+            }
+            
+            var val = to_double ();
+
+            /* Factorial(x) = Gamma(x+1) - This is the formula used to calculate Factorial.*/
+            var fact = Math.tgamma(val+1);
+
+            return new Number.double(fact);
         }
 
         /* Convert to integer - if couldn't be converted then the factorial would be too big anyway */
diff --git a/src/test-equation.vala b/src/test-equation.vala
index 914393b..480dbba 100644
--- a/src/test-equation.vala
+++ b/src/test-equation.vala
@@ -352,10 +352,14 @@ private void test_equations ()
     test ("1!", "1", 0);
     test ("5!", "120", 0);
     test ("69!", 
"171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000", 0);
-    test ("0.1!", "", ErrorCode.MP);
     test ("−1!", "−1", 0);
     test ("(−1)!", "", ErrorCode.MP);
     test ("−(1!)", "−1", 0);
+    test ("3.5!", "11.631728397", 0);
+    test ("(-2)!", "", ErrorCode.MP);
+    test ("(-10.9)!", "", ErrorCode.MP);
+    test ("12.5!", "1710542068.319572266", 0);
+    test ("(-10.0)!", "", ErrorCode.MP);
 
     /* Powers */
     test ("2²", "4", 0);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]