forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatingPointComparison.cpp
59 lines (53 loc) · 2.24 KB
/
FloatingPointComparison.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "MantidKernel/FloatingPointComparison.h"
#include "MantidKernel/System.h"
#include <cmath>
#include <limits>
namespace Mantid::Kernel {
/**
* Compare floating point numbers for equality to within
* std::numeric_limits<TYPE>::epsilon precision
* @param x :: LHS comparator
* @param y :: RHS comparator
* @returns True if the numbers are considered equal within the given tolerance,
* false otherwise
*/
template <typename TYPE> bool equals(const TYPE x, const TYPE y) {
return !(std::fabs(x - y) > std::numeric_limits<TYPE>::epsilon());
}
/**
* Compare two floating-point numbers as to whether they satisfy x<=y within
* machine precision
* @param x :: LHS comparator
* @param y :: RHS comparator
* @returns True if the numbers are considered <= within the machine tolerance,
* false otherwise
*/
template <typename T> MANTID_KERNEL_DLL bool ltEquals(const T x, const T y) { return (equals(x, y) || x < y); }
/**
* Compare two floating-point numbers as to whether they satisfy x>=y within
* machine precision
* @param x :: LHS comparator
* @param y :: RHS comparator
* @returns True if the numbers are considered <= within the machine tolerance,
* false otherwise
*/
template <typename T> MANTID_KERNEL_DLL bool gtEquals(const T x, const T y) { return (equals(x, y) || x > y); }
///@cond
// Concrete instantiations
template DLLExport bool equals<double>(const double, const double);
template DLLExport bool equals<float>(const float, const float);
template DLLExport bool ltEquals<double>(const double, const double);
template DLLExport bool ltEquals<float>(const float, const float);
template DLLExport bool gtEquals<double>(const double, const double);
template DLLExport bool gtEquals<float>(const float, const float);
///@endcond
} // namespace Mantid::Kernel