aboutsummaryrefslogtreecommitdiffstats
path: root/poi
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-01-25 23:05:43 +0000
committerPJ Fanning <fanningpj@apache.org>2022-01-25 23:05:43 +0000
commit958e992032647dddeeff8d994812d910fb50213a (patch)
treef0e67b0584e8501e18a636597885a05809185c18 /poi
parent9989999916321c5bd4ad7b3f05106d0aa9a14aa0 (diff)
downloadpoi-958e992032647dddeeff8d994812d910fb50213a.tar.gz
poi-958e992032647dddeeff8d994812d910fb50213a.zip
normsdist function
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897477 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java2
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java2
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java8
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/NormSDist.java68
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java2
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormSDist.java87
6 files changed, 165 insertions, 4 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
index 0016b3af60..5e3e05cb36 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
@@ -149,6 +149,8 @@ public final class AnalysisToolPak implements UDFFinder {
r(m, "MULTINOMIAL", null);
r(m, "NETWORKDAYS", NetworkdaysFunction.instance);
r(m, "NOMINAL", null);
+ r(m, "NORM.DIST", NormDist.instance);
+ r(m, "NORM.S.DIST", NormSDist.instance);
r(m, "NUMBERVALUE", NumberValueFunction.instance);
r(m, "OCT2BIN", null);
r(m, "OCT2DEC", Oct2Dec.instance);
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java b/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java
index 23ddf3aac6..493a3fbe05 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java
@@ -273,7 +273,7 @@ public final class FunctionEval {
// 291: LOGINV
// 292: NEGBINOMDIST
retval[293] = NormDist.instance;
- // 294: NORMSDIST
+ retval[294] = NormSDist.instance;
// 295: NORMINV
// 296: NORMSINV
// 297: STANDARDIZE
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java
index 9a9ea03e02..32fd5bc300 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java
@@ -26,9 +26,11 @@ import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval;
/**
- * Implementation for Excel NORMDIST() function.<p>
-
- * https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58
+ * Implementation for Excel NORMDIST() and NORM.DIST() functions.<p>
+ * <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>
+ * </ul>
*/
public final class NormDist extends Fixed4ArgFunction implements FreeRefFunction {
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/NormSDist.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/NormSDist.java
new file mode 100644
index 0000000000..3be6721604
--- /dev/null
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/NormSDist.java
@@ -0,0 +1,68 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.EvaluationException;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.OperandResolver;
+import org.apache.poi.ss.formula.eval.ValueEval;
+
+/**
+ * Implementation for Excel NORMSDIST() function.<p>
+ * <ul>
+ * <li>https://support.microsoft.com/en-us/office/normsdist-function-463369ea-0345-445d-802a-4ff0d6ce7cac</li>
+ * <li>https://support.microsoft.com/en-us/office/norm-s-dist-function-1e787282-3832-4520-a9ae-bd2a8d99ba88</li>
+ * </ul>
+ */
+public final class NormSDist extends Fixed1ArgFunction implements FreeRefFunction {
+
+ public static final NormSDist instance = new NormSDist();
+
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1) {
+ try {
+ Double xval = evaluateValue(arg1, srcRowIndex, srcColumnIndex);
+ if (xval == null) {
+ return ErrorEval.VALUE_INVALID;
+ } else if (xval < 0) {
+ return ErrorEval.NUM_ERROR;
+ }
+
+ return new NumberEval(NormDist.probability(xval.doubleValue(), 0, 1, true));
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ }
+
+ @Override
+ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
+ if (args.length == 1) {
+ return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
+ }
+
+ return ErrorEval.VALUE_INVALID;
+ }
+
+ private static Double evaluateValue(ValueEval arg, int srcRowIndex, int srcColumnIndex) throws EvaluationException {
+ ValueEval veText = OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex);
+ String strText1 = OperandResolver.coerceValueToString(veText);
+ return OperandResolver.parseDouble(strText1);
+ }
+} \ No newline at end of file
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java
index 1202430183..2468558b98 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java
@@ -63,6 +63,7 @@ final class TestNormDist {
}
//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 {
try (HSSFWorkbook wb = new HSSFWorkbook()) {
@@ -74,6 +75,7 @@ final class TestNormDist {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
assertDouble(fe, cell, "NORMDIST(A2,A3,A4,TRUE)", 0.9087888, 0.000001);
+ assertDouble(fe, cell, "NORM.DIST(A2,A3,A4,TRUE)", 0.9087888, 0.000001);
}
}
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormSDist.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormSDist.java
new file mode 100644
index 0000000000..5fd209eba0
--- /dev/null
+++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormSDist.java
@@ -0,0 +1,87 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+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.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.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;
+
+/**
+ * Tests for {@link NormSDist}
+ */
+final class TestNormSDist {
+
+ private static final OperationEvaluationContext ec = new OperationEvaluationContext(null, null, 0, 0, 0, null);
+
+ @Test
+ void testBasic() {
+ confirmValue("1.333333", 0.908788726);
+ }
+
+ @Test
+ void testInvalid() {
+ confirmInvalidError("A1");
+ }
+
+ //https://support.microsoft.com/en-us/office/normsdist-function-463369ea-0345-445d-802a-4ff0d6ce7cac
+ //https://support.microsoft.com/en-us/office/norm-s-dist-function-1e787282-3832-4520-a9ae-bd2a8d99ba88
+ @Test
+ void testMicrosoftExample1() throws IOException {
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ HSSFSheet sheet = wb.createSheet();
+ HSSFRow row = sheet.createRow(0);
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ HSSFCell cell = row.createCell(0);
+ assertDouble(fe, cell, "NORMSDIST(1.333333)", 0.908788726, 0.000001);
+ assertDouble(fe, cell, "NORM.S.DIST(1.333333)", 0.908788726, 0.000001);
+ }
+ }
+
+ private static ValueEval invokeValue(String number1) {
+ ValueEval[] args = new ValueEval[] { new StringEval(number1)};
+ return NormSDist.instance.evaluate(args, ec);
+ }
+
+ private static void confirmValue(String number1, double expected) {
+ ValueEval result = invokeValue(number1);
+ assertEquals(NumberEval.class, result.getClass());
+ assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0000001);
+ }
+
+ private static void confirmInvalidError(String number1) {
+ ValueEval result = invokeValue(number1);
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.VALUE_INVALID, result);
+ }
+
+}