Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Janitor: Fix clippy error about f32 comparison #289

Merged
merged 1 commit into from
Jul 6, 2021

Conversation

hunger
Copy link
Member

@hunger hunger commented Jul 3, 2021

This is a code change, but it should not change behavior.

@hunger
Copy link
Member Author

hunger commented Jul 3, 2021

This is actually a clippy error, so it stops processing of the crate and potentially hides other clippy errors and warnings.

@@ -235,9 +235,9 @@ impl From<RgbaColor<f32>> for HsvaColor {
let hue = 60.
* if chroma == 0. {
0.0
} else if max == red {
} else if (max - red).abs() < f32::EPSILON {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case we might be better off with #[allow(clippy::float_cmp)]. rust-lang/rust-clippy#6816 suggests that this is basically the same and in this case I can’t think of a better epsilon either.

No strong opinion though. @ogoffart has more experience on this matter I believe

Copy link
Member Author

@hunger hunger Jul 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... How about defining our own epsilon of 0.00001 or something? That's smaller than 1/(2^16 - 1). That should be good enough for color spaces up to 16 bit per channel.

I have seen 10bit/color channel before, but 16bit should be safe enough for the time being.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could be

Suggested change
} else if (max - red).abs() < f32::EPSILON {
} else if max <= red {

Since we know that max is the maximum between red, green, and blue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, we know that max must be one of the three and it's not the result of another arithmetic operation that might affect precision. I totally missed that :)

<= sounds like it would also silence clippy, indeed.

I favour an explicit #[allow(clippy::float_cmp)] with a comment that says that it's okay to compare because it can only be one of the three options.

A fourth way would be to have let red_is_max: bool = ...; let green_is_max: bool = ...; ... ;-)

What this also means is that Tobias' variant is also correct, despite the rust-lang issue.

@hunger hunger force-pushed the pr/cargo_error branch 2 times, most recently from aee5230 to d8859cc Compare July 6, 2021 19:06
@hunger
Copy link
Member Author

hunger commented Jul 6, 2021

This is probably over-doing it a bit, considering what Olivier said: max can only be one of three values in this case.

@hunger
Copy link
Member Author

hunger commented Jul 6, 2021

How about just allowing this here (with a comment stating why this is OK)?

@@ -232,6 +232,7 @@ impl From<RgbaColor<f32>> for HsvaColor {
let max = red.max(green).max(blue);
let chroma = max - min;

#[allow(clippy::float_cmp)] // `max` is either `red`, `green` or `blue`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my favourite :)

@ogoffart ogoffart merged commit 23708a4 into slint-ui:master Jul 6, 2021
@hunger hunger deleted the pr/cargo_error branch July 7, 2021 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants