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

Duration div mul extras #52813

Merged
merged 18 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 109 additions & 1 deletion src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
//! ```

use fmt;
use {fmt, u64};
use iter::Sum;
use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};

Expand Down Expand Up @@ -501,13 +501,77 @@ impl Mul<u32> for Duration {
}
}

#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
impl Mul<Duration> for u32 {
type Output = Duration;

fn mul(self, rhs: Duration) -> Duration {
rhs.checked_mul(self).expect("overflow when multiplying scalar by duration")
Copy link
Member

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?

Copy link
Contributor Author

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"

Copy link
Contributor Author

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.

}
}

#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
impl Mul<f64> for Duration {
type Output = Duration;

fn mul(self, rhs: f64) -> Duration {
const NPS: f64 = NANOS_PER_SEC as f64;
if rhs.is_sign_negative() {
panic!("duration can not be multiplied by negative float");
}
let nanos_f64 = rhs * (NPS * (self.secs as f64) + (self.nanos as f64));
Copy link
Member

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

if !nanos_f64.is_finite() {
panic!("got non-finite value when multiplying duration by float");
}
if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
panic!("overflow when multiplying duration by float");
};
let nanos_u128 = nanos_f64 as u128;
Duration {
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
}
}
}

#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
impl Mul<Duration> for f64 {
type Output = Duration;

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");
Copy link
Member

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.

}
let nanos_f64 = self * (NPS * (rhs.secs as f64) + (rhs.nanos as f64));
if !nanos_f64.is_finite() {
panic!("got non-finite value when multiplying float by duration");
}
if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
panic!("overflow when multiplying float by duration");
};
let nanos_u128 = nanos_f64 as u128;
Duration {
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
}
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl MulAssign<u32> for Duration {
fn mul_assign(&mut self, rhs: u32) {
*self = *self * rhs;
}
}

#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
impl MulAssign<f64> for Duration {
fn mul_assign(&mut self, rhs: f64) {
*self = *self * rhs;
}
}

#[stable(feature = "duration", since = "1.3.0")]
impl Div<u32> for Duration {
type Output = Duration;
Expand All @@ -517,13 +581,57 @@ impl Div<u32> for Duration {
}
}

#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
impl Div<f64> for Duration {
type Output = Duration;

fn div(self, rhs: f64) -> Duration {
const NPS: f64 = NANOS_PER_SEC as f64;
if rhs.is_sign_negative() {
panic!("duration can not be divided by negative float");
}
let nanos_f64 = (NPS * (self.secs as f64) + (self.nanos as f64)) / rhs;
if !nanos_f64.is_finite() {
panic!("got non-finite value when dividing duration by float");
}
if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
panic!("overflow when dividing duration by float");
};
let nanos_u128 = nanos_f64 as u128;
Duration {
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
}
}
}

#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
impl Div<Duration> for Duration {
type Output = f64;

fn div(self, rhs: Duration) -> f64 {
const NPS: f64 = NANOS_PER_SEC as f64;
let nanos1 = NPS * (self.secs as f64) + (self.nanos as f64);
let nanos2 = NPS * (rhs.secs as f64) + (rhs.nanos as f64);
nanos1/nanos2
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl DivAssign<u32> for Duration {
fn div_assign(&mut self, rhs: u32) {
*self = *self / rhs;
}
}

#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
impl DivAssign<f64> for Duration {
fn div_assign(&mut self, rhs: f64) {
*self = *self / rhs;
}
}


macro_rules! sum_durations {
($iter:expr) => {{
let mut total_secs: u64 = 0;
Expand Down
26 changes: 26 additions & 0 deletions src/libstd/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,30 @@ mod tests {
let hundred_twenty_years = thirty_years * 4;
assert!(a < hundred_twenty_years);
}

#[test]
fn duration_float_ops() {
let dur = Duration::new(2, 700_000_000);

let dur2 = 3.14*dur;
assert_eq!(dur2, dur*3.14);
assert_eq!(dur2.as_secs(), 8);
assert_eq!(dur2.subsec_nanos(), 478_000_000);

let dur3 = 3.14e5*dur;
assert_eq!(dur3, dur*3.14e5);
assert_eq!(dur3.as_secs(), 847_800);
assert_eq!(dur3.subsec_nanos(), 0);

let dur4 = dur/3.14;
assert_eq!(dur4.as_secs(), 0);
assert_eq!(dur4.subsec_nanos(), 859_872_611);

let dur5 = dur/3.14e5;
assert_eq!(dur5.as_secs(), 0);
// we are using truncation and not rounding
assert_eq!(dur5.subsec_nanos(), 8598);

assert_eq!(dur/Duration::new(5, 400_000_000), 0.5);
}
}