Changeset 1312
- Timestamp:
- May 19, 2008, 6:30:52 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/NEWS
r1303 r1312 6 6 7 7 - yat now requires GSL 1.8 or later 8 - is_int now depends on numerical_limits for inf and NaN treatment 9 - Similar for is_float. See revision [1312] for details. 8 10 9 11 Version 0.4.1 (released 14 May 2008) -
trunk/test/utility_test.cc
r1304 r1312 77 77 if (!utility::is_double(s)){ 78 78 suite.add(false); 79 suite.err() << "error: " << s << " is a double\n"; 79 80 } 81 /* we don't require NaN for float and int 80 82 else if (!utility::is_float(s)) { 81 83 suite.add(false); 84 suite.err() << "error: " << s << " is a float\n"; 82 85 } 83 86 else if (!utility::is_int(s)) { 84 87 suite.add(false); 88 suite.err() << "error: " << s << " is a int\n"; 85 89 } 90 */ 86 91 else if (!utility::is_nan(s)) { 87 92 suite.add(false); 93 suite.err() << "error: " << s << " is nan\n"; 94 88 95 } 89 96 … … 108 115 utility::convert<double>("inf"); 109 116 utility::convert<double>("NaN"); 117 118 if (!suite.add(utility::is<double>("-inf"))) 119 suite.err() << "is<double>(\"-inf\") should return true\n"; 120 if (!suite.add(utility::is<double>("inf"))) 121 suite.err() << "is<double>(\"inf\") should return true\n"; 122 if (!suite.add(utility::is<double>("NaN"))) 123 suite.err() << "is<double>(\"NaN\") should return true\n"; 124 if (!suite.add(utility::is<double>("1.23"))) 125 suite.err() << "is<double>(\"1.23\") should return true\n"; 126 if (!suite.add(!utility::is<double>("1.23.2"))) 127 suite.err() << "is<double>(\"1.23.2\") should return false\n"; 128 if (!suite.add(!utility::is<int>("1.23"))) 129 suite.err() << "is<int>(\"1.23\") should return true\n"; 110 130 111 131 { -
trunk/yat/utility/utility.cc
r1304 r1312 39 39 bool is_double(const std::string& s) 40 40 { 41 if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf")) 42 return true; 43 std::stringstream ss(s); 44 double a; 45 ss>>a; 46 if(ss.fail()) 47 return false; 48 // Check that nothing is left on stream 49 std::string b; 50 ss >> b; 51 return (b.size() ? false : true); 41 return is<double>(s); 52 42 } 53 43 … … 70 60 bool is_float(const std::string& s) 71 61 { 72 if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf")) 73 return true; 74 std::stringstream ss(s); 75 float a; 76 ss>>a; 77 if(ss.fail()) 78 return false; 79 // Check that nothing is left on stream 80 std::string b; 81 ss >> b; 82 return (b.size() ? false : true); 62 return is<float>(s); 83 63 } 84 64 … … 86 66 bool is_int(const std::string& s) 87 67 { 88 if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf")) 89 return true; 90 std::stringstream ss(s); 91 int a; 92 ss >> a; 93 if(ss.fail()) 94 return false; 95 // Check that nothing is left on stream 96 std::string b; 97 ss >> b; 98 return (b.size() ? false : true); 68 return is<int>(s); 99 69 } 100 70 -
trunk/yat/utility/utility.h
r1304 r1312 54 54 T convert(const std::string& s); 55 55 56 /** 57 \brief check if string is convertible to (numerical) type 58 */ 59 template<typename T> 60 bool is(const std::string& s); 61 56 62 /// 57 63 /// @return true if string is a double 64 /// 65 /// \deprecated use is<double>(const std::string&) 58 66 /// 59 67 bool is_double(const std::string&); … … 69 77 /// @return true if string is a float 70 78 /// 79 /// \deprecated use is<float>(const std::string&) 80 /// 71 81 bool is_float(const std::string&); 72 82 73 83 /// 74 84 /// @return true if string is an int 85 /// 86 /// \deprecated use is<int>(const std::string&) 75 87 /// 76 88 bool is_int(const std::string&); … … 114 126 } 115 127 128 template<typename T> 129 bool is(const std::string& s) 130 { 131 if (is_nan(s)) 132 return std::numeric_limits<T>::has_quiet_NaN; 133 if (is_equal(s, "inf")) 134 return std::numeric_limits<T>::has_infinity; 135 if (is_equal(s, "-inf")) 136 return std::numeric_limits<T>::has_infinity && 137 std::numeric_limits<T>::is_signed; 138 std::stringstream ss(s); 139 T a; 140 ss >> a; 141 if(ss.fail()) 142 return false; 143 // Check that nothing is left on stream 144 std::string b; 145 ss >> b; 146 return b.empty(); 147 } 148 116 149 }}} // of namespace utility, yat, and theplu 117 150
Note: See TracChangeset
for help on using the changeset viewer.