Skip to content

fix(battery): round capacity instead of truncating it - #5398

Merged
Alexays merged 1 commit into
Alexays:masterfrom
ln-bodicherla:fix/battery-capacity-rounding
Oct 19, 2026
Merged

Alexays merged 1 commit into
Alexays:masterfrom
ln-bodicherla:fix/battery-capacity-rounding

Conversation

@ln-bodicherla

Copy link
Copy Markdown
Contributor

Fixes #5392.

The capacity computed from charge_now/charge_full or energy_now/energy_full used integer division, so it was truncated rather than rounded. With full-at that error gets scaled up. With the numbers from the issue (energy_now 45420000, energy_full 57120000):

  • the real charge is 79.52%, which was truncated to 79 (so the module showed 79%, not 80%)
  • with full-at: 80: 79 * 100 / 80 = 98.75, which shows as 99% and never reaches 100%

Rounding with std::lround gives 80, which shows 80% without full-at and 100% with full-at: 80.

Testing

  • Built and ran meson test --suite waybar in a container from Dockerfiles/debian (arm64): build OK, 3/3 tests pass, no warnings in battery.cpp
  • Checked the old and new expressions with the values from the issue: 79% / 99% before, 80% / 100% after
  • clang-format --dry-run --Werror is clean
  • Not tested on real battery hardware

The capacity computed from charge_now/charge_full or energy_now/energy_full
used integer division, so 79.5% read as 79%. With full-at that error gets
scaled up: a battery stopped at an 80% threshold showed 99% (79 * 100 / 80)
and never reached 100%.

Fixes Alexays#5392
@Alexays
Alexays merged commit ed0d8eb into Alexays:master Oct 19, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Battery capacity rounding error

2 participants