-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Duration div mul extras #52813
Duration div mul extras #52813
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Could you squash all fixups into a single commit? |
9b0d0b5
to
c24fb12
Compare
Done. |
src/libcore/time.rs
Outdated
if !nanos_f64.is_finite() { | ||
panic!("got non-finite value when multiplying duration by float"); | ||
} | ||
if nanos_f64 > (u128::MAX as f64) { |
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 upper limit is u64::MAX * NANOS_PER_SEC
(1.8 × 1028) which is a much smaller number than u128::MAX
(3.4 × 1038). If you check for the former you don't need the second secs > (u64::MAX as u128)
below.
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.
I was conservative about potential edge effects. So you think it will be enough to just check the following condition?
nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64
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.
@newpavlov Yes. But make this a const
please.
src/libcore/time.rs
Outdated
|
||
fn mul(self, rhs: f64) -> Duration { | ||
const NPS: f64 = NANOS_PER_SEC as f64; | ||
let nanos_f64 = rhs * (NPS * (self.secs as f64) + (self.nanos as f64)); |
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.
Consider panicking when !(rhs >= 0)
.
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.
Good point.
c2eb698
to
9246687
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
9246687
to
9086139
Compare
9086139
to
12d8f27
Compare
src/libcore/time.rs
Outdated
fn mul(self, rhs: Duration) -> Duration { | ||
const NPS: f64 = NANOS_PER_SEC as f64; | ||
if self.is_sign_negative() { | ||
panic!("duration can not be multiplied by negative float"); |
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.
This will trigger for a NaN with the sign bit set, which feels like it'd be better reported under non-finite, so maybe just use self < 0
here? Also, multiplying a duration by negative zero seems like it should successfully give a zero duration, not a panic.
src/libcore/time.rs
Outdated
type Output = Duration; | ||
|
||
fn mul(self, rhs: Duration) -> Duration { | ||
rhs.checked_mul(self).expect("overflow when multiplying scalar by duration") |
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.
Maybe just use rhs * self
to avoid repeating things?
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.
They have different panic messages "multiplying scalar by duration" vs "multiplying duration by scalar"
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.
If having different panic messages is not important, then I'll change it and f64
impl to rhs * self
.
5cc5272
to
2ddc2ea
Compare
2ddc2ea
to
2c300fa
Compare
Considering Rust 1.28 release should I change stabilization version to |
@newpavlov Yes. |
src/libcore/time.rs
Outdated
@@ -30,6 +30,7 @@ const NANOS_PER_MILLI: u32 = 1_000_000; | |||
const NANOS_PER_MICRO: u32 = 1_000; | |||
const MILLIS_PER_SEC: u64 = 1_000; | |||
const MICROS_PER_SEC: u64 = 1_000_000; | |||
const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128) - 1) as f64; |
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 - 1
here doesn't change the value because f64
doesn't have enough precision so this constant ends up being exactly 1ns greater than the largest possible Duration
. Either this constant needs to be changed to the next smallest f64
or the check below needs to be changed to nanos >= MAX_NANOS_F64
. from_float_secs(2f64.powi(64))
should trigger an overflow but currently it returns a Duration
of 0ns.
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.
Ah, yes, it should've been >=
. Fixed.
@bors: r+ Looks good to me, thanks! |
📌 Commit 2aca697 has been approved by |
@bors: r- er actually, mind filing a tracking issue and filling that in? |
Done. |
@bors: r+ |
📌 Commit fd7565b has been approved by |
⌛ Testing commit fd7565b with merge 3c8c357fcf68c8348b3ced62bc0d01b7d68820f7... |
💔 Test failed - status-appveyor |
@bors retry 3 hour timeout |
…r=alexcrichton Duration div mul extras Successor of rust-lang#52556. This PR adds the following `impl`s: - `impl Mul<Duration> for u32` (to allow `10*SECOND` in addition to `SECOND*10`) - `impl Mul<f64> for Duration` (to allow `2.5*SECOND` vs `2*SECOND + 500*MILLISECOND`) - `impl Mul<Duration> for f64` - `impl MulAssign<f64> for Duration` - `impl Div<f64> for Duration` - `impl DivAssign<f64> for Duration` - `impl Div<Duration> for Duration` (`Output = f64`, can be useful e.g. for `duration/MINUTE`) `f64` is chosen over `f32` to minimize rounding errors. (52 bits fraction precision vs `Duration`'s ~94 bit)
Rollup of 15 pull requests Successful merges: - #52813 (Duration div mul extras) - #53470 (Warn about metadata loader errors) - #54233 (Remove LLVM 3.9 workaround.) - #54257 (Switch wasm math symbols to their original names) - #54258 (Enable fatal warnings for the wasm32 linker) - #54266 (Update LLVM to fix "bool" arguments on PPC32) - #54290 (Switch linker for aarch64-pc-windows-msvc from LLD to MSVC) - #54292 (Suggest array indexing when tuple indexing on an array) - #54295 (A few cleanups and minor improvements to rustc/traits) - #54298 (miri: correctly compute expected alignment for field) - #54333 (Update The Book to latest) - #54337 (Remove unneeded clone() from tests in librustdoc) - #54346 (rustc: future-proof error reporting for polymorphic constants in types.) - #54362 (Pass --batch to gdb) - #54367 (Add regression test for thread local static mut borrows)
Successor of #52556.
This PR adds the following
impl
s:impl Mul<Duration> for u32
(to allow10*SECOND
in addition toSECOND*10
)impl Mul<f64> for Duration
(to allow2.5*SECOND
vs2*SECOND + 500*MILLISECOND
)impl Mul<Duration> for f64
impl MulAssign<f64> for Duration
impl Div<f64> for Duration
impl DivAssign<f64> for Duration
impl Div<Duration> for Duration
(Output = f64
, can be useful e.g. forduration/MINUTE
)f64
is chosen overf32
to minimize rounding errors. (52 bits fraction precision vsDuration
's ~94 bit)