Elements  6.0.1
A C++ base framework for the Euclid Software.
Real.cpp
Go to the documentation of this file.
1 /*
2  * @file Real.cpp
3  *
4  * @date Jun 13, 2013
5  * @author Hubert Degaudenzi
6  *
7  *
8  * @copyright 2012-2020 Euclid Science Ground Segment
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
11  * Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "ElementsKernel/Real.h"
23 
24 #include <cmath> // for pow
25 #include <cstdint> // for std::int64_t, std::int32_t
26 #include <cstdlib> // for abs
27 #include <limits> // for numeric_limits
28 
29 using std::abs;
31 using std::pow;
32 
33 namespace Elements {
34 
37 
38 // Usable AlmostEqual function
39 bool almostEqual2sComplement(const float& left, const float& right, const int& max_ulps) {
40 
41  using std::int32_t;
42  using std::uint32_t;
43 
44  // int a_int = *(int*)&a;
45  int32_t a_int = *reinterpret_cast<const int32_t*>(&left);
46  // Make a_int lexicographically ordered as a twos-complement int
47  if (a_int < 0) {
48  a_int = static_cast<int32_t>(0x80000000 - static_cast<uint32_t>(a_int));
49  }
50  // Make b_int lexicographically ordered as a twos-complement int
51  // int b_int = *(int*)&b;
52  int32_t b_int = *reinterpret_cast<const int32_t*>(&right);
53  if (b_int < 0) {
54  b_int = static_cast<int32_t>(0x80000000 - static_cast<uint32_t>(b_int));
55  }
56  int32_t int_diff = abs(a_int - b_int);
57  if (int_diff <= max_ulps && -max_ulps <= int_diff) {
58  return true;
59  }
60  return false;
61 }
62 
63 bool almostEqual2sComplement(const double& left, const double& right, const int& max_ulps) {
64 
65  using std::int64_t;
66  using std::uint64_t;
67 
68  // long long a_int = *(long long*)&a;
69 
70  int64_t a_int = *reinterpret_cast<const int64_t*>(&left);
71  // Make a_int lexicographically ordered as a twos-complement int
72  if (a_int < 0) {
73  a_int = static_cast<int64_t>(0x8000000000000000LL - static_cast<uint64_t>(a_int));
74  }
75  // Make b_int lexicographically ordered as a twos-complement int
76  // long long b_int = *(long long*)&b;
77  int64_t b_int = *reinterpret_cast<const int64_t*>(&right);
78  if (b_int < 0) {
79  b_int = static_cast<int64_t>(0x8000000000000000LL - static_cast<uint64_t>(b_int));
80  }
81  int64_t int_diff = abs(a_int - b_int);
82  if (int_diff <= max_ulps && -max_ulps <= int_diff) {
83  return true;
84  }
85  return false;
86 }
87 
88 template bool realBitWiseEqual<float>(const float& left, const float& right);
89 template bool realBitWiseEqual<double>(const double& left, const double& right);
90 
91 } // namespace Elements
Floating point comparison implementations.
template bool realBitWiseEqual< float >(const float &left, const float &right)
bool almostEqual2sComplement(ELEMENTS_UNUSED const FloatType &a, ELEMENTS_UNUSED const FloatType &b, ELEMENTS_UNUSED const std::size_t &max_ulps=0)
Definition: Real.h:327
ELEMENTS_API const double FLT_DEFAULT_TEST_TOLERANCE
Single precision float default test tolerance.
Definition: Real.cpp:35
ELEMENTS_API const double DBL_DEFAULT_TEST_TOLERANCE
Double precision float default test tolerance.
Definition: Real.cpp:36
template bool realBitWiseEqual< double >(const double &left, const double &right)
T pow(T... args)