Floating point types let us work with numbers that have decimals, like 3.14 or 0.5. They help us handle measurements, money, or any value that isn't a whole number.
Floating point types in Rust
let x: f32 = 3.14; let y: f64 = 2.71828;
f32 means 32-bit floating point number (less precise, uses less memory).
f64 means 64-bit floating point number (more precise, uses more memory).
let pi: f32 = 3.14; let e: f64 = 2.71828;
f64 for decimal numbers.let temperature = 36.6; // defaults to f64
let zero = 0.0_f32; let one = 1.0_f64;
This program calculates the total price including tax. It shows how to use f32 and f64 together by converting f32 to f64 for calculation.
fn main() {
let price: f32 = 19.99;
let tax_rate: f64 = 0.075;
let total = price as f64 * (1.0 + tax_rate);
println!("Price: ${:.2}", price);
println!("Tax rate: {:.3}", tax_rate);
println!("Total price: ${:.2}", total);
}Use f64 when you need more precision, like in financial or scientific calculations.
Be careful mixing f32 and f64 directly; convert one type to the other to avoid errors.
Floating point numbers can have small rounding errors, so avoid checking equality directly.
Floating point types store decimal numbers in Rust.
f32 is less precise, f64 is more precise and the default.
Use them when you need to work with numbers that have fractions.
Practice
3.14?Solution
Step 1: Understand Rust's default floating point type
Rust usesf64as the default floating point type for decimal numbers for better precision.Step 2: Compare with other types
f32is less precise and not the default;floatanddoubleare not Rust types.Final Answer:
f64 -> Option DQuick Check:
Default floating point type = f64 [OK]
- Confusing f32 as default
- Using non-Rust types like float or double
- Assuming integers are default for decimals
num with value 5.5 in Rust?Solution
Step 1: Check Rust syntax for f32 declaration
Declaring a variable with typef32useslet num: f32 = 5.5f32;.Step 2: Validate other options
let num = 5.5 f32; is invalid syntax (should be5.5f32without space). let num: float = 5.5; usesfloatwhich is not a Rust type. let num = 5.5f64; declaresf64, notf32.Final Answer:
let num: f32 = 5.5f32; -> Option BQuick Check:
Correct f32 declaration = let num: f32 = 5.5f32; [OK]
let var: f32 = value; for 32-bit floats [OK]- Using non-existent type 'float'
- Mixing f32 and f64 suffixes incorrectly
- Omitting type when specific precision is needed
let x = 1.0; // f64 by default
let y: f32 = 2.0f32;
println!("{}", x + y as f64);Solution
Step 1: Understand type casting in the expression
Variableyisf32, but it is cast tof64withy as f64before addition.Step 2: Calculate the sum and output format
Adding1.0 (f64)and2.0 (f64)results in3.0. Theprintln!macro prints it as3.0when using{}with floating point numbers.Final Answer:
3.0 -> Option AQuick Check:
println!("{}", 1.0 + 2.0f32 as f64) = 3.0 [OK]
- Not casting f32 to f64 causing type errors
- Expecting integer output instead of float
- Confusing output format with extra zeros
let a: f32 = 3.5f32; let b: f64 = 4.5; let c = a + b;
Solution
Step 1: Check types of variables a and b
Variableaisf32, andbisf64. Rust does not allow direct arithmetic between different float types.Step 2: Identify the error in addition
Addinga + bcauses a type mismatch error. One must cast eitheraorbto the other's type before adding.Final Answer:
Cannot add f32 and f64 without casting -> Option AQuick Check:
Mixing f32 and f64 needs casting [OK]
- Ignoring type mismatch between f32 and f64
- Assuming implicit conversion happens
- Thinking missing semicolon causes error here
Solution
Step 1: Choose the highest precision floating point type
Rust'sf64provides higher precision thanf32, so declaringavgasf64is best.Step 2: Check the expression and types
Using decimal literals without suffix defaults tof64. Dividing by3.0(alsof64) keeps precision. let avg: f64 = (4.2 + 5.8 + 6.0) / 3.0; correctly declaresavg: f64and usesf64literals.Final Answer:
let avg: f64 = (4.2 + 5.8 + 6.0) / 3.0; -> Option CQuick Check:
Use f64 for highest precision average [OK]
- Using f32 when higher precision is needed
- Dividing by integer 3 causing integer division
- Mixing types without explicit declaration
Ready to prove it?
Pass the test and this pattern moves unknown
