-
Notifications
You must be signed in to change notification settings - Fork 141
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
[question] Is num_traits::cast::<f64, f32>(f64) -> Option<f32>
just f64 as f32
? So it never returns None
.
#266
Comments
Ah, I found |
There are varying opinions on the matter. The main difference with integers is that those have no representation of "overflow" at all, whereas any float operation that overflows will get infinity. It's probably a bad choice of words to call that "saturating" though. Some people don't even want float casting to admit any loss, and this debate is a big reason why floats don't implement |
The std lib only has
|
Well, to answer your original question directly:
Yes. Once you accept lossy conversions, as we have here, then all The Note, the Footnotes |
Is
num_traits::cast::<f64, f32>(f64) -> Option<f32>
justf64 as f32
? So it never returnsNone
.But meanwhile
cast::<u64, u32>(u64::MAX)
returnNone
.I checked the doc, found there were 'conversions with possible precision loss or truncation are admitted, ..., or even a large f64 saturating to f32 infinity.' in
NumCast
. So it's as design? It's should be emphasized with large and red fonts, and also incast
.Is there any reasonable
cast_f64_to_f32()
which will fail at something likecast_f64_to_f32(f32::MAX as f64 + 1.0f64) == None
.The text was updated successfully, but these errors were encountered: