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

Slow and biased rounding #13224

Open
TomFryersMidsummer opened this issue Aug 6, 2024 · 0 comments
Open

Slow and biased rounding #13224

TomFryersMidsummer opened this issue Aug 6, 2024 · 0 comments
Labels
A-lint Area: New lints

Comments

@TomFryersMidsummer
Copy link

What it does

Add a lint censuring f32::round and f64::round in favour of the corresponding round_ties_even function. Currently, one can achieve this with

disallowed-methods = [
    { path = "f32::round", reason = "round_ties_even is less biased and faster"},
    { path = "f64::round", reason = "round_ties_even is less biased and faster"},
]

but I think it would be valuable as a separate lint. (Possibly it could be part of in suboptimal_flops, but there's talk about splitting that up already.

For some context, see this issue and this pull request and its tracking issue.

Advantage

  • It's faster.
  • It doesn't introduce an unnecessary positive bias.
  • It is the IEEE-754 default behaviour.

Drawbacks

  • round_ties_even is a longer name that's harder to read.
  • round matches more closely the behaviour of the method taught to young children.
  • It introduces a bias towards even numbers.

Example

fn print_num(x: f32) {
    println!("{}", x.round());
}

Could be written as:

fn print_num(x: f32) {
    println!("{}", x.round_ties_even());
}
@TomFryersMidsummer TomFryersMidsummer added the A-lint Area: New lints label Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant