Unverified Commit 328ff806 authored by Robert Kimball's avatar Robert Kimball Committed by GitHub

INFINITY working for float and double (#460)

parent bce27b39
......@@ -270,3 +270,32 @@ ngraph::FpropCache ngraph::cache_fprop(std::shared_ptr<ngraph::Function> fprop,
return fprop_cache;
}
namespace ngraph
{
template <>
float parse_string<float>(const std::string& s)
{
const char* tmp = s.c_str();
char* end;
float result = strtof(tmp, &end);
if (*end != 0)
{
throw std::runtime_error("Could not parse literal '" + s + "'");
}
return result;
}
template <>
double parse_string<double>(const std::string& s)
{
const char* tmp = s.c_str();
char* end;
double result = strtod(tmp, &end);
if (*end != 0)
{
throw std::runtime_error("Could not parse literal '" + s + "'");
}
return result;
}
}
......@@ -183,6 +183,13 @@ namespace ngraph
return result;
}
/// template specializations for float and double to handle INFINITY, -INFINITY
/// and NaN values.
template <>
float parse_string<float>(const std::string& s);
template <>
double parse_string<double>(const std::string& s);
/// Parses a list of strings containing literals of the underlying type.
template <typename T>
std::vector<T> parse_string(const std::vector<std::string>& ss)
......@@ -262,7 +269,7 @@ namespace ngraph
/**
* This utility takes forward-propogation and back-propogation XLAunctions
* and turns them into clone functions where the intermediate values of
* and turns them into clone functions where the intermediate values of
* the forward prop are added to the output of fprop and the input of the bprop
* to avoid repeat calcualtions.
* The last argument is the adjoints coming into the bprop function, the output
......
......@@ -26,6 +26,7 @@
using namespace std;
using namespace ngraph;
using json = nlohmann::json;
TEST(serialize, main)
{
......
......@@ -340,3 +340,20 @@ TEST(util, round_up)
EXPECT_EQ(4, round_up(4, 4));
EXPECT_EQ(8, round_up(5, 4));
}
TEST(util, parse_string)
{
EXPECT_FLOAT_EQ(2, parse_string<float>("2"));
EXPECT_FLOAT_EQ(2.125, parse_string<float>("2.125"));
EXPECT_FLOAT_EQ(numeric_limits<float>::infinity(), parse_string<float>("INFINITY"));
EXPECT_FLOAT_EQ(numeric_limits<float>::infinity(), parse_string<float>("infinity"));
EXPECT_FLOAT_EQ(-numeric_limits<float>::infinity(), parse_string<float>("-INFINITY"));
EXPECT_TRUE(isnan(parse_string<float>("NaN")));
EXPECT_FLOAT_EQ(2, parse_string<double>("2"));
EXPECT_FLOAT_EQ(2.125, parse_string<double>("2.125"));
EXPECT_FLOAT_EQ(numeric_limits<double>::infinity(), parse_string<double>("INFINITY"));
EXPECT_FLOAT_EQ(numeric_limits<double>::infinity(), parse_string<double>("infinity"));
EXPECT_FLOAT_EQ(-numeric_limits<double>::infinity(), parse_string<double>("-INFINITY"));
EXPECT_TRUE(isnan(parse_string<double>("NaN")));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment