Skip to content

Regression in rounding of Duration::from_secs_f64 in 1.60.0 #96045

Closed

Description

use std::time::Duration;

fn main() {
    let dur = Duration::from_secs_f64(0.000042);
    assert_eq!(dur, Duration::new(0, 42000));
}

The above assertion succeeds on Rust standard library versions prior to 1.60.0. On 1.60.0 it fails with:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `41.999µs`,
 right: `42µs`', src/main.rs:6:5

The exact mathematical value represented by 0.000042_f64 is:

0.0000419999999999999976759042230600726952616241760551929473876953125

which is about 41999.999999999998ns. This is astronomically closer to 42000ns than to 41999ns. I believe the correct behavior for from_secs_f64 would be to produce 42000ns as before, not truncate to 41999ns.

Mentioning @newpavlov @nagisa since #90247 seems related and is in the right commit range. The PR summary mentions that the PR would be performing truncation instead of rounding but I don't see that this decision was ever discussed by the library API team. (Mentioning @rust-lang/libs-api to comment on desired behavior.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.P-mediumMedium priorityT-libsRelevant to the library team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions