-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
f32/f64 examples should not use EPSILON as an absolute tolerance value #116916
Copy link
Copy link
Open
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-floating-pointArea: Floating point numbers and arithmeticArea: Floating point numbers and arithmeticC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: 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.Relevant 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.Relevant to the library API team, which will review and decide on the PR/issue.
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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-floating-pointArea: Floating point numbers and arithmeticArea: Floating point numbers and arithmeticC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: 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.Relevant 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.Relevant to the library API team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.