Show: Delphi C++
Display Preferences

CFPV -- Comparing Floating-Point Values

From RAD Studio
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

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions
Assistance