is_naninf.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_CORE_DATA_HAS_NANINF_HPP
15 #define MLPACK_CORE_DATA_HAS_NANINF_HPP
16 
17 #include <mlpack/prereqs.hpp>
18 
19 namespace mlpack {
20 namespace data {
21 
26 template<typename T>
27 inline bool IsNaNInf(T& val, const std::string& token)
28 {
29  // See if the token represents a NaN or Inf.
30  if ((token.length() == 3) || (token.length() == 4))
31  {
32  const bool neg = (token[0] == '-');
33  const bool pos = (token[0] == '+');
34 
35  const size_t offset = ((neg || pos) && (token.length() == 4)) ? 1 : 0;
36 
37  const std::string token2 = token.substr(offset, 3);
38 
39  if ((token2 == "inf") || (token2 == "Inf") || (token2 == "INF"))
40  {
41  if (std::numeric_limits<T>::has_infinity)
42  {
43  val = (!neg) ? std::numeric_limits<T>::infinity() :
44  -1 * std::numeric_limits<T>::infinity();
45  }
46  else
47  {
48  val = (!neg) ? std::numeric_limits<T>::max() :
49  -1 * std::numeric_limits<T>::max();
50  }
51 
52  return true;
53  }
54  else if ((token2 == "nan") || (token2 == "Nan") || (token2 == "NaN") ||
55  (token2 == "NAN") )
56  {
57  if (std::numeric_limits<T>::has_quiet_NaN)
58  val = std::numeric_limits<T>::quiet_NaN();
59  else
60  val = T(0);
61 
62  return true;
63  }
64  }
65 
66  return false;
67 }
68 
69 } // namespace data
70 } // namespace mlpack
71 
72 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
bool IsNaNInf(T &val, const std::string &token)
See if the token is a NaN or an Inf, and if so, set the value accordingly and return a boolean repres...
Definition: is_naninf.hpp:27