import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
/**
}
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));
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);
}
}
}
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