Elements  6.2
A C++ base framework for the Euclid Software.
Classes | Namespaces | Functions | Variables
Real.h File Reference

Floating point comparison implementations. More...

#include <cmath>
#include <cstring>
#include <limits>
#include <type_traits>
#include "ElementsKernel/Export.h"
#include "ElementsKernel/Unused.h"
Include dependency graph for Real.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Elements::TypeWithSize< size >
 
class  Elements::TypeWithSize< 4 >
 
class  Elements::TypeWithSize< 8 >
 
class  Elements::FloatingPoint< RawType >
 
union  Elements::FloatingPoint< RawType >::FloatingPointUnion
 

Namespaces

 Elements
 

Functions

template<typename RawType >
constexpr std::size_t Elements::defaultMaxUlps ()
 
template<>
constexpr std::size_t Elements::defaultMaxUlps< float > ()
 
template<>
constexpr std::size_t Elements::defaultMaxUlps< double > ()
 
template<typename FloatType >
bool Elements::almostEqual2sComplement (ELEMENTS_UNUSED const FloatType &a, ELEMENTS_UNUSED const FloatType &b, ELEMENTS_UNUSED const std::size_t &max_ulps=0)
 
template<typename RawType >
bool Elements::isNan (const RawType &x)
 
template<typename RawType , std::size_t max_ulps = defaultMaxUlps<RawType>()>
bool Elements::isEqual (const RawType &left, const RawType &right)
 
template<std::size_t max_ulps>
bool Elements::isEqual (const float &left, const float &right)
 
template<std::size_t max_ulps>
bool Elements::isEqual (const double &left, const double &right)
 
template<typename RawType , std::size_t max_ulps = defaultMaxUlps<RawType>()>
bool Elements::isNotEqual (const RawType &left, const RawType &right)
 
template<std::size_t max_ulps>
bool Elements::isNotEqual (const float &left, const float &right)
 
template<std::size_t max_ulps>
bool Elements::isNotEqual (const double &left, const double &right)
 
template<typename RawType , std::size_t max_ulps = defaultMaxUlps<RawType>()>
bool Elements::isLess (const RawType &left, const RawType &right)
 
template<std::size_t max_ulps>
bool Elements::isLess (const float &left, const float &right)
 
template<std::size_t max_ulps>
bool Elements::isLess (const double &left, const double &right)
 
template<typename RawType , std::size_t max_ulps = defaultMaxUlps<RawType>()>
bool Elements::isGreater (const RawType &left, const RawType &right)
 
template<std::size_t max_ulps>
bool Elements::isGreater (const float &left, const float &right)
 
template<std::size_t max_ulps>
bool Elements::isGreater (const double &left, const double &right)
 
template<typename RawType , std::size_t max_ulps = defaultMaxUlps<RawType>()>
bool Elements::isLessOrEqual (const RawType &left, const RawType &right)
 
template<std::size_t max_ulps>
bool Elements::isLessOrEqual (const float &left, const float &right)
 
template<std::size_t max_ulps>
bool Elements::isLessOrEqual (const double &left, const double &right)
 
template<typename RawType , std::size_t max_ulps = defaultMaxUlps<RawType>()>
bool Elements::isGreaterOrEqual (const RawType &left, const RawType &right)
 
template<std::size_t max_ulps>
bool Elements::isGreaterOrEqual (const float &left, const float &right)
 
template<std::size_t max_ulps>
bool Elements::isGreaterOrEqual (const double &left, const double &right)
 
ELEMENTS_API bool Elements::almostEqual2sComplement (const float &left, const float &right, const int &max_ulps=FLT_DEFAULT_MAX_ULPS)
 This function compare 2 floats with a relative tolerance. More...
 
ELEMENTS_API bool Elements::almostEqual2sComplement (const double &left, const double &right, const int &max_ulps=DBL_DEFAULT_MAX_ULPS)
 This function compare 2 doubles with a relative tolerance. More...
 
template<typename RawType >
ELEMENTS_API bool Elements::realBitWiseEqual (const RawType &left, const RawType &right)
 This function compares 2 floating point numbers bitwise. These are the strict equivalent of the "==". They are only here for the example. More...
 

Variables

constexpr std::size_t Elements::FLT_DEFAULT_MAX_ULPS {4}
 Single precision float default maximum unit in the last place. More...
 
constexpr std::size_t Elements::DBL_DEFAULT_MAX_ULPS {10}
 Double precision float default maximum unit in the last place. More...
 
const ELEMENTS_API double Elements::FLT_DEFAULT_TEST_TOLERANCE = pow(10.0, -numeric_limits<float>::digits10)
 Single precision float default test tolerance. More...
 
const ELEMENTS_API double Elements::DBL_DEFAULT_TEST_TOLERANCE = pow(10.0, -numeric_limits<double>::digits10)
 Double precision float default test tolerance. More...
 

Detailed Description

Floating point comparison implementations.

Author
Hubert Degaudenzi
Date
Jun 13, 2013

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version.

This library 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

Due to the finite representation of the real numbers in the computing architecture, the comparison between 2 floating point numbers needs to be done carefully. In details, even if the representation bit-wise of 2 numbers is different, the real numbers they represent might be the same.

In essence, this is equivalent to compare the 2 numbers \(x\) and \(y\) with a relative tolerance number \(\epsilon\):

\[ |x-y| \leq \epsilon |x+y| \]

Definition in file Real.h.