-
-
Notifications
You must be signed in to change notification settings - Fork 99
Fix incorrenct indexing #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
WalkthroughImplements defensive fixes and indexing corrections in Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Caller
participant Pred as Predictive
Caller->>Pred: getOvershootAdjustMillis(params)
rect rgb(230,245,255)
note over Pred: compute local rate and guard early
Pred->>Pred: getRate(measurements, cutoff)
alt stop index underflow risk
note right of Pred: while (i > 0 && measurementTimes[i-1] > cutoff)
end
alt tiny variance (tdev2 < 1e-10)
Pred-->>Pred: return 0.0 (degenerate)
else alt non-positive slope
Pred-->>Pred: return 0.0 (non-positive volume)
end
end
alt rate < 1e-10
Pred-->>Caller: 0.0 (avoid division)
else
Pred-->>Caller: overshootMillis (uses cached rate)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. 📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/display/core/predictive.h (1)
4-5: Include what you use.This header uses
std::vectorandstd::powbut doesn’t include their headers. Avoid relying on transitive includes.#include <Arduino.h> +#include <vector> +#include <cmath>
🧹 Nitpick comments (4)
src/display/core/predictive.h (4)
45-47: Indexing fix is correct; avoid pow in the hot loop.Use precomputed deltas and multiplication to reduce floating-point overhead on MCU targets.
- tdev_vdev += (measurementTimes[j] - t_mean) * (measurements[j] - v_mean); - tdev2 += pow(measurementTimes[j] - t_mean, 2.0); + const double dt = measurementTimes[j] - t_mean; + const double dv = measurements[j] - v_mean; + tdev_vdev += dt * dv; + tdev2 += dt * dt;
48-53: Near-zero denominator guard is good; name the threshold.Minor: give the magic number a name for readability and easier tuning.
- // Prevent division by zero - if (tdev2 < 1e-10) { + // Prevent division by near-zero denominator (units: ms^2) + constexpr double kTdev2Epsilon = 1e-10; + if (tdev2 < kTdev2Epsilon) { return 0.0; }
54-56: Comment mismatch on units.The code returns volume/ms; the comment suggests seconds. Align the comment to avoid confusion.
- double volumePerMilliSecond = tdev_vdev / tdev2; // the slope (volume per millisecond) of the linear best fit - return volumePerMilliSecond > 0 ? volumePerMilliSecond : 0.0; // return 0 if it is not positive, convert to seconds + double volumePerMilliSecond = tdev_vdev / tdev2; // slope (volume per millisecond) of the linear best fit + return volumePerMilliSecond > 0 ? volumePerMilliSecond : 0.0; // return 0 if it is not positive (units: volume/ms)
62-70: Division-by-zero guard is correct; clarify negative overshoot behavior.If actual < expected, result is negative ms. Is that intended? Consider clamping to 0 for “overshoot” semantics.
double rate = getRate(measurementTimes.back()); // Prevent division by zero if (rate < 1e-10) { return 0.0; } - - return overshoot / rate; + // If actual <= expected, there is no overshoot to adjust. + if (overshoot <= 0.0) { + return 0.0; + } + return overshoot / rate;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
src/display/core/predictive.h(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: deploy
- GitHub Check: test
🔇 Additional comments (1)
src/display/core/predictive.h (1)
25-27: Good underflow safeguard in cutoff scan.The added
i > 0guard prevents size_t wraparound and out-of-bounds access when the window excludes all points. LGTM.



Summary by CodeRabbit