Skip to content

Commit 1a200c9

Browse files
authored
Fix float arithmetic in BLF reader (#1927)
* Fix float arithmetic in BLF reader Using the Python decimal module for fast correctly rounded decimal floating-point arithmetic when applying the timestamp factor. * Remove the allowed_timestamp_delta from the BLF UT Now that the BLF float arithmetic issue is fixed we no longer need to tweek the `allowed_timestamp_delta` in the BLF unit tests
1 parent 613c653 commit 1a200c9

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

can/io/blf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import struct
1818
import time
1919
import zlib
20+
from decimal import Decimal
2021
from typing import Any, BinaryIO, Generator, List, Optional, Tuple, Union, cast
2122

2223
from ..message import Message
@@ -264,8 +265,8 @@ def _parse_data(self, data):
264265
continue
265266

266267
# Calculate absolute timestamp in seconds
267-
factor = 1e-5 if flags == 1 else 1e-9
268-
timestamp = timestamp * factor + start_timestamp
268+
factor = Decimal("1e-5") if flags == 1 else Decimal("1e-9")
269+
timestamp = float(Decimal(timestamp) * factor) + start_timestamp
269270

270271
if obj_type in (CAN_MESSAGE, CAN_MESSAGE2):
271272
channel, flags, dlc, can_id, can_data = unpack_can_msg(data, pos)

test/logformats_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ def _setup_instance(self):
694694
check_fd=True,
695695
check_comments=False,
696696
test_append=True,
697-
allowed_timestamp_delta=1.0e-6,
698697
preserves_channel=False,
699698
adds_default_channel=0,
700699
)

0 commit comments

Comments
 (0)