Elements  5.12.0
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 <cstdlib> // for abs
25 #include <limits> // for numeric_limits
26 #include <cstdint> // for std::int64_t, std::int32_t
27 #include <cmath> // for pow
28 
29 using std::abs;
30 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 
64 bool almostEqual2sComplement(const double& left, const double& right, const int& max_ulps) {
65 
66  using std::int64_t;
67  using std::uint64_t;
68 
69  // long long a_int = *(long long*)&a;
70 
71  int64_t a_int = *reinterpret_cast<const int64_t *>(&left);
72  // Make a_int lexicographically ordered as a twos-complement int
73  if (a_int < 0) {
74  a_int = static_cast<int64_t>(0x8000000000000000LL - static_cast<uint64_t>(a_int));
75  }
76  // Make b_int lexicographically ordered as a twos-complement int
77  // long long b_int = *(long long*)&b;
78  int64_t b_int = *reinterpret_cast<const int64_t *>(&right);
79  if (b_int < 0) {
80  b_int = static_cast<int64_t>(0x8000000000000000LL - static_cast<uint64_t>(b_int));
81  }
82  int64_t int_diff = abs(a_int - b_int);
83  if (int_diff <= max_ulps && -max_ulps <= int_diff) {
84  return true;
85  }
86  return false;
87 }
88 
89 
90 template bool realBitWiseEqual<float>(const float& left, const float& right);
91 template bool realBitWiseEqual<double>(const double& left, const double& right);
92 
93 
94 } // 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:330
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)