8000 Fix float arithmetic in BLF reader by pierreluctg · Pull Request #1927 · hardbyte/python-can · GitHub
[go: up one dir, main page]

Skip to content

Fix float arithmetic in BLF reader #1927

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

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions can/io/blf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import struct
import time
import zlib
from decimal import Decimal
from typing import Any, BinaryIO, Generator, List, Optional, Tuple, Union, cast

from ..message import Message
Expand Down Expand Up @@ -264,8 +265,8 @@ def _parse_data(self, data):
continue

# Calculate absolute timestamp in seconds
factor = 1e-5 if flags == 1 else 1e-9
timestamp = timestamp * factor + start_timestamp
factor = Decimal("1e-5") if flags == 1 else Decimal("1e-9")
timestamp = float(Decimal(timestamp) * factor) + start_timestamp
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that can be achieved without Decimal:

            factor = 100_000 if flags == 1 else 1_000_000_000
            timestamp = (timestamp + round(start_timestamp * factor)) / factor

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree that we can achieve it without Decimal.
But what is the downside of using Decimal?

IMO, using the Python built-in module Decimal makes it very obvious what we are doing here.


if obj_type in (CAN_MESSAGE, CAN_MESSAGE2):
channel, flags, dlc, can_id, can_data = unpack_can_msg(data, pos)
Expand Down
1 change: 0 additions & 1 deletion test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,6 @@ def _setup_instance(self):
check_fd=True,
check_comments=False,
test_append=True,
allowed_timestamp_delta=1.0e-6,
preserves_channel=False,
adds_default_channel=0,
)
Expand Down
Loading
0