Skip to content

f32/f64 examples should not use EPSILON as an absolute tolerance value #116916

Open
@SludgePhD

Description

@SludgePhD

Many examples on the floating-point types incorrectly teach readers that EPSILON is an appropriate tolerance value for approximate comparisons. For instance, the example for f32::mul_add looks like this:

let m = 10.0_f32;
let x = 4.0_f32;
let b = 60.0_f32;

// 100.0
let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();

assert!(abs_difference <= f32::EPSILON);

The machine epsilon is unsuitable for this use case. It is not a general-purpose tolerance value for comparisons, and it should not be taught like it is one.

More in-depth information about how to properly deal with floats can be found here: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

(I suppose ideally libstd would have some API for this built in so that the examples can make use of it)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-floating-pointArea: Floating point numbers and arithmeticC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libsRelevant to the library team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions