Skip to content

A check for (a + b) / 2 #116

@cushon

Description

@cushon

Original issue created by kevinb@google.com on 2013-04-04 at 03:45 PM


Fact: some percentage of all occurrences of "(a + b) / 2" out there are bugs, or bugs waiting to happen.
(http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html)

Unfortunately, there's no reasonable fix we can suggest that is guaranteed to work for all possible values of a and b... except Guava's IntMath.mean(a, b) / LongMath.mean(a, b) methods.

If there is ever a place in error-prone for "checks for which the fix requires Guava", then it might be worth having a check that forces users of "(a + b) / 2" to change to use mean(). Most of those users probably weren't vulnerable to the bug, but we can't tell which they are.

If we don't do this, we'll probably just have to give up on ever catching that bug, and we will sadly probably give up on these Guava methods entirely, because literally NO ONE is using them (why would they? They already know how to get the mean of two numbers!).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions