bpp-core3  3.0.0
IntegerTools.h
Go to the documentation of this file.
1 //
2 // File: IntegerTools.h
3 // Authors:
4 // Francois Gindraud (2017)
5 // Created: 2017-03-28 00:00:00
6 // Last modified: 2017-06-26 00:00:00
7 //
8 
9 /*
10  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
11 
12  This software is a computer program whose purpose is to provide utilitary
13  classes. This file belongs to the Bio++ Project.
14 
15  This software is governed by the CeCILL license under French law and
16  abiding by the rules of distribution of free software. You can use,
17  modify and/ or redistribute the software under the terms of the CeCILL
18  license as circulated by CEA, CNRS and INRIA at the following URL
19  "http://www.cecill.info".
20 
21  As a counterpart to the access to the source code and rights to copy,
22  modify and redistribute granted by the license, users are provided only
23  with a limited warranty and the software's author, the holder of the
24  economic rights, and the successive licensors have only limited
25  liability.
26 
27  In this respect, the user's attention is drawn to the risks associated
28  with loading, using, modifying and/or developing or reproducing the
29  software by the user in light of its specific status of free software,
30  that may mean that it is complicated to manipulate, and that also
31  therefore means that it is reserved for developers and experienced
32  professionals having in-depth computer knowledge. Users are therefore
33  encouraged to load and test the software's suitability as regards their
34  requirements in conditions enabling the security of their systems and/or
35  data to be ensured and, more generally, to use and operate it in the
36  same conditions as regards security.
37 
38  The fact that you are presently reading this means that you have had
39  knowledge of the CeCILL license and that you accept its terms.
40 */
41 
42 #ifndef BPP_NUMERIC_INTEGERTOOLS_H
43 #define BPP_NUMERIC_INTEGERTOOLS_H
44 
45 #include <type_traits>
46 
47 
48 namespace bpp
49 {
51 namespace IntegerTools
52 {
54 template<typename T>
55 T divideDown(T n, T divisor) noexcept
56 {
57  static_assert (std::is_integral<T>::value, "T must be an integral type");
58  return n / divisor;
59 }
60 
62 template<typename T>
63 T divideUp(T n, T divisor) noexcept
64 {
65  static_assert (std::is_integral<T>::value, "T must be an integral type");
66  return divideDown(n + divisor - 1, divisor);
67 }
68 
70 template<typename T>
71 T roundDown(T n, T divisor) noexcept
72 {
73  static_assert (std::is_integral<T>::value, "T must be an integral type");
74  return divisor * divideDown(n, divisor);
75 }
76 
78 template<typename T>
79 T roundUp(T n, T divisor) noexcept
80 {
81  static_assert (std::is_integral<T>::value, "T must be an integral type");
82  return divisor * divideUp(n, divisor);
83 }
84 }
85 } // namespace bpp
86 #endif // BPP_NUMERIC_INTEGERTOOLS_H
T divideDown(T n, T divisor) noexcept
Returns floor(n/divisor).
Definition: IntegerTools.h:55
T divideUp(T n, T divisor) noexcept
Returns ceil (n/divisor).
Definition: IntegerTools.h:63
T roundDown(T n, T divisor) noexcept
Round n to previous divisor multiple.
Definition: IntegerTools.h:71
T roundUp(T n, T divisor) noexcept
Round n to next divisor multiple.
Definition: IntegerTools.h:79