diff --git a/src/units.cpp b/src/units.cpp index 87bbd9b73..8a489cd26 100644 --- a/src/units.cpp +++ b/src/units.cpp @@ -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; } diff --git a/src/utilities.cpp b/src/utilities.cpp index 00f41f49a..c3cf3746e 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -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::infinity(); + if (*candidate.begin() == '-') { + return -std::numeric_limits::infinity(); + } + return std::numeric_limits::infinity(); } - return value; } bool hasNonWhitespaceCharacters(const std::string &input) diff --git a/tests/parser/parser.cpp b/tests/parser/parser.cpp index df17443df..db116259c 100644 --- a/tests/parser/parser.cpp +++ b/tests/parser/parser.cpp @@ -582,6 +582,7 @@ TEST(Parser, modelWithInvalidUnits) " \n" " \n" " \n" + " \n" " \n" " \n" " \n" @@ -596,6 +597,7 @@ TEST(Parser, modelWithInvalidUnits) "\n" " \n" " \n" + " \n" " \n" " \n" " \n"