You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2022/05/23 13:12:41 UTC

svn commit: r1901167 - in /poi/trunk/poi/src: main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java main/java/org/apache/poi/ss/formula/functions/CeilingMath.java test/java/org/apache/poi/ss/formula/functions/TestCeilingMath.java

Author: fanningpj
Date: Mon May 23 13:12:41 2022
New Revision: 1901167

URL: http://svn.apache.org/viewvc?rev=1901167&view=rev
Log:
partial implementation CEILING.MATH function (still need to support optional 2nd param)

Added:
    poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/CeilingMath.java
      - copied, changed from r1901161, poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java
    poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestCeilingMath.java
      - copied, changed from r1901161, poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java
Modified:
    poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java

Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java?rev=1901167&r1=1901166&r2=1901167&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java Mon May 23 13:12:41 2022
@@ -79,6 +79,7 @@ public final class AnalysisToolPak imple
         r(m, "BIN2HEX", null);
         r(m, "BIN2OCT", null);
         r(m, "COMPLEX", Complex.instance);
+        r(m, "CEILING.MATH", CeilingMath.instance);
         r(m, "CONCAT", TextFunction.CONCAT);
         r(m, "CONVERT", null);
         r(m, "COUNTIFS", Countifs.instance);

Copied: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/CeilingMath.java (from r1901161, poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java)
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/CeilingMath.java?p2=poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/CeilingMath.java&p1=poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java&r1=1901161&r2=1901167&rev=1901167&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/CeilingMath.java Mon May 23 13:12:41 2022
@@ -17,7 +17,6 @@
 
 package org.apache.poi.ss.formula.functions;
 
-import org.apache.commons.math3.distribution.NormalDistribution;
 import org.apache.poi.ss.formula.OperationEvaluationContext;
 import org.apache.poi.ss.formula.eval.ErrorEval;
 import org.apache.poi.ss.formula.eval.EvaluationException;
@@ -26,60 +25,39 @@ import org.apache.poi.ss.formula.eval.Op
 import org.apache.poi.ss.formula.eval.ValueEval;
 
 /**
- * Implementation for Excel NORMDIST() and NORM.DIST() functions.
+ * Implementation for Excel CEILING.MATH() function.
  * <ul>
- *   <li>https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58</li>
- *   <li>https://support.microsoft.com/en-us/office/norm-dist-function-edb1cc14-a21c-4e53-839d-8082074c9f8d</li>
+ *   <li>https://support.microsoft.com/en-us/office/ceiling-math-function-80f95d2f-b499-4eee-9f16-f795a8e306c8</li>
  * </ul>
  */
-public final class NormDist extends Fixed4ArgFunction implements FreeRefFunction {
+public final class CeilingMath implements FreeRefFunction {
 
-    public static final NormDist instance = new NormDist();
+    public static final CeilingMath instance = new CeilingMath();
 
-    static double probability(double x, double mean, double stdev, boolean cumulative) {
-        NormalDistribution normalDistribution = new NormalDistribution(mean, stdev);
-        return cumulative ? normalDistribution.cumulativeProbability(x) : normalDistribution.density(x);
-    }
-
-    private NormDist() {}
+    private CeilingMath() {}
 
     @Override
-    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2,
-                              ValueEval arg3, ValueEval arg4) {
+    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
+        if (args.length == 0) {
+            return ErrorEval.VALUE_INVALID;
+        }
         try {
-            Double xval = evaluateValue(arg1, srcRowIndex, srcColumnIndex);
+            Double xval = evaluateValue(args[0], ec.getRowIndex(), ec.getColumnIndex());
             if (xval == null) {
-                return ErrorEval.VALUE_INVALID;
-            }
-            Double mean = evaluateValue(arg2, srcRowIndex, srcColumnIndex);
-            if (mean == null) {
-                return ErrorEval.VALUE_INVALID;
-            }
-            Double stdev = evaluateValue(arg3, srcRowIndex, srcColumnIndex);
-            if (stdev == null) {
-                return ErrorEval.VALUE_INVALID;
-            } else if (stdev.doubleValue() <= 0) {
                 return ErrorEval.NUM_ERROR;
             }
-            Boolean cumulative = OperandResolver.coerceValueToBoolean(arg4, false);
-            if (cumulative == null) {
-                return ErrorEval.VALUE_INVALID;
+            boolean roundNegativeNumsDown = false;
+            if (args.length > 2) {
+                Double arg2Val = evaluateValue(args[2], ec.getRowIndex(), ec.getColumnIndex());
+                roundNegativeNumsDown = arg2Val != null && arg2Val.doubleValue() < 0.0;
             }
-
-            return new NumberEval(probability(
-                    xval.doubleValue(), mean.doubleValue(), stdev.doubleValue(), cumulative.booleanValue()));
-        } catch (EvaluationException e) {
-            return e.getErrorEval();
-        }
-    }
-
-    @Override
-    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
-        if (args.length == 4) {
-            return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1], args[2], args[3]);
+            if (roundNegativeNumsDown && xval < 0.0) {
+                return new NumberEval(Math.floor(xval));
+            }
+            return new NumberEval(Math.ceil(xval));
+        } catch (EvaluationException evaluationException) {
+            return evaluationException.getErrorEval();
         }
-
-        return ErrorEval.VALUE_INVALID;
     }
 
     private static Double evaluateValue(ValueEval arg, int srcRowIndex, int srcColumnIndex) throws EvaluationException {

Copied: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestCeilingMath.java (from r1901161, poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java)
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestCeilingMath.java?p2=poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestCeilingMath.java&p1=poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java&r1=1901161&r2=1901167&rev=1901167&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestCeilingMath.java Mon May 23 13:12:41 2022
@@ -19,87 +19,60 @@ package org.apache.poi.ss.formula.functi
 
 import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+import org.apache.poi.hssf.usermodel.HSSFRow;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.formula.OperationEvaluationContext;
-import org.apache.poi.ss.formula.eval.BlankEval;
-import org.apache.poi.ss.formula.eval.BoolEval;
 import org.apache.poi.ss.formula.eval.ErrorEval;
-import org.apache.poi.ss.formula.eval.NumberEval;
-import org.apache.poi.ss.formula.eval.StringEval;
-import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.usermodel.FormulaError;
 import org.junit.jupiter.api.Test;
 
 import java.io.IOException;
 
-import static org.apache.poi.ss.util.Utils.addRow;
 import static org.apache.poi.ss.util.Utils.assertDouble;
-import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.apache.poi.ss.util.Utils.assertError;
 
 /**
- * Tests for {@link NormDist}
+ * Tests for {@link CeilingMath}
  */
-final class TestNormDist {
+final class TestCeilingMath {
 
     private static final OperationEvaluationContext ec = new OperationEvaluationContext(null, null, 0, 0, 0, null);
 
+    //https://support.microsoft.com/en-us/office/ceiling-math-function-80f95d2f-b499-4eee-9f16-f795a8e306c8
     @Test
-    void testBasic() {
-        confirmValue("42", "40", "1.5", true, 0.908788780274132);
-        confirmValue("42", "40", "1.5", false, 0.109340049783996);
-    }
-
-    @Test
-    void testInvalid() {
-        confirmInvalidError("A1","B2","C2", false);
+    void testMicrosoftExamples() throws IOException {
+        try (HSSFWorkbook wb = new HSSFWorkbook()) {
+            HSSFSheet sheet = wb.createSheet();
+            HSSFRow row = sheet.createRow(0);
+            HSSFCell cell = row.createCell(0);
+            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+            assertDouble(fe, cell, "CEILING.MATH(24.3,5)", 25.0, 0.00000000000001);
+            assertDouble(fe, cell, "CEILING.MATH(6.7)", 7.0, 0.00000000000001);
+            assertDouble(fe, cell, "CEILING.MATH(-8.1,2)", -8.0, 0.00000000000001);
+            assertDouble(fe, cell, "CEILING.MATH(-5.5,2,-1)", -6.0, 0.00000000000001);
+        }
     }
 
     @Test
-    void testNumError() {
-        confirmNumError("42","40","0", false);
-        confirmNumError("42","40","0", true);
-        confirmNumError("42","40","-0.1", false);
-        confirmNumError("42","40","-0.1", true);
+    void testInvalid() throws IOException {
+        try (HSSFWorkbook wb = new HSSFWorkbook()) {
+            HSSFSheet sheet = wb.createSheet();
+            HSSFRow row = sheet.createRow(0);
+            HSSFCell cell = row.createCell(0);
+            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+            assertError(fe, cell, "CEILING.MATH()", FormulaError.VALUE);
+        }
     }
 
-    //https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58
-    //https://support.microsoft.com/en-us/office/norm-dist-function-edb1cc14-a21c-4e53-839d-8082074c9f8d
     @Test
-    void testMicrosoftExample1() throws IOException {
+    void testNumError() throws IOException {
         try (HSSFWorkbook wb = new HSSFWorkbook()) {
             HSSFSheet sheet = wb.createSheet();
-            addRow(sheet, 0, "Data", "Description");
-            addRow(sheet, 1, 42, "Value for which you want the distribution");
-            addRow(sheet, 2, 40, "Arithmetic mean of the distribution");
-            addRow(sheet, 3, 1.5, "Standard deviation of the distribution");
+            HSSFRow row = sheet.createRow(0);
+            HSSFCell cell = row.createCell(0);
             HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
-            HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
-            assertDouble(fe, cell, "NORMDIST(A2,A3,A4,TRUE)", 0.908788780274132, 0.00000000000001);
-            assertDouble(fe, cell, "NORM.DIST(A2,A3,A4,TRUE)", 0.908788780274132, 0.00000000000001);
+            assertError(fe, cell, "CEILING.MATH(\"abc\")", FormulaError.NUM);
         }
     }
-
-    private static ValueEval invokeValue(String number1, String number2, String number3, boolean cumulative) {
-        ValueEval[] args = new ValueEval[] { new StringEval(number1), new StringEval(number2), new StringEval(number3), BoolEval.valueOf(cumulative)};
-        return NormDist.instance.evaluate(args, ec);
-    }
-
-    private static void confirmValue(String number1, String number2, String number3, boolean cumulative, double expected) {
-        ValueEval result = invokeValue(number1, number2, number3, cumulative);
-        assertEquals(NumberEval.class, result.getClass());
-        assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.00000000000001);
-    }
-
-    private static void confirmInvalidError(String number1, String number2, String number3, boolean cumulative) {
-        ValueEval result = invokeValue(number1, number2, number3, cumulative);
-        assertEquals(ErrorEval.class, result.getClass());
-        assertEquals(ErrorEval.VALUE_INVALID, result);
-    }
-
-    private static void confirmNumError(String number1, String number2, String number3, boolean cumulative) {
-        ValueEval result = invokeValue(number1, number2, number3, cumulative);
-        assertEquals(ErrorEval.class, result.getClass());
-        assertEquals(ErrorEval.NUM_ERROR, result);
-    }
-
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org