Skip to content

Commit

Permalink
Utilities: have convertToDouble() return inf or -inf if a conve…
Browse files Browse the repository at this point in the history
…rsion cannot be done (closes #576).
  • Loading branch information
agarny committed Mar 19, 2020
2 parents d3b43dc + 5f3f24f commit 3942021
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ void Units::unitAttributes(size_t index, std::string &reference, std::string &pr
reference = u.mReference;
prefix = u.mPrefix;
if (!u.mExponent.empty()) {
exponent = std::stod(u.mExponent);
exponent = convertToDouble(u.mExponent);
} else {
exponent = 1.0;
}
if (!u.mMultiplier.empty()) {
multiplier = std::stod(u.mMultiplier);
multiplier = convertToDouble(u.mMultiplier);
} else {
multiplier = 1.0;
}
Expand Down
9 changes: 5 additions & 4 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ namespace libcellml {

double convertToDouble(const std::string &candidate)
{
double value = 0.0;
try {
value = std::stod(candidate);
return std::stod(candidate);
} catch (...) {
value = std::numeric_limits<double>::infinity();
if (*candidate.begin() == '-') {
return -std::numeric_limits<double>::infinity();
}
return std::numeric_limits<double>::infinity();
}
return value;
}

bool hasNonWhitespaceCharacters(const std::string &input)
Expand Down
2 changes: 2 additions & 0 deletions tests/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ TEST(Parser, modelWithInvalidUnits)
" <unit multiplier=\"Z\" exponent=\"35.0E+310\" units=\"kelvin\" bill=\"murray\">\n"
" <degrees/>\n"
" </unit>\n"
" <unit exponent=\"-35.0E+310\" units=\"kelvin\"/>\n"
" <bobshouse address=\"34 Rich Lane\"/>\n"
" <unit GUnit=\"50c\"/>\n"
" </units>\n"
Expand All @@ -596,6 +597,7 @@ TEST(Parser, modelWithInvalidUnits)
"<model xmlns=\"http://www.cellml.org/cellml/2.0#\" name=\"model_name\">\n"
" <units name=\"fahrenheitish\">\n"
" <unit exponent=\"inf\" units=\"kelvin\"/>\n"
" <unit exponent=\"-inf\" units=\"kelvin\"/>\n"
" <unit units=\"\"/>\n"
" </units>\n"
" <units>\n"
Expand Down

0 comments on commit 3942021

Please sign in to comment.