From 072850fb96ad71d8232d631c947dd069bb011298 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Tue, 6 Feb 2024 23:15:52 +0800 Subject: [PATCH] findTopCellItem() handle underflow (f7) --- Meter.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Meter.c b/Meter.c index 4aabf1644..767cfcab2 100644 --- a/Meter.c +++ b/Meter.c @@ -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)) @@ -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;