Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Comparing Real Numbers

Comparing whether or not two real numbers are identical is problematic due to rounding errors. See the below C++ example for an illustration:

// {...}
int main() {
cout << (sqrt(3) * sqrt(3) - 3) << endl
<< (sqrt(3) * sqrt(3) == 3) << endl
<< (fabs(sqrt(3) * sqrt(3) - 3) < 0.0000001) << endl;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content