Skip to content
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

Fix float arithmetic in BLF reader #1927

Merged
merged 2 commits into from
Mar 6, 2025
Merged

Conversation

pierreluctg
Copy link
Collaborator

@pierreluctg pierreluctg commented Feb 26, 2025

Using the Python decimal module for fast correctly rounded decimal floating-point arithmetic when applying the timestamp factor.

The BLF unit tests are also updated to reflect this new accuracy in the read timestamps.

@pierreluctg pierreluctg added bug file-io about reading & writing to files labels Feb 26, 2025
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.

Using the Python decimal module for fast correctly rounded decimal floating-point arithmetic when applying the timestamp factor.
Now that the BLF float arithmetic issue is fixed we no longer need to  tweek the `allowed_timestamp_delta` in the BLF unit tests
@pierreluctg pierreluctg force-pushed the bfl-float-arithmetic-fix branch from a21f63e to 55b986f Compare March 4, 2025 13:56
@pierreluctg pierreluctg merged commit 1a200c9 into main Mar 6, 2025
64 checks passed
@mergify mergify bot deleted the bfl-float-arithmetic-fix branch March 6, 2025 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug file-io about reading & writing to files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants