Skip to content

Commit

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

double valueSum = 0.0;
bool hasPrevArea = false;
bool maxValueIsArea = false;
double maxValue = 0.0;
uint8_t topCellItem = this->curItems - 1;
for (uint8_t i = 0; i < this->curItems && valueSum < DBL_MAX; i++) {
Expand All @@ -411,30 +411,28 @@ static uint8_t GraphMeterMode_findTopCellItem(const Meter* this, double scaledTo
// Favor item with higher index in case of a tie.

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

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

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

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

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

0 comments on commit 837b61b

Please sign in to comment.