From 4092da0525e70e8bebb0b6c38ca67263002b914f Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 29 Jan 2021 22:21:55 +0100 Subject: [PATCH] Additional unit tests for statistical functions, with a fix to ordering for RANK() (#1813) * Additional unit tests for statistical functions, with a fix to ordering for RANK() --- .../Calculation/Statistical.php | 8 ++- .../Functions/Statistical/PercentRankTest.php | 34 ++++++++++++ .../Functions/Statistical/PercentileTest.php | 31 +++++++++++ .../Functions/Statistical/QuartileTest.php | 31 +++++++++++ .../Functions/Statistical/RankTest.php | 34 ++++++++++++ .../Calculation/Statistical/PERCENTILE.php | 28 ++++++++++ .../Calculation/Statistical/PERCENTRANK.php | 52 +++++++++++++++++++ .../data/Calculation/Statistical/QUARTILE.php | 40 ++++++++++++++ tests/data/Calculation/Statistical/RANK.php | 31 +++++++++++ 9 files changed, 287 insertions(+), 2 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/PercentRankTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/PercentileTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/QuartileTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/RankTest.php create mode 100644 tests/data/Calculation/Statistical/PERCENTILE.php create mode 100644 tests/data/Calculation/Statistical/PERCENTRANK.php create mode 100644 tests/data/Calculation/Statistical/QUARTILE.php create mode 100644 tests/data/Calculation/Statistical/RANK.php diff --git a/src/PhpSpreadsheet/Calculation/Statistical.php b/src/PhpSpreadsheet/Calculation/Statistical.php index 19f40f2d..52234f5a 100644 --- a/src/PhpSpreadsheet/Calculation/Statistical.php +++ b/src/PhpSpreadsheet/Calculation/Statistical.php @@ -2868,6 +2868,9 @@ class Statistical * PERCENTRANK. * * Returns the rank of a value in a data set as a percentage of the data set. + * Note that the returned rank is simply rounded to the appropriate significant digits, + * rather than floored (as MS Excel), so value 3 for a value set of 1, 2, 3, 4 will return + * 0.667 rather than 0.666 * * @param float[] $valueSet An array of, or a reference to, a list of numbers * @param int $value the number whose rank you want to find @@ -3037,10 +3040,11 @@ class Statistical } if ($order == 0) { - rsort($valueSet, SORT_NUMERIC); - } else { sort($valueSet, SORT_NUMERIC); + } else { + rsort($valueSet, SORT_NUMERIC); } + $pos = array_search($value, $valueSet); if ($pos === false) { return Functions::NA(); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/PercentRankTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/PercentRankTest.php new file mode 100644 index 00000000..71eff7ac --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/PercentRankTest.php @@ -0,0 +1,34 @@ +