Open
Description
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
Labels
Area: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Floating point numbers and arithmeticCategory: An issue proposing an enhancement or a PR with one.Relevant to the library team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.