View | Details | Raw Unified | Return to bug 55058
Collapse All | Expand All

(-)poi/src/testcases/org/apache/poi/ss/formula/functions/TestFactDoubleFunctionsFromSpreadsheet.java (+31 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.formula.functions;
19
20
/**
21
 * Tests FactDouble() as loaded from a test data spreadsheet.<p/>
22
 *
23
 * @author cedric dot walter @ gmail dot com
24
 */
25
public class TestFactDoubleFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet {
26
27
    @Override
28
    protected String getFilename() {
29
        return "FactDoubleFunctionTestCaseData.xls";
30
    }
31
}
(-)poi/src/java/org/apache/poi/ss/formula/functions/FactDouble.java (+86 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.formula.functions;
19
20
import org.apache.poi.ss.formula.OperationEvaluationContext;
21
import org.apache.poi.ss.formula.eval.*;
22
23
import java.math.BigInteger;
24
import java.util.HashMap;
25
26
/**
27
 * Implementation for Excel FACTDOUBLE() function.<p/>
28
 * <p/>
29
 * <b>Syntax</b>:<br/> <b>FACTDOUBLE  </b>(<b>number</b>)<br/>
30
 * <p/>
31
 * Returns the double factorial of a number.
32
 * <p/>
33
 * Number is the value for which to return the double factorial. If number is not an integer, it is truncated.
34
 * <p/>
35
 * Remarks
36
 * <ul>
37
 * <li>If number is nonnumeric, FACTDOUBLE returns the #VALUE! error value.</li>
38
 * <li>If number is negative, FACTDOUBLE returns the #NUM! error value.</li>
39
 * </ul>
40
 * Use a cache for more speed of previously calculated factorial
41
 *
42
 * @author cedric dot walter @ gmail dot com
43
 */
44
public class FactDouble extends Fixed1ArgFunction implements FreeRefFunction {
45
46
    public static final FreeRefFunction instance = new FactDouble();
47
48
    //Caching of previously calculated factorial for speed
49
    static HashMap<Integer, BigInteger> cache = new HashMap<Integer, BigInteger>();
50
51
    @Override
52
    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) {
53
        int number = 0;
54
        try {
55
            number = OperandResolver.coerceValueToInt(numberVE);
56
        } catch (EvaluationException e) {
57
            return ErrorEval.VALUE_INVALID;
58
        }
59
60
        if (number < 0) {
61
            return ErrorEval.NUM_ERROR;
62
        }
63
64
        String value = String.valueOf(factorial(number));
65
        return new StringEval(value);
66
    }
67
68
    public static BigInteger factorial(int n) {
69
        BigInteger result;
70
        if (n == 0 || n < 0) {
71
            return BigInteger.ONE;
72
        }
73
74
        result = BigInteger.valueOf(n).multiply(factorial(n - 2));
75
        cache.put(n, result);
76
        return result;
77
    }
78
79
    @Override
80
    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
81
        if (args.length != 1) {
82
            return ErrorEval.VALUE_INVALID;
83
        }
84
        return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
85
    }
86
}
(-)poi/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java (-1 / +1 lines)
Lines 103-109 Link Here
103
        r(m, "EOMONTH", null);
103
        r(m, "EOMONTH", null);
104
        r(m, "ERF", null);
104
        r(m, "ERF", null);
105
        r(m, "ERFC", null);
105
        r(m, "ERFC", null);
106
        r(m, "FACTDOUBLE", null);
106
        r(m, "FACTDOUBLE", FactDouble.instance);
107
        r(m, "FVSCHEDULE", null);
107
        r(m, "FVSCHEDULE", null);
108
        r(m, "GCD", null);
108
        r(m, "GCD", null);
109
        r(m, "GESTEP", null);
109
        r(m, "GESTEP", null);

Return to bug 55058