C++ Utilities: misc/math.h Source File

C++ Utilities 5.34.2 Useful C++ classes and routines such as argument parser, IO and conversion utilities

Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1#ifndef MATHUTILITIES_H
2#define MATHUTILITIES_H
3
4#include "./traits.h"
5
6namespace CppUtilities {
7
11template <typename IntegralType, Traits::EnableIf<std::is_integral<IntegralType>> * = nullptr>
12 constexpr IntegralType digitsum(IntegralType number, IntegralType base = 10)
13{
14 IntegralType res = 0;
15 while (number > 0) {
16 res += number % base;
17 number /= base;
18 }
19 return res;
20}
21
25 template <typename IntegralType, Traits::EnableIf<std::is_integral<IntegralType>> * = nullptr> constexpr IntegralType factorial(IntegralType number)
26{
27 IntegralType res = 1;
28 for (IntegralType i = 1; i <= number; ++i) {
29 res *= i;
30 }
31 return res;
32}
33
37template <typename IntegralType, Traits::EnableIf<std::is_integral<IntegralType>, std::is_unsigned<IntegralType>> * = nullptr>
38 constexpr IntegralType powerModulo(const IntegralType base, const IntegralType exponent, const IntegralType module)
39{
40 IntegralType res = 1;
41 for (IntegralType mask = static_cast<IntegralType>(1) << static_cast<IntegralType>(sizeof(IntegralType) * 8 - 1); mask; mask >>= 1) {
42 if (mask & exponent) {
43 res *= base;
44 }
45 if (mask != 1) {
46 res *= res;
47 }
48 res %= module;
49 }
50 return res;
51}
52
56template <typename IntegralType, Traits::EnableIf<std::is_integral<IntegralType>, std::is_unsigned<IntegralType>> * = nullptr>
57 constexpr IntegralType inverseModulo(IntegralType number, IntegralType module)
58{
59 IntegralType y1 = 0, y2 = 1;
60 while (number != 1) {
61 IntegralType tmp = y1 - (module / number) * y2;
62 y1 = y2;
63 y2 = tmp;
64 tmp = module % number;
65 module = number;
66 number = tmp;
67 }
68 return y2;
69}
70
74template <typename IntegralType, Traits::EnableIf<std::is_integral<IntegralType>, std::is_unsigned<IntegralType>> * = nullptr>
75 constexpr IntegralType orderModulo(const IntegralType number, const IntegralType module)
76{
77 IntegralType order = 1;
78 for (; powerModulo(number, order, module) != 1 && order != module; ++order)
79 ;
80 return order != module ? order : 0;
81}
82
84 template <typename T> constexpr T min(T first, T second)
85{
86 return first < second ? first : second;
87}
88
90 template <typename T1, typename... T2> constexpr T1 min(T1 first, T1 second, T2... remaining)
91{
92 return first < second ? min(first, remaining...) : min(second, remaining...);
93}
94
96 template <typename T> constexpr T max(T first, T second)
97{
98 return first > second ? first : second;
99}
100
102 template <typename T1, typename... T2> constexpr T1 max(T1 first, T1 second, T2... remaining)
103{
104 return first > second ? max(first, remaining...) : max(second, remaining...);
105}
106
107} // namespace CppUtilities
108
109#endif // MATHUTILITIES_H
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::factorial
constexpr IntegralType factorial(IntegralType number)
Returns the factorial of the given number.
Definition math.h:25
CppUtilities::inverseModulo
constexpr IntegralType inverseModulo(IntegralType number, IntegralType module)
Computes the inverse of number modulo module.
Definition math.h:57
CppUtilities::digitsum
constexpr IntegralType digitsum(IntegralType number, IntegralType base=10)
Returns the digitsum of the given number using the specified base.
Definition math.h:12
CppUtilities::max
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition math.h:96
CppUtilities::powerModulo
constexpr IntegralType powerModulo(const IntegralType base, const IntegralType exponent, const IntegralType module)
Computes base power exponent modulo module.
Definition math.h:38
CppUtilities::min
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition math.h:84
CppUtilities::orderModulo
constexpr IntegralType orderModulo(const IntegralType number, const IntegralType module)
Computes the order of number modulo module.
Definition math.h:75
traits.h
i
constexpr int i
Definition traitstests.cpp:106