The Rust Programming Language — Primitives — Float sizes Integers
We have different sizes of floats and integers. f64
and f32
. And as the name suggests, f64
is 64 bit memory variable so it can hold more number of digits and f32
is 32 bit memory variable so it can hold lesser number of digits. So f64
is useful where more precision is of importance in the numbers.
Why not use “f64”
everywhere?
More memory used allows for more precision but also may slow down the program. Not when you have lesser number of f64
variables in your programs but when you have thousands of variables it can significantly increase the RAM consumption.
Take for example below code snippet:
fn main() {
let x: f64 = 10.0/3.0;
let y: f32 = 10.0/3.0;
println!("{}", x);
println!("{}", y);
}
Output:
Above output shows the difference in the number of decimals stored for f64
and f32
type variables.
Where there is a need of performance intensive computations, developers often tend to use f32
rather than f64
which gives them slight advantage over memory consumed and performance…