A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
common.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_MATH_COMMON_H_
2 #define GENESIS_UTILS_MATH_COMMON_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <algorithm>
35 #include <cmath>
36 #include <limits>
37 #include <type_traits>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Constants
44 // =================================================================================================
45 
49 constexpr double PI = 3.141592653589793238463;
50 
51 // =================================================================================================
52 // Number Handling
53 // =================================================================================================
54 
61 template< typename T > inline constexpr
62 T abs_diff( T const& lhs, T const& rhs )
63 {
64  return ((lhs > rhs) ? (lhs - rhs) : (rhs - lhs));
65 }
66 
70 template <typename T> inline constexpr
71 int signum(T x, std::false_type )
72 {
73  // The tag type `std::false_type` is unnamed in order to avoid compiler warnings about an
74  // unused parameter. As this function is `constexpr`, we cannot shut this warning down by
75  // using `(void) param`, so making it unnamed is a reasonable solution in this case.
76  return T(0) < x;
77 }
78 
82 template <typename T> inline constexpr
83 int signum(T x, std::true_type )
84 {
85  // The tag type `std::false_type` is unnamed in order to avoid compiler warnings about an
86  // unused parameter. As this function is `constexpr`, we cannot shut this warning down by
87  // using `(void) param`, so making it unnamed is a reasonable solution in this case.
88  return (T(0) < x) - (x < T(0));
89 }
90 
99 template <typename T> inline constexpr
100 int signum(T x)
101 {
102  return signum( x, std::is_signed<T>() );
103 }
104 
109  double lhs,
110  double rhs,
111  double max_rel_diff = std::numeric_limits<double>::epsilon()
112 ) {
113  // Calculate the difference.
114  auto diff = std::abs( lhs - rhs );
115 
116  // Find the larger number.
117  auto largest = std::max( std::abs( lhs ), std::abs( rhs ));
118 
119  // Do the comparison.
120  return ( diff <= largest * max_rel_diff );
121 }
122 
126 inline double round_to( double x, size_t accuracy_order )
127 {
128  double factor = std::pow( 10, accuracy_order );
129  return std::round( x * factor ) / factor;
130 }
131 
142 inline size_t int_pow( size_t base, size_t exp )
143 {
144  // Using Exponentiation by squaring, see
145  // http://stackoverflow.com/a/101613/4184258
146  size_t result = 1;
147  while( exp ) {
148  if( exp & 1 ) {
149  result *= base;
150  }
151  exp >>= 1;
152  base *= base;
153  }
154  return result;
155 }
156 
162 inline bool is_valid_int_pow( size_t base, size_t exp )
163 {
164  return std::pow( base, exp ) < std::numeric_limits<size_t>::max();
165 }
166 
167 } // namespace utils
168 } // namespace genesis
169 
170 #endif // include guard
bool is_valid_int_pow(size_t base, size_t exp)
Return whether the given power can be stored within a size_t.
Definition: common.hpp:162
size_t int_pow(size_t base, size_t exp)
Calculate the power base^exp for positive integer values.
Definition: common.hpp:142
constexpr int signum(T x, std::false_type)
Implementation of signum(T x) for unsigned types. See there for details.
Definition: common.hpp:71
double round_to(double x, size_t accuracy_order)
Retun the value of x, rounded to the decimal digit given by accuracy_order.
Definition: common.hpp:126
constexpr double PI
Make the world go round.
Definition: common.hpp:49
constexpr T abs_diff(T const &lhs, T const &rhs)
Calculate the absolute differenence between two values.
Definition: common.hpp:62
bool almost_equal_relative(double lhs, double rhs, double max_rel_diff=std::numeric_limits< double >::epsilon())
Check whether two doubles are almost equal, using a relative epsilon to compare them.
Definition: common.hpp:108