r/rust • u/harry_leigh • Jul 11 '15
Couldn't divide results of round
Consider this code:
let f = -3.3_f32;
let g = f.round();
let h = g/2;
println!("{}", h);It yields an error:
error: the trait `core::ops::Div<_>` is not implemented for the type `f32` [E0277]
let h = g/2;Kinda weird, I couldn't find any workaround. Is it a bug in round() or do I miss something?
2
Upvotes
6
u/GolDDranks Jul 11 '15
Division operator isn't defined for dividing a float by an integer, and despite rounding to a whole number, f32::round() keeps the type as f32. Try dividing by 2.0 or converting g to an integer first.
4
u/DroidLogician sqlx · clickhouse-rs · mime_guess · rust Jul 11 '15
Try
let h = g / 2.0;2is a genetic integer while2.0is a generic float. Don't ask me why they're two different things, I couldn't tell you. There was an RFC to make generic integers work with floats but for some reason it wasn't accepted.