aboutsummaryrefslogtreecommitdiffstats
path: root/poi/src
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-10-22 09:03:46 +0000
committerPJ Fanning <fanningpj@apache.org>2021-10-22 09:03:46 +0000
commit80b852452f69778dc04da5bfa0e53d86420cec06 (patch)
treea1a4f33ac8ffe4fbede872e0ca2e4cdebf4b542d /poi/src
parent5d5778d4071bb90f01960cd58ab554494d26bd86 (diff)
downloadpoi-80b852452f69778dc04da5bfa0e53d86420cec06.tar.gz
poi-80b852452f69778dc04da5bfa0e53d86420cec06.zip
fix negative X on T.DIST.RT
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894469 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src')
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java1
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/TDistLt.java87
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/TDistRt.java3
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDistRt.java10
4 files changed, 96 insertions, 5 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 6647eb581a..8768cb6678 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
@@ -173,6 +173,7 @@ public final class AnalysisToolPak implements UDFFinder {
r(m, "TBILLEQ", null);
r(m, "TBILLPRICE", null);
r(m, "TBILLYIELD", null);
+ r(m, "T.DIST", TDistLt.instance);
r(m, "T.DIST.2T", TDist2t.instance);
r(m, "T.DIST.RT", TDistRt.instance);
r(m, "TEXTJOIN", TextJoinFunction.instance);
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/TDistLt.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/TDistLt.java
new file mode 100644
index 0000000000..e5f9b1438a
--- /dev/null
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/TDistLt.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.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.*;
+
+/**
+ * Implementation for Excel T.DIST() function.
+ * <p>
+ * <b>Syntax</b>:<br> <b>T.DIST </b>(<b>X</b>,<b>Deg_freedom</b>,<b>Cumulative</b>)<br>
+ * <p>
+ * Returns the Student's left-tailed t-distribution.
+ *
+ * The t-distribution is used in the hypothesis testing of small sample data sets.
+ * Use this function in place of a table of critical values for the t-distribution.
+ *
+ * <ul>
+ * <li>X Required. The numeric value at which to evaluate the distribution.</li>
+ * <li>Deg_freedom Required. An integer indicating the number of degrees of freedom.</li>
+ * <li>Cumulative Required. A logical value that determines the form of the function. If cumulative is TRUE,
+ * T.DIST returns the cumulative distribution function; if FALSE, it returns the probability density function.</li>
+ * </ul>
+ *
+ * <ul>
+ * <li>If any argument is non-numeric, T.DIST returns the #VALUE! error value.</li>
+ * <li>If Deg_freedom &lt; 1, T.DIST returns the #NUM! error value.</li>
+ * <li>The Deg_freedom argument is truncated to an integer.
+ * </ul>
+ *
+ * https://support.microsoft.com/en-us/office/t-dist-rt-function-20a30020-86f9-4b35-af1f-7ef6ae683eda
+ */
+public final class TDistLt extends Fixed3ArgFunction implements FreeRefFunction {
+
+ public static final TDistLt instance = new TDistLt();
+
+ @Override
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2, ValueEval arg3) {
+ try {
+ Double number1 = evaluateValue(arg1, srcRowIndex, srcColumnIndex);
+ if (number1 == null) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ Double number2 = evaluateValue(arg2, srcRowIndex, srcColumnIndex);
+ if (number2 == null) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ int degreesOfFreedom = number2.intValue();
+ if (degreesOfFreedom < 1) {
+ return ErrorEval.NUM_ERROR;
+ }
+ return new NumberEval(TDist.tdistOneTail(Math.abs(number1), degreesOfFreedom));
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ }
+
+ @Override
+ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
+ if (args.length == 3) {
+ return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1], args[2]);
+ }
+
+ 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/main/java/org/apache/poi/ss/formula/functions/TDistRt.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/TDistRt.java
index 971aa26423..6de5ac10b6 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/TDistRt.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/TDistRt.java
@@ -17,7 +17,6 @@
package org.apache.poi.ss.formula.functions;
-import org.apache.commons.math3.distribution.TDistribution;
import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.*;
@@ -63,7 +62,7 @@ public final class TDistRt extends Fixed2ArgFunction implements FreeRefFunction
if (degreesOfFreedom < 1) {
return ErrorEval.NUM_ERROR;
}
- return new NumberEval(TDist.tdistOneTail(Math.abs(number1), degreesOfFreedom));
+ return new NumberEval(TDist.tdistOneTail(number1, degreesOfFreedom));
} catch (EvaluationException e) {
return e.getErrorEval();
}
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDistRt.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDistRt.java
index d0bca918ae..1037bc436d 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDistRt.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDistRt.java
@@ -46,7 +46,7 @@ final class TestTDistRt {
confirmValue("5.968191467", "8", 0.00016754180265310392);
confirmValue("5.968191467", "8.2", 0.00016754180265310392);
confirmValue("5.968191467", "8.9", 0.00016754180265310392);
- confirmValue("-5.968191467", "8", 0.00016754180265310392);
+ confirmValue("-5.968191467", "8", 0.999832458, 0.01);
}
@Test
@@ -72,7 +72,7 @@ final class TestTDistRt {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
assertDouble(fe, cell, "T.DIST.RT(A2,A3)", 0.027322465, 0.01);
- assertDouble(fe, cell, "T.DIST.RT(-A2,A3)", 0.027322465, 0.01);
+ assertDouble(fe, cell, "T.DIST.RT(-A2,A3)", 0.972677535, 0.01);
}
}
@@ -82,9 +82,13 @@ final class TestTDistRt {
}
private static void confirmValue(String number1, String number2, double expected) {
+ confirmValue(number1, number2, expected, 0.0);
+ }
+
+ private static void confirmValue(String number1, String number2, double expected, double tolerance) {
ValueEval result = invokeValue(number1, number2);
assertEquals(NumberEval.class, result.getClass());
- assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0);
+ assertEquals(expected, ((NumberEval) result).getNumberValue(), tolerance);
}
private static void confirmInvalidError(String number1, String number2) {