Skip to content

Commit

Permalink
findTopCellItem() handle underflow (f7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Explorer09 committed Feb 7, 2024
1 parent 30f1565 commit 072850f
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ static uint8_t GraphMeterMode_findTopCellItem(const Meter* this, double scaledTo
assert(topCell < graphHeight);

double valueSum = 0.0;
bool hasArea = false;
double maxValue = 0.0;
uint8_t topCellItem = this->curItems - 1;
bool hasPrevArea = false;
double maxValue = 0.0;
for (uint8_t i = 0; i < this->curItems && valueSum < DBL_MAX; i++) {
double value = this->values[i];
if (!isPositive(value))
Expand All @@ -412,38 +412,39 @@ static uint8_t GraphMeterMode_findTopCellItem(const Meter* this, double scaledTo

double area = (value / scaledTotal) * (double)(int)graphHeight;

// if (maxValue <= 0.0)
// if (topCellItem >= i)
if (topCellItem > i) {
if (topCellItem >= i) {
assert(!hasPrevArea);
assert(maxValue <= 0.0);

double topPoint = (valueSum / scaledTotal) * (double)(int)graphHeight;
assert(topPoint >= 0.0);

if (topCell > 0 && !(topPoint > (double)(int)topCell)) {
continue;
}
// If (topCell == 0), permit the cases where the division underflows
// (topPoint == 0.0).
// If (topCell == 0), permit the cases where the division underflows,
// i.e. "topPoint" and "area" both round to 0.0.

topCellItem = i;
if (area > topPoint - (double)(int)topCell) {
hasArea = true;
maxValue = topPoint - (double)(int)topCell;
hasPrevArea = true;
maxValue = topPoint - (double)(int)topCell;
} else {
maxValue = value;
}
topCellItem = i;
}
} else {
if (hasArea) {
if (hasPrevArea) {
if (area >= maxValue) {
hasArea = false;
hasPrevArea = false;
maxValue = value;
topCellItem = i;
}
}
} else {
if (value >= maxValue) {
maxValue = value;
topCellItem = i;
}
}
}
}
}
return topCellItem;
Expand Down

0 comments on commit 072850f

Please sign in to comment.