-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversion from u32
to f32
via cast()
can be inexact
#5
Comments
There is a check ... macro_rules! impl_via_as_revert_check {
($x:ty: $y:ty) => {
impl Conv<$x> for $y {
#[inline]
fn conv(x: $x) -> $y {
let y = x as $y;
debug_assert_eq!(x, y as $x);
y
}
}
};
}
impl_via_as_revert_check!(u32: f32); ... but apparently it doesn't work: fn main() {
println!("{}, {}, {}", !0u32, !0u32 as f32, (!0u32 as f32) as u32);
}
Thanks for reporting. |
Interestingly, |
OK, so I think I've figured this one out. Ugh. Rust recently defined their cast semantics: casting a float to an int now sets it to maxint if the result is too large to fit. This is IMHO a terrible, terrible design decision, but there we are. So Why don't Rust's casts just fail when they aren't exact? Wouldn't that be nice? use easy_cast::Cast;
fn main() {
let x = !0u32 - 1;
let y = x as f32;
println!("{}", x); // prints 429467294
println!("{}", y); // prints 429467300
println!("{}", y as u32); // prints 429467295 (neat?)
let xx = !0u32;
let y: f32 = x.cast(); // does not fail as it should
println!("{}", xx); // prints 429467295
println!("{}", y); // prints 429467300
println!("{}", y as u32); // prints 429467295 (neat?)
let _: f32 = x.cast(); // fails as desired
} |
Looks like you already caught this and fixed it in PR #6. Nice! |
Because I think most of the uses of this "revert check" were wrong actually hence why I replaced them. Only the |
My understanding from the docs is that conversions via
cast()
should always be exact, but this is not currently the case.The text was updated successfully, but these errors were encountered: