stator
A math, geometry, and utility library
precision.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 Marcus N Campbell Bannerman <[email protected]>
3 
4  This file is part of stator.
5 
6  stator is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  stator is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with stator. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 #include <cstddef>
22 #include <cstdlib>
23 #include <cmath>
24 #include <limits>
25 #include <type_traits>
26 
27 namespace sym {
46  template<class T>
47  size_t subtraction_precision(const T f1, const T f2) {
48  static_assert(std::is_floating_point<T>(), "Can only calculate the precision of addition between floating point types");
49 
50  //Catch the case where this is not actually a subtraction at all
51  if ((f1 == 0) || (f2 == 0) || (std::signbit(f1) != std::signbit(f2)))
52  return std::numeric_limits<size_t>::max();
53 
54  int exp1, exp2;
55  std::frexp(f1, &exp1);
56  std::frexp(f2, &exp2);
57  return std::abs(exp1-exp2);
58  }
59 
68  template<class T>
69  size_t addition_precision(const T f1, const T f2) {
70  return subtraction_precision(f1, -f2);
71  }
72 
74 }
The stator symbolic math library.
size_t addition_precision(const T f1, const T f2)
Calculate a "precision" for addition between two float types.
Definition: precision.hpp:69
constexpr C<(1 - 2 *(num< 0)) *num,(1 - 2 *(den< 0)) *den > abs(const C< num, den > &a)
Definition: constants.hpp:189
size_t subtraction_precision(const T f1, const T f2)
Calculate a "precision" for subtraction between two float types.
Definition: precision.hpp:47