CFPV -- Comparing Floating-Point Values

From RAD Studio
(Redirected from CFPV)
Jump to: navigation, search

Go Up to C++ Audits

Description

Avoids testing floating-point numbers for equality. Floating-point numbers that should be equal are not always equal, due to rounding problems.

Incorrect:

 void calc(double limit) {
    if (limit == 0.0) {
        ...
    }
 }

Correct:

 const double EPS = 0.00001;
 
 void calc(double limit) {
    if (abs(limit) < EPS) {
        ...
    }
 }

See Also