Skip to content

Commit 7e23c02

Browse files
authored
Fix float arithmetic in BLF reader
Using the Python decimal module for fast correctly rounded decimal floating-point arithmetic when applying the timestamp factor.
1 parent 319a9f2 commit 7e23c02

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

can/io/blf.py

+3-2
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)

0 commit comments

Comments
 (0)