Skip to content

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

Merged
friederschueler merged 3 commits intocanopen-python:masterfrom
bizfsc:fix/encode-phys-int64-precision
May 6, 2026
Merged

Fix encode_phys precision loss for int64 with factor=1#648
friederschueler merged 3 commits intocanopen-python:masterfrom
bizfsc:fix/encode-phys-int64-precision

Conversation

@bizfsc
Copy link
Copy Markdown
Collaborator

@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

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
canopen/objectdictionary/__init__.py 50.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Member

@acolomb acolomb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight changes to the tests applied. Including one additional test to ensure a float factor always promotes the result to float (when decoding).

@friederschueler friederschueler merged commit 5dd8e6f into canopen-python:master May 6, 2026
3 of 5 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.

3 participants