Below I quote a simple but useful library (implemented in C++) which contains functions for comparing double precision floating point numbers.
Interface of library (arithmetictools.h):
/* * arithmetictools.h -- floating point numbers comparison tools. * * Copyright (C) 2010 Efstathios Chatzikyriakidis (stathis.chatzikyriakidis@gmail.com) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* header defining the interface of the library. */ #ifndef ARITHMETICTOOLS_H #define ARITHMETICTOOLS_H /* include C++ library headers. */ #include <cmath> #include <limits> /* check if double a is less than double b. */ bool isLessThan (const double a, const double b); /* check if double a is greater than double b. */ bool isGreaterThan (const double a, const double b); /* check if double a is approximately equal to double b. */ bool isApproximatelyEqual (const double a, const double b); /* check if double a is essentially equal to double b. */ bool isEssentiallyEqual (const double a, const double b); #endif // ARITHMETICTOOLS_H
Implementation of library (arithmetictools.cpp):
/*
* arithmetictools.cpp -- floating point numbers comparison tools.
*
* Copyright (C) 2010 Efstathios Chatzikyriakidis (stathis.chatzikyriakidis@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* include header defining the interface of the library. */
#include "arithmetictools.h"
/* check if double a is less than double b. */
bool
isLessThan (const double a, const double b) {
return (b - a) > ((fabs (a) < fabs (b) ? fabs (b) : fabs (a)) *
std::numeric_limits<double>::epsilon ());
}
/* check if double a is greater than double b. */
bool
isGreaterThan (const double a, const double b) {
return (a - b) > ((fabs (a) < fabs (b) ? fabs (b) : fabs (a)) *
std::numeric_limits<double>::epsilon ());
}
/* check if double a is approximately equal to double b. */
bool
isApproximatelyEqual (const double a, const double b) {
return fabs (a - b) <= ((fabs (a) < fabs (b) ? fabs (b) : fabs (a)) *
std::numeric_limits<double>::epsilon ());
}
/* check if double a is essentially equal to double b. */
bool
isEssentiallyEqual(const double a, const double b) {
return fabs (a - b) <= ((fabs (a) > fabs (b) ? fabs (b) : fabs (a)) *
std::numeric_limits<double>::epsilon ());
}


