Skip to content

Loss of precision with duration totaling #648

@catamorphism

Description

@catamorphism

The following test program:

use temporal_rs::{ Duration, PlainDate };
use temporal_rs::options::{RelativeTo, Unit};

fn main() {
    let d5 = Duration::new(0, 0, 0, 40, 0, 0, 0, 0, 0, 0).unwrap();
    let relative_to = PlainDate::try_new_iso(1972, 1, 31).expect("Failed to initialize date");
    let total = d5.total(Unit::Month, Some(RelativeTo::PlainDate(relative_to))).expect("total failed");
    println!("{}", total);
}

prints 1.3548387096774195. The reference implementation and SpiderMonkey both return 1.3548387096774193 for the equivalent JS expression, new Temporal.Duration(0, 0, 0, 40).total({ unit: "months", relativeTo: "1972-01-31" }). I believe temporal_rs is incorrect here, because when looking at nudge_calendar_unit in a debugger, total evaluates to 1.3548387096774195. total is effectively (1 + progress) in this case, and progress evaluates to 0.35483870967741937. It seems to me that total should be 1.35483870967741937 or 1.3548387096774193 with rounding (truncation).

This isn't too surprising since there are already comments in the code about not using f64.

There is no test262 coverage for this case yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions