-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Loss of precision in Duration::mul_f32 (and mul_f64) #149794
Copy link
Copy link
Open
Labels
A-floating-pointArea: Floating point numbers and arithmeticArea: Floating point numbers and arithmeticA-timeArea: TimeArea: TimeC-bugCategory: This is a bug.Category: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-floating-pointArea: Floating point numbers and arithmeticArea: Floating point numbers and arithmeticA-timeArea: TimeArea: TimeC-bugCategory: This is a bug.Category: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
The methods to multiply a
Durationby a float are currently implemented by converting to that float type (as seconds), multiplying, and then converting back to aDuration. This implies unnecessary rounding that loses precision in cases that ideally wouldn't.It is particularly surprising that multiplying by
1.0can change a duration, as floating point multiplication by1.0does preserve numeric values exactly.I expected these methods to behave just like floating point multiplication: As if the exact result was rounded to the resulting type.
The above uses very long durations, but for more realistic examples, this can be observed with durations like:
100ms
1s + 111ns