-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duration::from_secs_f32 gives wrong results #90225
Comments
|
|
Works on current stable and nightly: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=5c01f41664bf7f4c81861033f80b6578 I mean, that this issue exist for that versions. |
@klensy
|
What is interesting the function: from_secs_f64 doesn't seem to have this problem... |
Extending(?) f32 to u128 gives this addition in 1024: Line 823 in bdcb528
|
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
CC: @newpavlov |
f32 will naturally have trouble representing all possible nanosecond values and so doing calculations in f64 may help here somewhat. However the most appropriate approach would be to decompose the float into the whole part and the fractional part and then operate on the two separately to produce the values necessary to create the
EDIT: I think we would still need to be careful about how the |
Small correction: 1_000_000_000 is represented exactly by I would say such loss of precision is expected for It could be worthwhile to improve the code by processing the fractional part separately, but note that it will not solve this issue fundamentally. We still have the issue of mapping at most 23 fractional bits into ~30 bits, e.g. calling |
|
Argh, I forgot that the UPD: I have implemented manual fraction/exponent splitting and their conversion to integers. |
Hello.
I am diagnosing why I've got a wrong duration computed from Duration::from_secs_f32()...
I tried this code:
I expected to see an exact 30-second duration.
Instead, I've got this:
"30s 1us 24ns"
which is: 30 seconds and 1024 nanos.
I dig deeper into rust code and I've prepared a sample code which is computing it based on the rust/time.rs source code:
As the result I've got:
sec=30, nanos=1024
Instead of exact 30 seconds and 0 nanos.
The text was updated successfully, but these errors were encountered: