Skip to content

Round scaled config values instead of truncating on cast - #1256

Open
gskjold wants to merge 1 commit into
mainfrom
fix/1251-round-scaled-config-values
Open

Round scaled config values instead of truncating on cast#1256
gskjold wants to merge 1 commit into
mainfrom
fix/1251-round-scaled-config-values

Conversation

@gskjold

@gskjold gskjold commented Sep 1, 2026

Copy link
Copy Markdown
Member

Reported in #1251: a price modifier of 0.0464 is stored as 0.0463, while 0.0463 and 0.0465 are kept.

Cause

User-entered decimals are stored as scaled integers, and the cast to integer truncates:

atof("0.0464")            = 0.046399999999999996…
        × 10000.0         = 463.99999999999994
(uint32_t)                = 463

This is not a float-vs-double problem. Sweeping all 999999 four-decimal values through the price modifier conversion:

conversion stored one unit too low
toFloat() * 10000 (≤ v2.5.5) 68886
toDouble() * 10000.0 (v2.5.6 → main) 68866
lround(toDouble() * 10000.0) 0

a733365 (#1133) barely moved the count — it only changed which values land just below the integer. 0.0464 happened to be correct with float and moved into the broken set in v2.5.6, which is why this surfaces now on v2.5.7.

Change

lround() at every scaling site rather than relying on the cast:

  • AmsWebServer::handleSave() — price modifier value, meter wattage/voltage/amperage/accumulated multipliers, Wi-Fi power, Vcc offset/multiplier/boot limit
  • AmsWebServer::modifyDayPlot() / modifyMonthPlot() — the day/month data editor
  • configFileParse() — the same fields on config-file restore. This path also still used toFloat() for the Vcc values and the price modifier, so a backup/restore re-applied the old truncation; those are now toDouble() too.

lround() rounds half away from zero, which is correct for the one signed field here (vccOffset).

Verification

  • 0.0464464; the full 999999-value sweep is exact for all of them.
  • pio run -e esp32 and pio run -e esp8266 both build. ESP8266 grows 208 bytes and stays at 98.0% flash (20973 bytes free).

🤖 Generated with Claude Code

User-entered decimals are stored as scaled integers, but the cast from
double to integer truncates. atof("0.0464") is 0.046399999999999996, so
* 10000.0 yields 463.99999999999994 and the price modifier is stored as
463 instead of 464.

68866 of the 999999 possible four-decimal price modifier values are
stored one unit too low this way. Switching from float to double in
a733365 barely moved that count (68886 before, 68866 after); it only
changed which values land just below the integer, and 0.0464 moved into
the broken set.

Round with lround() at every scaling site instead: price modifiers,
meter multipliers, Wi-Fi power, Vcc offset/multiplier/boot limit and the
day/month plot editor, both in the web form handler and in the config
file parser. The config file parser also still used toFloat() for the
Vcc values and the price modifier, so it kept the old truncation on
backup restore.

Reported in #1251

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: ee6d73c

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

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.

1 participant