Limbo
3.5.4
Toggle main menu visibility
Loading...
Searching...
No Matches
limbo
math
Math.h
Go to the documentation of this file.
1
7
8
#ifndef _LIMBO_MATH_MATH
9
#define _LIMBO_MATH_MATH
10
11
#include <iterator>
12
#include <limits>
13
15
namespace
limbo
16
{
17
20
template
<
typename
T>
21
inline
T
abs
(T
const
& t)
22
{
23
return
(t > 0)? t : -t;
24
}
25
30
template
<
typename
Iterator>
31
inline
typename
std::iterator_traits<Iterator>::value_type
sum
(Iterator first, Iterator last)
32
{
33
typename
std::iterator_traits<Iterator>::value_type v = 0;
34
for
(; first != last; ++first)
35
v += *first;
36
return
v;
37
}
38
43
template
<
typename
Iterator>
44
inline
typename
std::iterator_traits<Iterator>::value_type
average
(Iterator first, Iterator last)
45
{
46
typename
std::iterator_traits<Iterator>::value_type v = 0;
47
size_t
cnt = 0;
48
for
(; first != last; ++first)
49
{
50
v += *first;
51
cnt += 1;
52
}
53
return
v/cnt;
54
}
55
60
template
<
typename
Iterator>
61
inline
typename
std::iterator_traits<Iterator>::value_type
max
(Iterator first, Iterator last)
62
{
63
typename
std::iterator_traits<Iterator>::value_type v = *first;
64
for
(; first != last; ++first)
65
{
66
if
(v < *first)
67
v = *first;
68
}
69
return
v;
70
}
71
76
template
<
typename
Iterator>
77
inline
typename
std::iterator_traits<Iterator>::value_type
min
(Iterator first, Iterator last)
78
{
79
typename
std::iterator_traits<Iterator>::value_type v = *first;
80
for
(; first != last; ++first)
81
{
82
if
(v > *first)
83
v = *first;
84
}
85
return
v;
86
}
87
92
template
<
typename
T>
93
inline
T
lowest
();
94
96
template
<>
97
inline
char
lowest<char>
()
98
{
99
return
std::numeric_limits<char>::min();
100
}
101
102
template
<>
103
inline
unsigned
char
lowest<unsigned char>
()
104
{
105
return
std::numeric_limits<unsigned char>::min();
106
}
107
108
template
<>
109
inline
short
lowest<short>
()
110
{
111
return
std::numeric_limits<short>::min();
112
}
113
114
template
<>
115
inline
unsigned
short
lowest<unsigned short>
()
116
{
117
return
std::numeric_limits<unsigned short>::min();
118
}
119
120
template
<>
121
inline
int
lowest<int>
()
122
{
123
return
std::numeric_limits<int>::min();
124
}
125
126
template
<>
127
inline
unsigned
int
lowest<unsigned int>
()
128
{
129
return
std::numeric_limits<unsigned int>::min();
130
}
131
132
template
<>
133
inline
long
lowest<long>
()
134
{
135
return
std::numeric_limits<long>::min();
136
}
137
138
template
<>
139
inline
unsigned
long
lowest<unsigned long>
()
140
{
141
return
std::numeric_limits<unsigned long>::min();
142
}
143
144
template
<>
145
inline
long
long
lowest<long long>
()
146
{
147
return
std::numeric_limits<long long>::min();
148
}
149
150
template
<>
151
inline
unsigned
long
long
lowest<unsigned long long>
()
152
{
153
return
std::numeric_limits<unsigned long long>::min();
154
}
155
156
template
<>
157
inline
float
lowest<float>
()
158
{
159
return
-std::numeric_limits<float>::max();
160
}
161
162
template
<>
163
inline
double
lowest<double>
()
164
{
165
return
-std::numeric_limits<double>::max();
166
}
167
168
template
<>
169
inline
long
double
lowest<long double>
()
170
{
171
return
-std::numeric_limits<long double>::max();
172
}
173
174
175
}
// namespace limbo
176
177
#endif
limbo
namespace for Limbo
Definition
GraphUtility.h:23
limbo::max
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
Definition
Math.h:61
limbo::min
std::iterator_traits< Iterator >::value_type min(Iterator first, Iterator last)
get min of an array
Definition
Math.h:77
limbo::sum
std::iterator_traits< Iterator >::value_type sum(Iterator first, Iterator last)
get summation of an array
Definition
Math.h:31
limbo::average
std::iterator_traits< Iterator >::value_type average(Iterator first, Iterator last)
get average of an array
Definition
Math.h:44
limbo::abs
T abs(T const &t)
generalized api can handle both integer and floating points
Definition
Math.h:21
limbo::lowest
T lowest()
generic function to get lowest value of numbers
Generated on
for Limbo by
1.18.0