diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-08-07 18:36:12 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-08-07 18:36:12 +0000 |
commit | b1bf15e2b555bcf48a5248dd6c8a6e1b3de70310 (patch) | |
tree | a30ede01f8de391360c9d8cb7a593fb9aa6e8223 /poi | |
parent | 9b697eeb76d5adb1f4c287e8afe5e7b88560ff9a (diff) | |
download | poi-b1bf15e2b555bcf48a5248dd6c8a6e1b3de70310.tar.gz poi-b1bf15e2b555bcf48a5248dd6c8a6e1b3de70310.zip |
[bug-49202] add PERCENTRANK.EXC function
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1892088 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r-- | poi/src/main/java/org/apache/poi/ss/formula/atp/PercentRankExcFunction.java | 14 | ||||
-rw-r--r-- | poi/src/main/java/org/apache/poi/ss/formula/functions/PercentRank.java | 18 |
2 files changed, 13 insertions, 19 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/PercentRankExcFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/PercentRankExcFunction.java index bc4d887245..8391666615 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/PercentRankExcFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/PercentRankExcFunction.java @@ -25,7 +25,6 @@ import org.apache.poi.ss.formula.functions.PercentRank; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** @@ -132,12 +131,8 @@ final class PercentRankExcFunction implements FreeRefFunction { } if (!recurse || closestMatchBelow == x || closestMatchAbove == x) { int lessThanCount = 0; - int greaterThanCount = 0; - int matchesCount = 0; for (Double d : numbers) { if (d < x) lessThanCount++; - else if (d > x) greaterThanCount++; - else matchesCount++; } BigDecimal result = new BigDecimal((double)(lessThanCount + 1) / (double)(numbers.size() + 1)); return new NumberEval(PercentRank.round(result, significance, RoundingMode.DOWN)); @@ -150,13 +145,8 @@ final class PercentRankExcFunction implements FreeRefFunction { if (!(aboveRank instanceof NumberEval)) { return aboveRank; } - NumberEval below = (NumberEval)belowRank; - NumberEval above = (NumberEval)aboveRank; - double diff = closestMatchAbove - closestMatchBelow; - double pos = x - closestMatchBelow; - double rankDiff = above.getNumberValue() - below.getNumberValue(); - BigDecimal result = new BigDecimal(below.getNumberValue() + (rankDiff * (pos / diff))); - return new NumberEval(PercentRank.round(result, significance, RoundingMode.HALF_UP)); + return PercentRank.interpolate(x, closestMatchBelow, closestMatchAbove, + (NumberEval)belowRank, (NumberEval)aboveRank, significance); } } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/PercentRank.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/PercentRank.java index 0eecb75c07..10d67f7cc8 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/PercentRank.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/PercentRank.java @@ -133,17 +133,21 @@ public final class PercentRank implements Function { if (!(aboveRank instanceof NumberEval)) { return aboveRank; } - NumberEval below = (NumberEval)belowRank; - NumberEval above = (NumberEval)aboveRank; - double diff = closestMatchAbove - closestMatchBelow; - double pos = x - closestMatchBelow; - double rankDiff = above.getNumberValue() - below.getNumberValue(); - BigDecimal result = new BigDecimal(below.getNumberValue() + (rankDiff * (pos / diff))); - return new NumberEval(round(result, significance, RoundingMode.HALF_UP)); + return interpolate(x, closestMatchBelow, closestMatchAbove, (NumberEval)belowRank, (NumberEval)aboveRank, significance); } } @Internal + public static NumberEval interpolate(double x, double closestMatchBelow, double closestMatchAbove, + NumberEval belowRank, NumberEval aboveRank, int significance) { + double diff = closestMatchAbove - closestMatchBelow; + double pos = x - closestMatchBelow; + double rankDiff = aboveRank.getNumberValue() - belowRank.getNumberValue(); + BigDecimal result = new BigDecimal(belowRank.getNumberValue() + (rankDiff * (pos / diff))); + return new NumberEval(round(result, significance, RoundingMode.HALF_UP)); + } + + @Internal public static double round(BigDecimal bd, int significance, RoundingMode rounding) { //the rounding in https://support.microsoft.com/en-us/office/percentrank-function-f1b5836c-9619-4847-9fc9-080ec9024442 //is very inconsistent, this hodge podge of rounding modes is the only way to match Excel results |