value_comparison.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_CORE_INCLUDE_CORBO_CORE_VALUE_COMPARISON_H_
26 #define SRC_CORE_INCLUDE_CORBO_CORE_VALUE_COMPARISON_H_
27 
28 #include <algorithm>
29 #include <cmath>
30 #include <complex>
31 
32 namespace corbo {
33 
43 inline bool approx_equal_abs(double a, double b, double epsilon = 1e-6) { return std::abs(a - b) < epsilon; }
44 
45 // The double/float comparison definitions can be found in "The art of computer programming by Knuth"
46 
56 inline bool approx_equal(double a, double b, double epsilon = 1e-6) { return std::abs(a - b) <= std::max(std::abs(a), std::abs(b)) * epsilon; }
57 
66 inline bool approx_equal(const std::complex<double>& a, const std::complex<double>& b, double epsilon = 1e-6)
67 {
68  return approx_equal(a.real(), b.real(), epsilon) && approx_equal(a.imag(), b.imag(), epsilon);
69 }
70 
80 inline bool essentially_equal(double a, double b, double epsilon = 1e-6) { return std::abs(a - b) <= std::min(std::abs(a), std::abs(b)) * epsilon; }
81 
90 inline bool essentially_equal(const std::complex<double>& a, const std::complex<double>& b, double epsilon = 1e-6)
91 {
92  return essentially_equal(a.real(), b.real(), epsilon) && essentially_equal(a.imag(), b.imag(), epsilon);
93 }
94 
102 inline bool approx_zero(double val, double epsilon = 1e-10) { return std::abs(val) <= epsilon; }
103 
112 inline bool definitely_greater_than(double a, double b, double epsilon = 1e-6) { return (a - b) > std::max(std::abs(a), std::abs(b)) * epsilon; }
113 
122 inline bool definitely_less_than(double a, double b, double epsilon = 1e-6) { return (b - a) > std::min(std::abs(a), std::abs(b)) * epsilon; }
123 
124 // almostEqual2sComplement(double A, double B, int maxUlps)
125 // {
126 // assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
127 // int64_t aLong = *reinterpret_cast<int64_t*>( &A );
128 // if (aLong < 0) aLong = 0x8000000000000000 - aLong; int64_t bLong = *reinterpret_cast<int64_t*>( &B );
129 // if (bLong < 0) bLong = 0x8000000000000000 - bLong; int64_t longDiff = (aLong - bLong) & 0x7FFFFFFFFFFFFFFF;
130 // if (longDiff <= maxUlps) return true;
131 // return false;
132 // }
133 
134 } // namespace corbo
135 
136 #endif // SRC_CORE_INCLUDE_CORBO_CORE_VALUE_COMPARISON_H_
#define max(a, b)
Definition: datatypes.h:20
bool approx_equal_abs(double a, double b, double epsilon=1e-6)
Check if two doubles are similar (but with an absolute threshold!)
bool approx_equal(double a, double b, double epsilon=1e-6)
Check if two doubles are appoximately equal.
#define min(a, b)
Definition: datatypes.h:19
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
Scalar * b
Definition: cholesky.cpp:56
Scalar * a
Definition: cholesky.cpp:26
bool approx_zero(double val, double epsilon=1e-10)
Check if a double is appoximately zero.
bool essentially_equal(double a, double b, double epsilon=1e-6)
Check if two doubles are essentially equal.
bool definitely_less_than(double a, double b, double epsilon=1e-6)
Check if a value is definetly less than another value.
bool definitely_greater_than(double a, double b, double epsilon=1e-6)
Check if a value is definetly greater than another value.


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:58