MLPACK  1.0.10
clamp.hpp
Go to the documentation of this file.
1 
21 #ifndef __MLPACK_CORE_MATH_CLAMP_HPP
22 #define __MLPACK_CORE_MATH_CLAMP_HPP
23 
24 #include <stdlib.h>
25 #include <math.h>
26 #include <float.h>
27 
28 namespace mlpack {
29 namespace math {
30 
38 inline double ClampNonNegative(const double d)
39 {
40  return (d + fabs(d)) / 2;
41 }
42 
50 inline double ClampNonPositive(const double d)
51 {
52  return (d - fabs(d)) / 2;
53 }
54 
63 inline double ClampRange(double value,
64  const double rangeMin,
65  const double rangeMax)
66 {
67  value -= rangeMax;
68  value = ClampNonPositive(value) + rangeMax;
69  value -= rangeMin;
70  value = ClampNonNegative(value) + rangeMin;
71  return value;
72 }
73 
74 }; // namespace math
75 }; // namespace mlpack
76 
77 #endif // __MLPACK_CORE_MATH_CLAMP_HPP