diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-10-22 09:48:30 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-10-22 09:48:30 +0000 |
commit | 6290068f49b4fb3f294ca3b0c9054390eced9a8c (patch) | |
tree | f5cf87ccaea129a8aabc38f0791179057378c009 /poi/src | |
parent | 80b852452f69778dc04da5bfa0e53d86420cec06 (diff) | |
download | poi-6290068f49b4fb3f294ca3b0c9054390eced9a8c.tar.gz poi-6290068f49b4fb3f294ca3b0c9054390eced9a8c.zip |
get some T.DIST tests to work (still not right on cumulative=false)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894470 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src')
5 files changed, 115 insertions, 7 deletions
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 index e5f9b1438a..4d8adcd99e 100644 --- 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 @@ -17,6 +17,7 @@ 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.*; @@ -49,6 +50,16 @@ public final class TDistLt extends Fixed3ArgFunction implements FreeRefFunction public static final TDistLt instance = new TDistLt(); + private static double tdistCumulative(double x, int degreesOfFreedom) { + TDistribution tdist = new TDistribution(degreesOfFreedom); + return tdist.cumulativeProbability(x); + } + + private static double tdistDensity(double x, int degreesOfFreedom) { + TDistribution tdist = new TDistribution(degreesOfFreedom); + return tdist.density(x); + } + @Override public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2, ValueEval arg3) { try { @@ -64,7 +75,15 @@ public final class TDistLt extends Fixed3ArgFunction implements FreeRefFunction if (degreesOfFreedom < 1) { return ErrorEval.NUM_ERROR; } - return new NumberEval(TDist.tdistOneTail(Math.abs(number1), degreesOfFreedom)); + Boolean cumulativeFlag = evaluateBoolean(arg3, srcRowIndex, srcColumnIndex); + if (cumulativeFlag == null) { + return ErrorEval.VALUE_INVALID; + } + if (cumulativeFlag.booleanValue()) { + return new NumberEval(tdistCumulative(number1, degreesOfFreedom)); + } else { + return new NumberEval(tdistDensity(number1, degreesOfFreedom)); + } } catch (EvaluationException e) { return e.getErrorEval(); } @@ -84,4 +103,9 @@ public final class TDistLt extends Fixed3ArgFunction implements FreeRefFunction String strText1 = OperandResolver.coerceValueToString(veText); return OperandResolver.parseDouble(strText1); } + + private static Boolean evaluateBoolean(ValueEval arg, int srcRowIndex, int srcColumnIndex) throws EvaluationException { + ValueEval veText = OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex); + return OperandResolver.coerceValueToBoolean(veText, false); + } }
\ No newline at end of file diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist.java index ab9a3f9d3d..b22c83dea6 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist.java @@ -74,8 +74,8 @@ final class TestTDist { addRow(sheet, 2, 60, "Degrees of freedom"); HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100); - assertDouble(fe, cell, "TDIST(A2,A3,2)", 0.054644930, 0.01); - assertDouble(fe, cell, "TDIST(A2,A3,1)", 0.027322465, 0.01); + assertDouble(fe, cell, "TDIST(A2,A3,2)", 0.054644930, 0.000001); + assertDouble(fe, cell, "TDIST(A2,A3,1)", 0.027322465, 0.000001); } } diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist2t.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist2t.java index c624dad67b..026aff8dc1 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist2t.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist2t.java @@ -71,7 +71,7 @@ final class TestTDist2t { addRow(sheet, 2, 60, "Degrees of freedom"); HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100); - assertDouble(fe, cell, "T.DIST.2T(A2,A3)", 0.054644930, 0.01); + assertDouble(fe, cell, "T.DIST.2T(A2,A3)", 0.054644930, 0.000001); } } diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDistLt.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDistLt.java new file mode 100644 index 0000000000..1c03b02b43 --- /dev/null +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDistLt.java @@ -0,0 +1,84 @@ +/* ==================================================================== + 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.NumberEval; +import org.apache.poi.ss.formula.eval.StringEval; +import org.apache.poi.ss.formula.eval.ValueEval; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests for {@link TDistLt} + */ +final class TestTDistLt { + + private static final OperationEvaluationContext ec = new OperationEvaluationContext(null, null, 0, 0, 0, null); + + @Test + void testBasic() { + //https://support.microsoft.com/en-us/office/t-dist-rt-function-20a30020-86f9-4b35-af1f-7ef6ae683eda + confirmValue("60", "1", "TRUE", 0.99469533, 0.000001); + confirmValue("-60", "1", "TRUE", 0.005304674, 0.000001); + //confirmValue("60", "1", "FALSE", 0.00073691, 0.000001); + } + + @Test + void testInvalid() { + confirmInvalidError("A1","B2", "C3"); + confirmInvalidError("5.968191467","B2", "FALSE"); + confirmInvalidError("A1","8", "TRUE"); + confirmInvalidError("5.968191467","8", ""); + } + + @Test + void testNumError() { + confirmNumError("-5.968191467", "-8", "TRUE"); + } + + private static ValueEval invokeValue(String number1, String number2, String value3) { + ValueEval[] args = new ValueEval[] { new StringEval(number1), new StringEval(number2), new StringEval(value3) }; + return TDistLt.instance.evaluate(args, ec); + } + + private static void confirmValue(String number1, String number2, String value3, double expected) { + confirmValue(number1, number2, value3, expected, 0.0); + } + + private static void confirmValue(String number1, String number2, String value3, double expected, double tolerance) { + ValueEval result = invokeValue(number1, number2, value3); + assertEquals(NumberEval.class, result.getClass()); + assertEquals(expected, ((NumberEval) result).getNumberValue(), tolerance); + } + + private static void confirmInvalidError(String number1, String number2, String value3) { + ValueEval result = invokeValue(number1, number2, value3); + assertEquals(ErrorEval.class, result.getClass()); + assertEquals(ErrorEval.VALUE_INVALID, result); + } + + private static void confirmNumError(String number1, String number2, String value3) { + ValueEval result = invokeValue(number1, number2, value3); + assertEquals(ErrorEval.class, result.getClass()); + assertEquals(ErrorEval.NUM_ERROR, result); + } + +} 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 1037bc436d..df38666f44 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.999832458, 0.01); + confirmValue("-5.968191467", "8", 0.999832458, 0.000001); } @Test @@ -71,8 +71,8 @@ final class TestTDistRt { addRow(sheet, 2, 60, "Degrees of freedom"); 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.972677535, 0.01); + assertDouble(fe, cell, "T.DIST.RT(A2,A3)", 0.027322465, 0.000001); + assertDouble(fe, cell, "T.DIST.RT(-A2,A3)", 0.972677535, 0.000001); } } |