Skip to content

Commit

Permalink
findTopCellItem() handle underflow case
Browse files Browse the repository at this point in the history
  • Loading branch information
Explorer09 committed Feb 6, 2024
1 parent 92876af commit 0248930
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,17 @@ static uint8_t GraphMeterMode_findTopCellItem(const Meter* this, double scaledTo

double topPoint = (valueSum / scaledTotal) * (double)(int)graphHeight;
double area = (value / scaledTotal) * (double)(int)graphHeight;
assert(area >= 0.0); // "area" can be 0.0 when the division underflows

if (topPoint - (double)(int)topCell > 0.0) {
if (area > topPoint - (double)(int)topCell)
if (topPoint - (double)(int)topCell > 0.0 || (topPoint <= 0.0 && topCell < 1)) {
// If the division underflows (topPoint <= 0.0 && topCell < 1), that
// item still counts as part of the top cell ("area" would be 0.0 in
// this case).

// Cut the excess area
if (area > topPoint - (double)(int)topCell) {
area = topPoint - (double)(int)topCell;
}
assert(area >= 0.0);

// Find the item that occupies the largest area of the top cell.
// Favor item with higher index in case of a tie.
Expand Down

0 comments on commit 0248930

Please sign in to comment.