-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric.h
More file actions
70 lines (59 loc) · 2.43 KB
/
Copy pathnumeric.h
File metadata and controls
70 lines (59 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @brief wrappers over standard algorithms, to minimize code size, and make more readable
* @brief cross-platform one-file C++ header
*
* @author Sergey Masuryan
* Contact Telegram: @SergeyJames
*
*/
#pragma once
#include <numeric>
namespace wrp {
/**
* @brief : calculate average value in the range [first,last) (container c) using operator+()
* @required : InputIt must meet the requirements of LegacyInputIterator.
* @complexity : At most last - first applications of the predicate
* @return value : average value (default is float)
**/
template<class C, class T = float>
inline constexpr T average(const C & c, T v = 0.0f) noexcept {
return c.empty() ? static_cast<T>(0) : std::accumulate(c.cbegin(), c.cend(), v) / static_cast<T>(c.size());
}
template<class C, class T = float>
inline constexpr T average(C && c, T v = 0.0f) noexcept {
return c.empty() ? static_cast<T>(0) : std::accumulate(c.cbegin(), c.cend(), v) / static_cast<T>(c.size());
}
/**
* @brief : calculate average value in the range [first,last) using operator+()
* @required : InputIt must meet the requirements of LegacyInputIterator.
* @complexity : At most last - first applications of the predicate
* @return value : average value (default is float)
**/
template<class Init, class T = float>
inline constexpr T average(Init _begin, Init _end, T v = 0.0f) noexcept {
return _begin == _end ? 0.0 : std::accumulate(_begin, _end, v) / static_cast<T>(_end - _begin);
}
/**
* @brief : In statistics and probability theory, a median is a value separating the higher half from the lower half of a data sample
* @required : InputIt must meet the requirements of LegacyInputIterator.
* @complexity : At most last - first applications of the predicate
* @return value : median value (default is float)
**/
template<class Init, class T = float>
inline constexpr T median(Init _begin, Init _end, T v = 0.0f) noexcept {
if (!std::is_sorted(_begin, _end)) {
std::sort(_begin, _end);
}
return *(_begin + (_end - _begin) / 2);
}
/**
* @brief : calculate number of digits(only signed integral types, and not more than INT64_MAX)
* @complexity : Logarithmic
* @return value : number of digits, min val is 1 max cal is 19 (INT64_MAX)
**/
inline constexpr unsigned short number_of_digits(int64_t a) noexcept {
short n = 1;
while ((a /= 10) > 0) ++n;
return n;
}
} // !namespace wrp