bpp-core3  3.0.0
IntegerTools.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_INTEGERTOOLS_H
6 #define BPP_NUMERIC_INTEGERTOOLS_H
7 
8 #include <type_traits>
9 
10 
11 namespace bpp
12 {
14 namespace IntegerTools
15 {
17 template<typename T>
18 T divideDown(T n, T divisor) noexcept
19 {
20  static_assert (std::is_integral<T>::value, "T must be an integral type");
21  return n / divisor;
22 }
23 
25 template<typename T>
26 T divideUp(T n, T divisor) noexcept
27 {
28  static_assert (std::is_integral<T>::value, "T must be an integral type");
29  return divideDown(n + divisor - 1, divisor);
30 }
31 
33 template<typename T>
34 T roundDown(T n, T divisor) noexcept
35 {
36  static_assert (std::is_integral<T>::value, "T must be an integral type");
37  return divisor * divideDown(n, divisor);
38 }
39 
41 template<typename T>
42 T roundUp(T n, T divisor) noexcept
43 {
44  static_assert (std::is_integral<T>::value, "T must be an integral type");
45  return divisor * divideUp(n, divisor);
46 }
47 }
48 } // namespace bpp
49 #endif // BPP_NUMERIC_INTEGERTOOLS_H
T divideUp(T n, T divisor) noexcept
Returns ceil (n/divisor).
Definition: IntegerTools.h:26
T roundUp(T n, T divisor) noexcept
Round n to next divisor multiple.
Definition: IntegerTools.h:42
T roundDown(T n, T divisor) noexcept
Round n to previous divisor multiple.
Definition: IntegerTools.h:34
T divideDown(T n, T divisor) noexcept
Returns floor(n/divisor).
Definition: IntegerTools.h:18