You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FactDouble.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula.functions;
  16. import org.apache.poi.ss.formula.OperationEvaluationContext;
  17. import org.apache.poi.ss.formula.eval.*;
  18. import java.math.BigInteger;
  19. import java.util.HashMap;
  20. /**
  21. * Implementation for Excel FACTDOUBLE() function.<p/>
  22. * <p/>
  23. * <b>Syntax</b>:<br/> <b>FACTDOUBLE </b>(<b>number</b>)<br/>
  24. * <p/>
  25. * Returns the double factorial of a number.
  26. * <p/>
  27. * Number is the value for which to return the double factorial. If number is not an integer, it is truncated.
  28. * <p/>
  29. * Remarks
  30. * <ul>
  31. * <li>If number is nonnumeric, FACTDOUBLE returns the #VALUE! error value.</li>
  32. * <li>If number is negative, FACTDOUBLE returns the #NUM! error value.</li>
  33. * </ul>
  34. * Use a cache for more speed of previously calculated factorial
  35. *
  36. * @author cedric dot walter @ gmail dot com
  37. */
  38. public class FactDouble extends Fixed1ArgFunction implements FreeRefFunction {
  39. public static final FreeRefFunction instance = new FactDouble();
  40. //Caching of previously calculated factorial for speed
  41. static HashMap<Integer, BigInteger> cache = new HashMap<Integer, BigInteger>();
  42. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) {
  43. int number;
  44. try {
  45. number = OperandResolver.coerceValueToInt(numberVE);
  46. } catch (EvaluationException e) {
  47. return ErrorEval.VALUE_INVALID;
  48. }
  49. if (number < 0) {
  50. return ErrorEval.NUM_ERROR;
  51. }
  52. return new NumberEval(factorial(number).longValue());
  53. }
  54. public static BigInteger factorial(int n) {
  55. if (n == 0 || n < 0) {
  56. return BigInteger.ONE;
  57. }
  58. if (cache.containsKey(n)) {
  59. return cache.get(n);
  60. }
  61. BigInteger result = BigInteger.valueOf(n).multiply(factorial(n - 2));
  62. cache.put(n, result);
  63. return result;
  64. }
  65. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  66. if (args.length != 1) {
  67. return ErrorEval.VALUE_INVALID;
  68. }
  69. return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
  70. }
  71. }