Skip to content

Fix encode_phys precision loss for int64 with factor=1#648

Open
bizfsc wants to merge 1 commit intocanopen-python:masterfrom
bizfsc:fix/encode-phys-int64-precision
Open

Fix encode_phys precision loss for int64 with factor=1#648
bizfsc wants to merge 1 commit intocanopen-python:masterfrom
bizfsc:fix/encode-phys-int64-precision

Conversation

@bizfsc
Copy link
Copy Markdown
Contributor

@bizfsc bizfsc commented Apr 29, 2026

Skip the float division in encode_phys when factor is 1 (the default). Previously, value /= self.factor always performed float division, causing up to 10 bits of precision loss for large int64 values (e.g. 0x55554444AAAABBBB became 0x55554444AAAABC00).

Use != 1 for the comparison since we are comparing values, not identity. Also clean up the redundant int(round(...)) to just round(), since round() already returns int when ndigits is omitted.

Add unit tests covering:

  • int64 roundtrip with factor=1 (the original bug)
  • int type preservation with factor=1
  • rounding with integer factor != 1 (existing behavior preserved)
  • float factor (existing behavior preserved)

Based on the discussion in #611.

Skip the float division in encode_phys when factor is 1 (the default).
Previously, `value /= self.factor` always performed float division,
causing up to 10 bits of precision loss for large int64 values
(e.g. 0x55554444AAAABBBB became 0x55554444AAAABC00).

Use `!= 1` for the comparison since we are comparing values, not
identity. Also clean up the redundant int(round(...)) to just round(),
since round() already returns int when ndigits is omitted.

Add unit tests covering:
- int64 roundtrip with factor=1 (the original bug)
- int type preservation with factor=1
- rounding with integer factor != 1 (existing behavior preserved)
- float factor (existing behavior preserved)

Based on the discussion in canopen-python#611.
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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