Skip to content

Commit

Permalink
change negativity check
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Jul 30, 2018
1 parent 3e07236 commit 2c300fa
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,16 @@ impl Mul<f64> for 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));
if !nanos_f64.is_finite() {
panic!("got non-finite value when multiplying duration by float");
}
if nanos_f64 > MAX_NANOS_F64 {
panic!("overflow when multiplying duration by float");
};
}
if nanos_f64 < 0.0 {
panic!("underflow when multiplying duration by float");
}
let nanos_u128 = nanos_f64 as u128;
Duration {
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
Expand All @@ -541,16 +541,16 @@ impl Mul<Duration> for f64 {

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");
}
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 > MAX_NANOS_F64 {
panic!("overflow when multiplying float by duration");
};
}
if nanos_f64 < 0.0 {
panic!("underflow when multiplying float by duration");
}
let nanos_u128 = nanos_f64 as u128;
Duration {
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
Expand Down Expand Up @@ -588,16 +588,16 @@ impl Div<f64> for 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 > MAX_NANOS_F64 {
panic!("overflow when dividing duration by float");
};
}
if nanos_f64 < 0.0 {
panic!("underflow when multiplying duration by float");
}
let nanos_u128 = nanos_f64 as u128;
Duration {
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
Expand Down

0 comments on commit 2c300fa

Please sign in to comment.