Skip to content

Commit bad2c6d

Browse files
committed
Fix Duration::from_secs_f32
Currently the function is returning wrong values, eg: Duration::from_secs_f32(30.0); is giving: 30.000001024s (30s + 1024ns) This commit fixes this problem. Fixes: rust-lang#90225
1 parent bdcb528 commit bad2c6d

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

library/core/src/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ impl Duration {
812812
#[inline]
813813
pub const fn try_from_secs_f32(secs: f32) -> Result<Duration, FromSecsError> {
814814
const MAX_NANOS_F32: f32 = ((u64::MAX as u128 + 1) * (NANOS_PER_SEC as u128)) as f32;
815-
let nanos = secs * (NANOS_PER_SEC as f32);
815+
let nanos = secs as f64 * (NANOS_PER_SEC as f64);
816816
if !nanos.is_finite() {
817817
Err(FromSecsError { kind: FromSecsErrorKind::NonFinite })
818818
} else if nanos >= MAX_NANOS_F32 {

0 commit comments

Comments
 (0)