Improve precision of Duration-float operations#150933
Improve precision of Duration-float operations#150933eggyal wants to merge 1 commit intorust-lang:mainfrom
Conversation
|
Reminder, once the PR becomes ready for a review, use |
5844308 to
adc30ec
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
cf585ba to
c7098b3
Compare
|
No, some corner cases are not quite right here. I'll add some more tests and push a fix. @rustbot author |
|
@rustbot ready |
…s, r=tgross35 Boundary tests for various Duration-float operations As requested in rust-lang#150933 (comment) r? tgross35
…s, r=tgross35 Boundary tests for various Duration-float operations As requested in rust-lang#150933 (comment) r? tgross35
Rollup merge of #153358 - eggyal:duration-flop-boundary-tests, r=tgross35 Boundary tests for various Duration-float operations As requested in #150933 (comment) r? tgross35
|
I don't expect it to tell us that much, but I think this works enough to kick off a crater run. @bors try |
This comment has been minimized.
This comment has been minimized.
Improve precision of Duration-float operations
|
@craterbot run mode=build-and-test |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
Should we run crater while our own tests are failing? Surely I should fix that first! |
|
The failure is just a changed panic string right? I don't expect anyone to be checking that |
|
We also were introducing new panics for some negative factors that previously yielded zero, which has been corrected in my latest push. @rustbot ready |
Rather than convert Duration to a float in order to multiply or divide by a floating point number, which entails a loss of precision, we instead represent both operands as u128 nanoseconds and perform integer arithmetic thereupon.
|
Once you get into floating-point arithmetic, some amount of precision loss is to be expected. I actually don't find it too surprising that multiplying by 1.0 would result in precision loss for very large durations. The main problem with the code in this PR is that it adds a huge amount of complexity for what is really an edge case. It would be more appropriate to just have a warning in the documentation which explains that this method may cause precision loss due to the use of floating-point arithmetic, even when multiplying with 1.0. @rfcbot close libs |
|
Team member @Amanieu has proposed to close this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
Footnotes
|
I agree on the complexity, but I do still think the issue needs fixing. The current implementation doesn't follow the basic principle of floating point arithmetic: Compute the exact result and round to the output type. It doesn't make sense that The complexity of this PR is unfortunate, but it's mostly due to the ad hoc implementation of scaling an integer by a float. It could be significantly cleaned up with a hypothetical |
|
If we want to keep the full |
View all comments
Rather than convert Duration to a float in order to multiply or divide by a floating point number, which entails a loss of precision, we instead represent both operands as u128 nanoseconds and perform integer arithmetic thereupon.
This improvement in precision affects some of the documented examples.
Given that these methods have been stabilised, is it a breaking change to affect their output? If so, this PR obviously cannot be merged as-is: we might instead need to create new methods for this more precise calculation (and possibly deprecate the existing ones).
Fixes #149794
r? libs-api