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.

TwoOperandNumericOperation.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.eval;
  16. import org.apache.poi.ss.formula.functions.ArrayFunction;
  17. import org.apache.poi.ss.formula.functions.Fixed2ArgFunction;
  18. import org.apache.poi.ss.formula.functions.Function;
  19. import org.apache.poi.ss.formula.functions.MatrixFunction.MutableValueCollector;
  20. import org.apache.poi.ss.formula.functions.MatrixFunction.TwoArrayArg;
  21. /**
  22. * @author Josh Micich
  23. */
  24. public abstract class TwoOperandNumericOperation extends Fixed2ArgFunction implements ArrayFunction {
  25. protected final double singleOperandEvaluate(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
  26. ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
  27. return OperandResolver.coerceValueToDouble(ve);
  28. }
  29. public ValueEval evaluateArray(ValueEval args[], int srcRowIndex, int srcColumnIndex) {
  30. if (args.length != 2) {
  31. return ErrorEval.VALUE_INVALID;
  32. }
  33. return new ArrayEval().evaluate(srcRowIndex, srcColumnIndex, args[0], args[1]);
  34. }
  35. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
  36. double result;
  37. try {
  38. double d0 = singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
  39. double d1 = singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
  40. result = evaluate(d0, d1);
  41. if (result == 0.0) { // this '==' matches +0.0 and -0.0
  42. // Excel converts -0.0 to +0.0 for '*', '/', '%', '+' and '^'
  43. if (!(this instanceof SubtractEvalClass)) {
  44. return NumberEval.ZERO;
  45. }
  46. }
  47. if (Double.isNaN(result) || Double.isInfinite(result)) {
  48. return ErrorEval.NUM_ERROR;
  49. }
  50. } catch (EvaluationException e) {
  51. return e.getErrorEval();
  52. }
  53. return new NumberEval(result);
  54. }
  55. protected abstract double evaluate(double d0, double d1) throws EvaluationException;
  56. private final class ArrayEval extends TwoArrayArg {
  57. private final MutableValueCollector instance = new MutableValueCollector(true, true);
  58. protected double[] collectValues(ValueEval arg) throws EvaluationException {
  59. return instance.collectValues(arg);
  60. }
  61. protected double[][] evaluate(double[][] d1, double[][] d2) throws IllegalArgumentException, EvaluationException {
  62. int width = (d1[0].length < d2[0].length) ? d1[0].length : d2[0].length;
  63. int height = (d1.length < d2.length) ? d1.length : d2.length;
  64. double result[][] = new double[height][width];
  65. for (int j = 0; j < height; j++) {
  66. for (int i = 0; i < width; i++) {
  67. result[j][i] = TwoOperandNumericOperation.this.evaluate(d1[j][i], d2[j][i]);
  68. }
  69. }
  70. return result;
  71. }
  72. }
  73. public static final Function AddEval = new TwoOperandNumericOperation() {
  74. protected double evaluate(double d0, double d1) {
  75. return d0+d1;
  76. }
  77. };
  78. public static final Function DivideEval = new TwoOperandNumericOperation() {
  79. protected double evaluate(double d0, double d1) throws EvaluationException {
  80. if (d1 == 0.0) {
  81. throw new EvaluationException(ErrorEval.DIV_ZERO);
  82. }
  83. return d0/d1;
  84. }
  85. };
  86. public static final Function MultiplyEval = new TwoOperandNumericOperation() {
  87. protected double evaluate(double d0, double d1) {
  88. return d0*d1;
  89. }
  90. };
  91. public static final Function PowerEval = new TwoOperandNumericOperation() {
  92. protected double evaluate(double d0, double d1) {
  93. return Math.pow(d0, d1);
  94. }
  95. };
  96. private static final class SubtractEvalClass extends TwoOperandNumericOperation {
  97. public SubtractEvalClass() {
  98. //
  99. }
  100. protected double evaluate(double d0, double d1) {
  101. return d0-d1;
  102. }
  103. }
  104. public static final Function SubtractEval = new SubtractEvalClass();
  105. }