#5843 MathLib::toLongNumber()/MathLib::toULongNumber() now use min/max values for bigint/biguint type in case the number to converted cannot be converted properly

This commit is contained in:
Alexander Mai 2014-05-21 21:36:17 +02:00
parent 4cad064c3c
commit cccddc20bf
1 changed files with 17 additions and 2 deletions

View File

@ -24,6 +24,7 @@
#include <cmath>
#include <cctype>
#include <limits>
#include <limits>
MathLib::biguint MathLib::toULongNumber(const std::string & str)
{
@ -64,7 +65,13 @@ MathLib::biguint MathLib::toULongNumber(const std::string & str)
}
if (isFloat(str)) {
return static_cast<biguint>(std::atof(str.c_str()));
// Things are going to be less precise now: the value can't b represented in the biguint type.
// Use min/max values as an approximation. See #5843
const double doubleval = std::atof(str.c_str());
if (doubleval > (double)std::numeric_limits<biguint>::max())
return std::numeric_limits<biguint>::max();
else
return static_cast<biguint>(doubleval);
}
biguint ret = 0;
@ -112,7 +119,15 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str)
}
if (isFloat(str)) {
return static_cast<bigint>(std::atof(str.c_str()));
// Things are going to be less precise now: the value can't be represented in the bigint type.
// Use min/max values as an approximation. See #5843
const double doubleval = std::atof(str.c_str());
if (doubleval > (double)std::numeric_limits<bigint>::max())
return std::numeric_limits<bigint>::max();
else if (doubleval < (double)std::numeric_limits<bigint>::min())
return std::numeric_limits<bigint>::min();
else
return static_cast<bigint>(doubleval);
}
bigint ret = 0;