Skip to content

Commit

Permalink
[statistics] Lower sumOfValuesBetween precision
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoNumworks committed Sep 2, 2022
1 parent ab7fe3f commit d0492d2
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions apps/statistics/store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,20 +471,23 @@ bool Store::updateSeries(int series, bool delayUpdate) {
}

double Store::sumOfValuesBetween(int series, double x1, double x2, bool strictUpperBound) const {
/* Use roughly_equal to handle impossible double representations such as
* 12.11 being 12.109999999999999 or 12.110000000000001. */
if (!seriesIsValid(series)) {
return NAN;
}
/* Use roughly_equal to handle impossible double representations such as
* 12.11 being 12.109999999999999 or 12.110000000000001. The precision we use
* must be higher than 1e-14 (max number of significant digits) but having it
* higher than DBL_EPSILON wouldn't be effective. */
constexpr static double k_precision = 1e-15;
double result = 0;
int numberOfPairs = numberOfPairsOfSeries(series);
for (int k = 0; k < numberOfPairs; k++) {
int sortedIndex = valueIndexAtSortedIndex(series, k);
double value = get(series, 0, sortedIndex);
if (value > x2 || (strictUpperBound && Poincare::Helpers::RelativelyEqual<double>(value, x2, DBL_EPSILON))) {
if (value > x2 || (strictUpperBound && Poincare::Helpers::RelativelyEqual<double>(value, x2, k_precision))) {
break;
}
if (value >= x1 || Poincare::Helpers::RelativelyEqual<double>(value, x1, DBL_EPSILON)) {
if (value >= x1 || Poincare::Helpers::RelativelyEqual<double>(value, x1, k_precision)) {
result += get(series, 1, sortedIndex);
}
}
Expand Down

0 comments on commit d0492d2

Please sign in to comment.