Limbo 3.5.4
Loading...
Searching...
No Matches
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
15namespace limbo
16{
17
20template <typename T>
21inline T abs(T const& t)
22{
23 return (t > 0)? t : -t;
24}
25
30template <typename Iterator>
31inline 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
43template <typename Iterator>
44inline 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
60template <typename Iterator>
61inline 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
76template <typename Iterator>
77inline 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
92template <typename T>
93inline T lowest();
94
96template <>
97inline char lowest<char>()
98{
99 return std::numeric_limits<char>::min();
100}
101
102template <>
103inline unsigned char lowest<unsigned char>()
104{
105 return std::numeric_limits<unsigned char>::min();
106}
107
108template <>
109inline short lowest<short>()
110{
111 return std::numeric_limits<short>::min();
112}
113
114template <>
115inline unsigned short lowest<unsigned short>()
116{
117 return std::numeric_limits<unsigned short>::min();
118}
119
120template <>
121inline int lowest<int>()
122{
123 return std::numeric_limits<int>::min();
124}
125
126template <>
127inline unsigned int lowest<unsigned int>()
128{
129 return std::numeric_limits<unsigned int>::min();
130}
131
132template <>
133inline long lowest<long>()
134{
135 return std::numeric_limits<long>::min();
136}
137
138template <>
139inline unsigned long lowest<unsigned long>()
140{
141 return std::numeric_limits<unsigned long>::min();
142}
143
144template <>
145inline long long lowest<long long>()
146{
147 return std::numeric_limits<long long>::min();
148}
149
150template <>
151inline unsigned long long lowest<unsigned long long>()
152{
153 return std::numeric_limits<unsigned long long>::min();
154}
155
156template <>
157inline float lowest<float>()
158{
159 return -std::numeric_limits<float>::max();
160}
161
162template <>
163inline double lowest<double>()
164{
165 return -std::numeric_limits<double>::max();
166}
167
168template <>
169inline long double lowest<long double>()
170{
171 return -std::numeric_limits<long double>::max();
172}
173
174
175} // namespace limbo
176
177#endif
namespace for Limbo
long long lowest< long long >()
specialization for integer types
Definition Math.h:145
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
Definition Math.h:61
unsigned long long lowest< unsigned long long >()
specialization for integer types
Definition Math.h:151
unsigned int lowest< unsigned int >()
specialization for integer types
Definition Math.h:127
std::iterator_traits< Iterator >::value_type min(Iterator first, Iterator last)
get min of an array
Definition Math.h:77
std::iterator_traits< Iterator >::value_type sum(Iterator first, Iterator last)
get summation of an array
Definition Math.h:31
std::iterator_traits< Iterator >::value_type average(Iterator first, Iterator last)
get average of an array
Definition Math.h:44
char lowest< char >()
specialization for integer types
Definition Math.h:97
long double lowest< long double >()
specialization for floating point types
Definition Math.h:169
unsigned char lowest< unsigned char >()
specialization for integer types
Definition Math.h:103
short lowest< short >()
specialization for integer types
Definition Math.h:109
int lowest< int >()
specialization for integer types
Definition Math.h:121
double lowest< double >()
specialization for floating point types
Definition Math.h:163
float lowest< float >()
specialization for floating point types
Definition Math.h:157
T abs(T const &t)
generalized api can handle both integer and floating points
Definition Math.h:21
T lowest()
generic function to get lowest value of numbers
unsigned short lowest< unsigned short >()
specialization for integer types
Definition Math.h:115
unsigned long lowest< unsigned long >()
specialization for integer types
Definition Math.h:139
long lowest< long >()
specialization for integer types
Definition Math.h:133