Author: cedricwalter
Date: Thu Oct 10 10:10:54 2013
New Revision: 1530907

URL: http://svn.apache.org/r1530907
Log:
Bug 55058: patch for missing function FactDouble
Returns the double factorial of a number: FACTDOUBLE(number)

Number     is the value for which to return the double factorial. If number is 
not an integer, it is truncated.

Remarks
If number is nonnumeric, FACTDOUBLE returns the #VALUE! error value.
If number is negative, FACTDOUBLE returns the #NUM! error value.

Has  a cache for more speed of previously calculated factorial
Add additional sanity check/integration test FormulaEvalTestData.xls

Added:
    poi/trunk/src/java/org/apache/poi/ss/formula/functions/FactDouble.java
    
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFactDoubleFunctionsFromSpreadsheet.java
    poi/trunk/test-data/spreadsheet/FactDoubleFunctionTestCaseData.xls   (with 
props)
Modified:
    poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
    poi/trunk/test-data/spreadsheet/FormulaEvalTestData.xls

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java?rev=1530907&r1=1530906&r2=1530907&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java 
(original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java Thu 
Oct 10 10:10:54 2013
@@ -101,7 +101,7 @@ public final class AnalysisToolPak imple
         r(m, "EOMONTH", null);
         r(m, "ERF", null);
         r(m, "ERFC", null);
-        r(m, "FACTDOUBLE", null);
+        r(m, "FACTDOUBLE", FactDouble.instance);
         r(m, "FVSCHEDULE", null);
         r(m, "GCD", null);
         r(m, "GESTEP", null);

Added: poi/trunk/src/java/org/apache/poi/ss/formula/functions/FactDouble.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/FactDouble.java?rev=1530907&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/FactDouble.java 
(added)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/FactDouble.java Thu 
Oct 10 10:10:54 2013
@@ -0,0 +1,86 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.*;
+
+import java.math.BigInteger;
+import java.util.HashMap;
+
+/**
+ * Implementation for Excel FACTDOUBLE() function.<p/>
+ * <p/>
+ * <b>Syntax</b>:<br/> <b>FACTDOUBLE  </b>(<b>number</b>)<br/>
+ * <p/>
+ * Returns the double factorial of a number.
+ * <p/>
+ * Number is the value for which to return the double factorial. If number is 
not an integer, it is truncated.
+ * <p/>
+ * Remarks
+ * <ul>
+ * <li>If number is nonnumeric, FACTDOUBLE returns the #VALUE! error 
value.</li>
+ * <li>If number is negative, FACTDOUBLE returns the #NUM! error value.</li>
+ * </ul>
+ * Use a cache for more speed of previously calculated factorial
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public class FactDouble extends Fixed1ArgFunction implements FreeRefFunction {
+
+    public static final FreeRefFunction instance = new FactDouble();
+
+    //Caching of previously calculated factorial for speed
+    static HashMap<Integer, BigInteger> cache = new HashMap<Integer, 
BigInteger>();
+
+    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval 
numberVE) {
+        int number;
+        try {
+            number = OperandResolver.coerceValueToInt(numberVE);
+        } catch (EvaluationException e) {
+            return ErrorEval.VALUE_INVALID;
+        }
+
+        if (number < 0) {
+            return ErrorEval.NUM_ERROR;
+        }
+
+        return new NumberEval(factorial(number).longValue());
+    }
+
+    public static BigInteger factorial(int n) {
+        if (n == 0 || n < 0) {
+            return BigInteger.ONE;
+        }
+
+        if (cache.containsKey(n))  {
+            return cache.get(n);
+        }
+
+        BigInteger result = BigInteger.valueOf(n).multiply(factorial(n - 2));
+        cache.put(n, result);
+        return result;
+    }
+
+    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) 
{
+        if (args.length != 1) {
+            return ErrorEval.VALUE_INVALID;
+        }
+        return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
+    }
+}

Added: 
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFactDoubleFunctionsFromSpreadsheet.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFactDoubleFunctionsFromSpreadsheet.java?rev=1530907&view=auto
==============================================================================
--- 
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFactDoubleFunctionsFromSpreadsheet.java
 (added)
+++ 
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFactDoubleFunctionsFromSpreadsheet.java
 Thu Oct 10 10:10:54 2013
@@ -0,0 +1,30 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+/**
+ * Tests FactDouble() as loaded from a test data spreadsheet.<p/>
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public class TestFactDoubleFunctionsFromSpreadsheet extends 
BaseTestFunctionsFromSpreadsheet {
+
+    protected String getFilename() {
+        return "FactDoubleFunctionTestCaseData.xls";
+    }
+}

Added: poi/trunk/test-data/spreadsheet/FactDoubleFunctionTestCaseData.xls
URL: 
http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/FactDoubleFunctionTestCaseData.xls?rev=1530907&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/FactDoubleFunctionTestCaseData.xls
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: poi/trunk/test-data/spreadsheet/FormulaEvalTestData.xls
URL: 
http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/FormulaEvalTestData.xls?rev=1530907&r1=1530906&r2=1530907&view=diff
==============================================================================
Binary files - no diff available.



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to