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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.util.NumberToTextConverter;
  20. import java.math.BigDecimal;
  21. import java.math.MathContext;
  22. public abstract class TwoOperandNumericOperation extends Fixed2ArgFunction implements ArrayFunction {
  23. protected final double singleOperandEvaluate(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
  24. ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
  25. return OperandResolver.coerceValueToDouble(ve);
  26. }
  27. @Override
  28. public ValueEval evaluateArray(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  29. if (args.length != 2) {
  30. return ErrorEval.VALUE_INVALID;
  31. }
  32. //return new ArrayEval().evaluate(srcRowIndex, srcColumnIndex, args[0], args[1]);
  33. return evaluateTwoArrayArgs(args[0], args[1], srcRowIndex, srcColumnIndex,
  34. (vA, vB) -> {
  35. try {
  36. double d0 = OperandResolver.coerceValueToDouble(vA);
  37. double d1 = OperandResolver.coerceValueToDouble(vB);
  38. double result = evaluate(d0, d1);
  39. return new NumberEval(result);
  40. } catch (EvaluationException e){
  41. return e.getErrorEval();
  42. }
  43. });
  44. }
  45. @Override
  46. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
  47. double result;
  48. try {
  49. double d0 = singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
  50. double d1 = singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
  51. result = evaluate(d0, d1);
  52. if (result == 0.0) { // this '==' matches +0.0 and -0.0
  53. // Excel converts -0.0 to +0.0 for '*', '/', '%', '+' and '^'
  54. if (!(this instanceof SubtractEvalClass)) {
  55. return NumberEval.ZERO;
  56. }
  57. }
  58. if (Double.isNaN(result) || Double.isInfinite(result)) {
  59. return ErrorEval.NUM_ERROR;
  60. }
  61. } catch (EvaluationException e) {
  62. return e.getErrorEval();
  63. }
  64. return new NumberEval(result);
  65. }
  66. protected abstract double evaluate(double d0, double d1) throws EvaluationException;
  67. public static final Function AddEval = new TwoOperandNumericOperation() {
  68. @Override
  69. protected double evaluate(double d0, double d1) {
  70. return d0+d1;
  71. }
  72. };
  73. public static final Function DivideEval = new TwoOperandNumericOperation() {
  74. @Override
  75. protected double evaluate(double d0, double d1) throws EvaluationException {
  76. if (d1 == 0.0) {
  77. throw new EvaluationException(ErrorEval.DIV_ZERO);
  78. }
  79. BigDecimal bd0 = new BigDecimal(NumberToTextConverter.toText(d0));
  80. BigDecimal bd1 = new BigDecimal(NumberToTextConverter.toText(d1));
  81. return bd0.divide(bd1, MathContext.DECIMAL128).doubleValue();
  82. }
  83. };
  84. public static final Function MultiplyEval = new TwoOperandNumericOperation() {
  85. @Override
  86. protected double evaluate(double d0, double d1) {
  87. BigDecimal bd0 = new BigDecimal(NumberToTextConverter.toText(d0));
  88. BigDecimal bd1 = new BigDecimal(NumberToTextConverter.toText(d1));
  89. return bd0.multiply(bd1).doubleValue();
  90. }
  91. };
  92. public static final Function PowerEval = new TwoOperandNumericOperation() {
  93. @Override
  94. protected double evaluate(double d0, double d1) {
  95. if(d0 < 0 && Math.abs(d1) > 0.0 && Math.abs(d1) < 1.0) {
  96. return -1 * Math.pow(d0 * -1, d1);
  97. }
  98. return Math.pow(d0, d1);
  99. }
  100. };
  101. private static final class SubtractEvalClass extends TwoOperandNumericOperation {
  102. public SubtractEvalClass() {
  103. //
  104. }
  105. @Override
  106. protected double evaluate(double d0, double d1) {
  107. return d0-d1;
  108. }
  109. }
  110. public static final Function SubtractEval = new SubtractEvalClass();
  111. }