Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
check_nonnegative.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__ERROR_HANDLING_CHECK_NONNEGATIVE_HPP
2 #define STAN__MATH__ERROR_HANDLING_CHECK_NONNEGATIVE_HPP
3 
6 #include <boost/type_traits/is_unsigned.hpp>
7 #include <stan/meta/traits.hpp>
9 
10 namespace stan {
11 
12  namespace math {
13 
14  namespace {
15  template <typename T_y, typename T_result, bool is_vec>
16  struct nonnegative {
17  static bool check(const char* function,
18  const T_y& y,
19  const char* name,
20  T_result* result) {
21  // have to use not is_unsigned. is_signed will be false
22  // floating point types that have no unsigned versions.
23  if (!boost::is_unsigned<T_y>::value && !(y >= 0))
24  return dom_err(function,y,name,
25  " is %1%, but must be >= 0!","",
26  result);
27  return true;
28  }
29  };
30 
31  template <typename T_y, typename T_result>
32  struct nonnegative<T_y, T_result, true> {
33  static bool check(const char* function,
34  const T_y& y,
35  const char* name,
36  T_result* result) {
37  using stan::length;
38  for (size_t n = 0; n < length(y); n++) {
39  if (!boost::is_unsigned<typename value_type<T_y>::type>::value
40  && !(stan::get(y,n) >= 0))
41  return dom_err_vec(n,function,y,name,
42  " is %1%, but must be >= 0!","",
43  result);
44  }
45  return true;
46  }
47  };
48  }
49 
50  // throws if any element in y is nan
51  template <typename T_y, typename T_result>
52  inline bool check_nonnegative(const char* function,
53  const T_y& y,
54  const char* name,
55  T_result* result) {
56  return nonnegative<T_y,T_result,is_vector_like<T_y>::value>
57  ::check(function, y, name, result);
58  }
59  }
60 }
61 #endif
bool dom_err(const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err.hpp:36
size_t length(const T &)
Definition: traits.hpp:159
bool dom_err_vec(const size_t i, const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err_vec.hpp:33
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
T get(const T &x, size_t n)
Definition: traits.hpp:143

     [ Stan Home Page ] © 2011–2014, Stan Development Team.