Skip to content

Slow and biased rounding #13224

Description

@TomFryersMidsummer

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());
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions