🎓 A1Learner
🔍
0 of 142 lessons completed

Rust Floating Point

Floating point numbers are numbers with a decimal part, such as 19.99 and 3.14. Floats are estimates, not exact values.

Two sizes of float

A number with a decimal point is a float. Leave the type out and Rust picks f64:

let price = 19.99;
let tax: f32 = 2.5;
println!("{} {}", price, tax);

Floats are close, not exact

A float stores a value very close to the real one. Sometimes you can see the seam:

let total = 0.1 + 0.2;
println!("total = {}", total);

Round a float four ways

floor goes down, ceil goes up, round goes to the nearest whole, and trunc just cuts the decimal part off:

let x: f64 = 3.7;
println!("down = {} up = {} nearest = {} cut = {}", x.floor(), x.ceil(), x.round(), x.trunc());
let y: f64 = -3.7;
println!("neg down = {} neg cut = {}", y.floor(), y.trunc());

Float maths tools

Floats carry their own maths: sqrt for square roots, powf for powers, abs for the size without the sign:

let area: f64 = 16.0;
println!("root = {}", area.sqrt());
println!("side = {}", 3.0f64.powf(2.0));
println!("gap = {}", (-2.5f64).abs());

Infinity and not-a-number

Dividing a float by zero does not stop the program. You get special values, and there are checks for them:

let big: f64 = 1.0 / 0.0;
println!("big = {}", big);
println!("inf? {}", big.is_infinite());
let mystery: f64 = 0.0 / 0.0;
println!("nan? {} finite? {}", mystery.is_nan(), mystery.is_finite());

Convert with as

A float and a whole number cannot divide until their types match. The word as turns a value into another type:

let people = 4;
let each = 47.80 / people as f64;
println!("each = {:.2}", each);

Example

fn main() {
    let bill = 47.80;
    let people = 4;
    let each = bill / people as f64;
    println!("each pays = {:.2}", each);
    let tip = (bill * 0.15).round();
    println!("tip = {:.2}", tip);
    let total = bill + tip;
    println!("total = {:.2}", total);
    println!("per person with tip = {:.2}", total / people as f64);
}

Key points

  • f32 and f64 are the two float types.
  • The number is the size in bits.
  • Both can be negative
  • A float method like .floor() needs the type settled first.
  • Set the type in the let line: let x: f64 = 3.7.
  • Rust will not guess while a method call is waiting (error E0689)
  • Compare floats with a little room for error, or round them first.
  • Never trust == on a calculated float

In this example, bill is 47.80 and people is 4. Dividing needs both sides to be the same type. So people as f64 turns the count into a float. And each comes out as 11.95. The tip is 15% of the bill, rounded to a whole number with .round() — 7.17 becomes 7. Adding gives a total of 54.80, and splitting it four ways gives 13.70 each. The {:.2} in each print keeps the money display at two decimal places. This hides the tiny float wobble you saw in this lesson.

Note

Floats divide by zero quietly: 1.0 / 0.0 is infinity, and 0.0 / 0.0 is NaN.

Integer division by zero stops the program instead.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What is 0.1 + 0.2 == 0.3 in Rust?