-
Notifications
You must be signed in to change notification settings - Fork 2
Fix flaky round_value property test #508
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
f64::EPSILONtolerance is too strict and causes test failures.The pipeline shows this test is still flaky:
round(37515032.691102825) = 37515032.69110283, round(37515032.69110283) = 37515032.69110284.f64::EPSILON(~2.2e-16) is the precision near 1.0, but for values around 3.7e7, the actual precision is roughly3.7e7 * 2.2e-16 ≈ 8e-9. Whenround_valuedivides by the factor, the result may not be exactly representable, and re-rounding can shift by 1 ULP—which is far larger thanf64::EPSILON.Use a tolerance proportional to the value's magnitude and the decimal precision being tested:
Proposed fix using relative tolerance
#[test] fn round_value_is_idempotent( amount in 0.0_f64..1.0e8, unit in arb_btc_unit(), ) { let once = unit.round_value(amount); let twice = unit.round_value(once); + // Tolerance must account for f64 representation limits at this magnitude. + // For 8 decimal places, 1e-7 provides margin for 1-ULP drift. + let tol = 1.0e-7_f64.max(once.abs() * 1.0e-14); prop_assert!( - (once - twice).abs() < f64::EPSILON, + (once - twice).abs() < tol, "round_value is not idempotent: round({amount}) = {once}, round({once}) = {twice}" ); }Alternatively, if strict idempotence is required, consider using fixed-point or decimal types for currency calculations.
📝 Committable suggestion
🧰 Tools
🪛 GitHub Actions: Build and Test
[error] 98-98: Assertion context for failing proptest 'currency::btc::tests::round_value_is_idempotent' (minimal failing input: amount=37515032.691102825, unit=BTC).
🤖 Prompt for AI Agents