-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Use rounding in float to Duration conversion methods #96051
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
r? @nagisa |
cb476c8
to
2521fcf
Compare
92a8201
to
65c75b2
Compare
there is really no tool for rounding float ? we are force to do it at hand in Duration ? |
Truncating and/or correctly rounded conversions to/from some formats such as 32-bit or 64-bit integers are provided, usually by hardware nowadays. |
Well, another theoretical alternative is to migrate |
@newpavlov to be clear I never say we should do this, I just wondering if there was a tool to manipulate float to avoid doing such complex operation in the code of |
Nominating for a T-libs decision on: a) Do we want rounding after all? |
Reviewed in the @rust-lang/libs meeting. a) Yes, we want rounding. |
I don't believe there are any significant performance concerns here since we aren't able to use any hardware acceleration for the conversion either way, and I would be surprised if any ever showed up. The only concern with different tie resolution models I believe is just that one of them is just significantly more nasty to implement and understand. |
@bors r+ |
📌 Commit 06af3a6 has been approved by |
@bors r- |
@newpavlov: when approving, remember to do |
@bors r=nagisa,joshtriplett |
📌 Commit 6495963 has been approved by |
…piler-errors Rollup of 3 pull requests Successful merges: - rust-lang#96051 (Use rounding in float to Duration conversion methods) - rust-lang#97066 (rustdoc: Remove `ItemFragment(Kind)`) - rust-lang#97436 (Update `triagebot.toml` for macos ping group) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Re-enable tests that were disabled due to floating-point arithmetic changes in the rust standard library. The behavior of floating-point arithmetic went back to rounding vs truncating in rust-lang/rust#96051, so it's safe to enable these tests again. A new bug has been filed to track the work to avoid doing floating-point arithmetic. The complication is that `StepRng` is behaving in unexpected ways and preventing migration of the existing unit tests. Fixed: 92923 Bug: 102199 Change-Id: I3372193a8d59f1bc0d816e9921056bb85fb290d4 Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/643581 Reviewed-by: Jake Fried <[email protected]> Reviewed-by: Ghanan Gowripalan <[email protected]> Commit-Queue: Tony Gong <[email protected]>
- Rust version 1.63 changed how they convert float to Durations. Previously they truncated, now they round - https://github.com/rust-lang/rust/releases/tag/1.63.0 - rust-lang/rust#96051 - 'Rounding is now used when converting a float to a Duration. The converted duration can differ slightly from what it was.'
* Updated dependencies Cargo and npm * Fixed clippy and format warnings * Fixed tests that are failing in Rust 1.63 - Rust version 1.63 changed how they convert float to Durations. Previously they truncated, now they round - https://github.com/rust-lang/rust/releases/tag/1.63.0 - rust-lang/rust#96051 - 'Rounding is now used when converting a float to a Duration. The converted duration can differ slightly from what it was.' * Fixed the names of some variables to match what they are
…on-try-from-secs-float, r=dtolnay Stabilize `duration_checked_float` ## Stabilization Report This stabilization report is for a stabilization of `duration_checked_float`, tracking issue: rust-lang#83400. ### Implementation History - rust-lang#82179 - rust-lang#90247 - rust-lang#96051 - Changed error type to `FromFloatSecsError` in rust-lang#90247 - rust-lang#96051 changes the rounding mode to round-to-nearest instead of truncate. ## API Summary This stabilization report proposes the following API to be stabilized in `core`, along with their re-exports in `std`: ```rust // core::time impl Duration { pub const fn try_from_secs_f32(secs: f32) -> Result<Duration, TryFromFloatSecsError>; pub const fn try_from_secs_f64(secs: f64) -> Result<Duration, TryFromFloatSecsError>; } #[derive(Debug, Clone, PartialEq, Eq)] pub struct TryFromFloatSecsError { ... } impl core::fmt::Display for TryFromFloatSecsError { ... } impl core::error::Error for TryFromFloatSecsError { ... } ``` These functions are made const unstable under `duration_consts_float`, tracking issue rust-lang#72440. There is an open question in the tracking issue around what the error type should be called which I was hoping to resolve in the context of an FCP. In this stabilization PR, I have altered the name of the error type to `TryFromFloatSecsError`. In my opinion, the error type shares the name of the method (adjusted to accommodate both types of floats), which is consistent with other error types in `core`, `alloc` and `std` like `TryReserveError` and `TryFromIntError`. ## Experience Report Code such as this is ready to be converted to a checked API to ensure it is panic free: ```rust impl Time { pub fn checked_add_f64(&self, seconds: f64) -> Result<Self, TimeError> { // Fail safely during `f64` conversion to duration if seconds.is_nan() || seconds.is_infinite() { return Err(TzOutOfRangeError::new().into()); } if seconds.is_sign_positive() { self.checked_add(Duration::from_secs_f64(seconds)) } else { self.checked_sub(Duration::from_secs_f64(-seconds)) } } } ``` See: artichoke/artichoke#2194. `@rustbot` label +T-libs-api -T-libs cc `@mbartlett21`
…on-try-from-secs-float, r=dtolnay Stabilize `duration_checked_float` ## Stabilization Report This stabilization report is for a stabilization of `duration_checked_float`, tracking issue: rust-lang#83400. ### Implementation History - rust-lang#82179 - rust-lang#90247 - rust-lang#96051 - Changed error type to `FromFloatSecsError` in rust-lang#90247 - rust-lang#96051 changes the rounding mode to round-to-nearest instead of truncate. ## API Summary This stabilization report proposes the following API to be stabilized in `core`, along with their re-exports in `std`: ```rust // core::time impl Duration { pub const fn try_from_secs_f32(secs: f32) -> Result<Duration, TryFromFloatSecsError>; pub const fn try_from_secs_f64(secs: f64) -> Result<Duration, TryFromFloatSecsError>; } #[derive(Debug, Clone, PartialEq, Eq)] pub struct TryFromFloatSecsError { ... } impl core::fmt::Display for TryFromFloatSecsError { ... } impl core::error::Error for TryFromFloatSecsError { ... } ``` These functions are made const unstable under `duration_consts_float`, tracking issue rust-lang#72440. There is an open question in the tracking issue around what the error type should be called which I was hoping to resolve in the context of an FCP. In this stabilization PR, I have altered the name of the error type to `TryFromFloatSecsError`. In my opinion, the error type shares the name of the method (adjusted to accommodate both types of floats), which is consistent with other error types in `core`, `alloc` and `std` like `TryReserveError` and `TryFromIntError`. ## Experience Report Code such as this is ready to be converted to a checked API to ensure it is panic free: ```rust impl Time { pub fn checked_add_f64(&self, seconds: f64) -> Result<Self, TimeError> { // Fail safely during `f64` conversion to duration if seconds.is_nan() || seconds.is_infinite() { return Err(TzOutOfRangeError::new().into()); } if seconds.is_sign_positive() { self.checked_add(Duration::from_secs_f64(seconds)) } else { self.checked_sub(Duration::from_secs_f64(-seconds)) } } } ``` See: artichoke/artichoke#2194. ``@rustbot`` label +T-libs-api -T-libs cc ``@mbartlett21``
version 1.60 of the rust stdlib included a change to improve the accuracy of calculations in `Duration::try_from_secs_f32/64` (see [1]), which had a minor breaking change of slightly altering the calculated results. version 1.63 further tweaked this (see [2]). this commit updates tests to match the new calculated values from 1.63+, and thus fixes failure running tests in newer stable rust releases and helps us to fix CI failure. this is done at the expense of breaking the ability to run the tests with older rust versions, which seems reasonably acceptable at this time. for the record, the new values used here are those calculated using version 1.66. it has been suggested that rather than compare float values directly, it may be better for this crate to compare via the `float_eq` crate instead. this would essentially mean switching to a less accurate comparison. since some time has passed now since i originally encountered this problem, i think that just updating the values compared against is the best move at this time, though i am not necessarily against moving to use of `float_eq` at some point in future if this causes significant problems for users of older rust versions, or if we encounter further such issues from rust stdlib changes. note that in the original issue (see [3]) there were four functions whose doc-tests were failing, yet only three have needed to be modified here. the lack of needing to fix the fourth function's tests is presumably as a result of the changes made in rust v1.63. (the original issue was noticed with a v1.60 nightly). [1]: rust-lang/rust#90247 [2]: rust-lang/rust#96051 [3]: #48
…om-secs-float, r=dtolnay Stabilize `duration_checked_float` ## Stabilization Report This stabilization report is for a stabilization of `duration_checked_float`, tracking issue: rust-lang/rust#83400. ### Implementation History - rust-lang/rust#82179 - rust-lang/rust#90247 - rust-lang/rust#96051 - Changed error type to `FromFloatSecsError` in rust-lang/rust#90247 - rust-lang/rust#96051 changes the rounding mode to round-to-nearest instead of truncate. ## API Summary This stabilization report proposes the following API to be stabilized in `core`, along with their re-exports in `std`: ```rust // core::time impl Duration { pub const fn try_from_secs_f32(secs: f32) -> Result<Duration, TryFromFloatSecsError>; pub const fn try_from_secs_f64(secs: f64) -> Result<Duration, TryFromFloatSecsError>; } #[derive(Debug, Clone, PartialEq, Eq)] pub struct TryFromFloatSecsError { ... } impl core::fmt::Display for TryFromFloatSecsError { ... } impl core::error::Error for TryFromFloatSecsError { ... } ``` These functions are made const unstable under `duration_consts_float`, tracking issue #72440. There is an open question in the tracking issue around what the error type should be called which I was hoping to resolve in the context of an FCP. In this stabilization PR, I have altered the name of the error type to `TryFromFloatSecsError`. In my opinion, the error type shares the name of the method (adjusted to accommodate both types of floats), which is consistent with other error types in `core`, `alloc` and `std` like `TryReserveError` and `TryFromIntError`. ## Experience Report Code such as this is ready to be converted to a checked API to ensure it is panic free: ```rust impl Time { pub fn checked_add_f64(&self, seconds: f64) -> Result<Self, TimeError> { // Fail safely during `f64` conversion to duration if seconds.is_nan() || seconds.is_infinite() { return Err(TzOutOfRangeError::new().into()); } if seconds.is_sign_positive() { self.checked_add(Duration::from_secs_f64(seconds)) } else { self.checked_sub(Duration::from_secs_f64(-seconds)) } } } ``` See: artichoke/artichoke#2194. `@rustbot` label +T-libs-api -T-libs cc `@mbartlett21`
Closes #96045