rustlings icon indicating copy to clipboard operation
rustlings copied to clipboard

clippy1 doesn't make sense

Open MrFiskarBengt opened this issue 6 years ago • 2 comments

I'm not sure what the exercise is asking for. First off, it took me a while to realize that clippy imposes additional rules and will raise a compilation error(?) if it's triggered. Second, the hint is not very helpful. The exercise tells me to compare two floating points numbers and the hint tells me not to do that.

I finally figured it out by following the link given in the error message and then following another link from that page. Here -> https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp to here -> https://floating-point-gui.de/errors/comparison/

After reading the second link I got it. I need to compare the differences between the numbers and compare them to a threshold, usually called "epsilon". Then I noticed the hint from Rust telling me to use std::f64::EPSILON, although I can't figure out how to use that.

And now, as I'm writing this I finally figured it out. ´error´ in the clippy message refers to the difference between the floats, not an actual programming error.

In conclusion: Maybe add some more information to the hint saying something about not comparing them directly but looking at their delta or diff or epsilon, or whatever you wanna call it.

Anyway, it was an easy lesson to learn, but cluttered with a lot of information that made it hard to figure out. Comparing floats by equality is a bad idea. Looking at the size of the error between them is a better tactic.

fn main() {
    let x = 1.2331f64;
    let y = 1.2332f64;
    if y != x {
        println!("Success!");
    }
} 

Error messages:

! Compiling of exercises/clippy/clippy1.rs failed! Please try again. Here's the output:
    Checking clippy1 v0.0.1 (C:\source\rustlings\exercises\clippy)
error: strict comparison of `f32` or `f64`
  --> clippy1.rs:12:8
   |
12 |     if y != x {
   |        ^^^^^^ help: consider comparing them within some error: `(y - x).abs() > error`
   |
   = note: `#[deny(clippy::float_cmp)]` on by default
note: `std::f32::EPSILON` and `std::f64::EPSILON` are available.
  --> clippy1.rs:12:8
   |
12 |     if y != x {
   |        ^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp

Hint:

Floating point calculations are usually imprecise, so asking if two values are exactly equal is asking for trouble

MrFiskarBengt avatar May 11 '20 09:05 MrFiskarBengt

EPSILON is what an allowed difference between two numbers is called and Clippy is telling you that floating point numbers are not represented perfectly but have some "fudge" in them. The instructions are telling you to essentially not directly compare these results but to see if they're within a tolerance. You may use EPSILON or a similar value to accomplish this.

sthavens avatar May 30 '20 15:05 sthavens

First off, it took me a while to realize that clippy imposes additional rules and will raise a compilation error(?) if it's triggered.

I think this was improved in this commit: https://github.com/rust-lang/rustlings/commit/630ff0e00b24bb5026fe248deb3a4818b9a38d4c

I do agree that error can be confusing here. To understand Clippy's warning, one needs to know about:

  1. Floating point rounding errors: that's adressed by the hint, though maybe the hint could link to more material?
  2. What the word error refers to in this warning: is this jargon specific to floating-point math, or was it short for "error margin" maybe? -> This should be brought up on https://github.com/rust-lang/rust-clippy

fvsch avatar Sep 14 '20 15:09 fvsch