2009-05-30 07:48:12 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2021-03-21 20:58:32 +01:00
|
|
|
* Copyright (C) 2007-2021 Cppcheck team.
|
2009-06-02 22:32:58 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-06-02 22:32:58 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-10-14 16:30:10 +02:00
|
|
|
#include "mathlib.h"
|
2020-05-23 07:16:49 +02:00
|
|
|
#include "errortypes.h"
|
2017-04-01 18:14:18 +02:00
|
|
|
#include "utils.h"
|
2009-06-02 22:32:58 +02:00
|
|
|
|
2021-05-02 13:43:56 +02:00
|
|
|
#include <simplecpp.h>
|
|
|
|
|
2009-08-17 21:24:17 +02:00
|
|
|
#include <cctype>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <cmath>
|
2015-11-29 10:49:10 +01:00
|
|
|
#include <cstdlib>
|
2013-05-09 18:50:24 +02:00
|
|
|
#include <limits>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <locale>
|
2021-02-13 19:10:45 +01:00
|
|
|
#include <stdexcept>
|
2015-07-10 20:36:38 +02:00
|
|
|
|
2015-07-14 20:48:08 +02:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER <= 1700 // VS2012 doesn't have std::isinf and std::isnan
|
|
|
|
#define ISINF(x) (!_finite(x))
|
|
|
|
#define ISNAN(x) (_isnan(x))
|
|
|
|
#elif defined(__INTEL_COMPILER)
|
|
|
|
#define ISINF(x) (isinf(x))
|
|
|
|
#define ISNAN(x) (isnan(x))
|
|
|
|
#else // Use C++11 functions
|
|
|
|
#define ISINF(x) (std::isinf(x))
|
|
|
|
#define ISNAN(x) (std::isnan(x))
|
|
|
|
#endif
|
|
|
|
|
2017-10-20 17:38:15 +02:00
|
|
|
const int MathLib::bigint_bits = 64;
|
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
MathLib::value::value(const std::string &s) :
|
2018-06-17 17:04:34 +02:00
|
|
|
mIntValue(0), mDoubleValue(0), mIsUnsigned(false)
|
2015-07-10 20:36:38 +02:00
|
|
|
{
|
|
|
|
if (MathLib::isFloat(s)) {
|
2020-11-01 11:47:34 +01:00
|
|
|
mType = MathLib::value::Type::FLOAT;
|
2018-06-17 17:04:34 +02:00
|
|
|
mDoubleValue = MathLib::toDoubleNumber(s);
|
2015-07-10 20:36:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MathLib::isInt(s))
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Invalid value: " + s);
|
2015-07-10 20:36:38 +02:00
|
|
|
|
2020-11-01 11:47:34 +01:00
|
|
|
mType = MathLib::value::Type::INT;
|
2018-06-17 17:04:34 +02:00
|
|
|
mIntValue = MathLib::toLongNumber(s);
|
2015-07-10 20:36:38 +02:00
|
|
|
|
2018-06-17 17:04:34 +02:00
|
|
|
if (isIntHex(s) && mIntValue < 0)
|
|
|
|
mIsUnsigned = true;
|
2016-03-18 10:30:57 +01:00
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
// read suffix
|
|
|
|
if (s.size() >= 2U) {
|
|
|
|
for (std::size_t i = s.size() - 1U; i > 0U; --i) {
|
2018-04-04 21:51:31 +02:00
|
|
|
const char c = s[i];
|
2015-07-10 20:36:38 +02:00
|
|
|
if (c == 'u' || c == 'U')
|
2018-06-17 17:04:34 +02:00
|
|
|
mIsUnsigned = true;
|
2015-07-10 20:36:38 +02:00
|
|
|
else if (c == 'l' || c == 'L') {
|
2020-11-01 11:47:34 +01:00
|
|
|
if (mType == MathLib::value::Type::INT)
|
|
|
|
mType = MathLib::value::Type::LONG;
|
|
|
|
else if (mType == MathLib::value::Type::LONG)
|
|
|
|
mType = MathLib::value::Type::LONGLONG;
|
2016-07-26 13:19:28 +02:00
|
|
|
} else if (i > 2U && c == '4' && s[i-1] == '6' && s[i-2] == 'i')
|
2020-11-01 11:47:34 +01:00
|
|
|
mType = MathLib::value::Type::LONGLONG;
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::value::str() const
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2020-11-01 11:47:34 +01:00
|
|
|
if (mType == MathLib::value::Type::FLOAT) {
|
2018-06-17 17:04:34 +02:00
|
|
|
if (ISNAN(mDoubleValue))
|
2015-07-10 20:36:38 +02:00
|
|
|
return "nan.0";
|
2018-06-17 17:04:34 +02:00
|
|
|
if (ISINF(mDoubleValue))
|
|
|
|
return (mDoubleValue > 0) ? "inf.0" : "-inf.0";
|
2015-07-14 20:48:08 +02:00
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
ostr.precision(9);
|
2018-06-17 17:04:34 +02:00
|
|
|
ostr << std::fixed << mDoubleValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
|
|
|
|
// remove trailing zeros
|
|
|
|
std::string ret(ostr.str());
|
|
|
|
std::string::size_type pos = ret.size() - 1U;
|
|
|
|
while (ret[pos] == '0')
|
|
|
|
pos--;
|
|
|
|
if (ret[pos] == '.')
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
return ret.substr(0, pos+1);
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mIsUnsigned)
|
|
|
|
ostr << static_cast<biguint>(mIntValue) << "U";
|
2016-03-18 10:30:57 +01:00
|
|
|
else
|
2018-06-17 17:04:34 +02:00
|
|
|
ostr << mIntValue;
|
2020-11-01 11:47:34 +01:00
|
|
|
if (mType == MathLib::value::Type::LONG)
|
2015-07-10 20:36:38 +02:00
|
|
|
ostr << "L";
|
2020-11-01 11:47:34 +01:00
|
|
|
else if (mType == MathLib::value::Type::LONGLONG)
|
2015-07-10 20:36:38 +02:00
|
|
|
ostr << "LL";
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathLib::value::promote(const MathLib::value &v)
|
|
|
|
{
|
|
|
|
if (isInt() && v.isInt()) {
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mType < v.mType) {
|
|
|
|
mType = v.mType;
|
|
|
|
mIsUnsigned = v.mIsUnsigned;
|
|
|
|
} else if (mType == v.mType) {
|
|
|
|
mIsUnsigned |= v.mIsUnsigned;
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
} else if (!isFloat()) {
|
2018-06-17 17:04:34 +02:00
|
|
|
mIsUnsigned = false;
|
|
|
|
mDoubleValue = mIntValue;
|
2020-11-01 11:47:34 +01:00
|
|
|
mType = MathLib::value::Type::FLOAT;
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathLib::value MathLib::value::calc(char op, const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
value temp(v1);
|
|
|
|
temp.promote(v2);
|
|
|
|
if (temp.isFloat()) {
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue += v2.getDoubleValue();
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '-':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue -= v2.getDoubleValue();
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '*':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue *= v2.getDoubleValue();
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '/':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue /= v2.getDoubleValue();
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
case '&':
|
|
|
|
case '|':
|
|
|
|
case '^':
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Invalid calculation");
|
2015-07-10 20:36:38 +02:00
|
|
|
default:
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Unhandled calculation");
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
2018-06-17 17:04:34 +02:00
|
|
|
} else if (temp.mIsUnsigned) {
|
2015-07-10 20:36:38 +02:00
|
|
|
switch (op) {
|
|
|
|
case '+':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue += (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '-':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue -= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '*':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue *= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '/':
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v2.mIntValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v1.mIntValue == std::numeric_limits<bigint>::min() && std::abs(v2.mIntValue)<=1)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division overflow");
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue /= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '%':
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v2.mIntValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue %= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '&':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue &= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '|':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue |= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '^':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue ^= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Unhandled calculation");
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue += v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '-':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue -= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '*':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue *= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '/':
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v2.mIntValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v1.mIntValue == std::numeric_limits<bigint>::min() && std::abs(v2.mIntValue)<=1)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division overflow");
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue /= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '%':
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v2.mIntValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue %= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '&':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue &= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '|':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue |= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '^':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue ^= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Unhandled calculation");
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int MathLib::value::compare(const MathLib::value &v) const
|
|
|
|
{
|
|
|
|
value temp(*this);
|
|
|
|
temp.promote(v);
|
|
|
|
|
|
|
|
if (temp.isFloat()) {
|
2018-06-17 17:04:34 +02:00
|
|
|
if (temp.mDoubleValue < v.getDoubleValue())
|
2015-07-10 20:36:38 +02:00
|
|
|
return -1;
|
2018-06-17 17:04:34 +02:00
|
|
|
if (temp.mDoubleValue > v.getDoubleValue())
|
2015-07-10 20:36:38 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:04:34 +02:00
|
|
|
if (temp.mIsUnsigned) {
|
|
|
|
if ((unsigned long long)mIntValue < (unsigned long long)v.mIntValue)
|
2015-07-10 20:36:38 +02:00
|
|
|
return -1;
|
2018-06-17 17:04:34 +02:00
|
|
|
if ((unsigned long long)mIntValue > (unsigned long long)v.mIntValue)
|
2015-07-10 20:36:38 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mIntValue < v.mIntValue)
|
2015-07-10 20:36:38 +02:00
|
|
|
return -1;
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mIntValue > v.mIntValue)
|
2015-07-10 20:36:38 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value MathLib::value::add(int v) const
|
|
|
|
{
|
|
|
|
MathLib::value temp(*this);
|
|
|
|
if (temp.isInt())
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue += v;
|
2015-07-10 20:36:38 +02:00
|
|
|
else
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue += v;
|
2015-07-10 20:36:38 +02:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2016-03-21 21:20:02 +01:00
|
|
|
MathLib::value MathLib::value::shiftLeft(const MathLib::value &v) const
|
|
|
|
{
|
|
|
|
if (!isInt() || !v.isInt())
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Shift operand is not integer");
|
2016-03-21 21:20:02 +01:00
|
|
|
MathLib::value ret(*this);
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v.mIntValue >= MathLib::bigint_bits) {
|
2017-10-21 21:04:14 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2018-06-17 17:04:34 +02:00
|
|
|
ret.mIntValue <<= v.mIntValue;
|
2016-03-21 21:20:02 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value MathLib::value::shiftRight(const MathLib::value &v) const
|
|
|
|
{
|
|
|
|
if (!isInt() || !v.isInt())
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Shift operand is not integer");
|
2016-03-21 21:20:02 +01:00
|
|
|
MathLib::value ret(*this);
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v.mIntValue >= MathLib::bigint_bits) {
|
2017-10-21 21:04:14 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2018-06-17 17:04:34 +02:00
|
|
|
ret.mIntValue >>= v.mIntValue;
|
2016-03-21 21:20:02 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
MathLib::biguint MathLib::toULongNumber(const std::string & str)
|
|
|
|
{
|
|
|
|
// hexadecimal numbers:
|
2015-07-26 08:06:56 +02:00
|
|
|
if (isIntHex(str)) {
|
2021-02-13 19:10:45 +01:00
|
|
|
try {
|
|
|
|
const biguint ret = std::stoull(str, nullptr, 16);
|
|
|
|
return ret;
|
|
|
|
} catch (const std::out_of_range& e) {
|
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::toULongNumber: out_of_range: " + str + " (" + e.what() +")");
|
|
|
|
}
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
// octal numbers:
|
|
|
|
if (isOct(str)) {
|
2021-02-13 19:10:45 +01:00
|
|
|
try {
|
|
|
|
const biguint ret = std::stoull(str, nullptr, 8);
|
|
|
|
return ret;
|
|
|
|
} catch (const std::out_of_range& e) {
|
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::toULongNumber: out_of_range: " + str + " (" + e.what() +")");
|
|
|
|
}
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
// binary numbers:
|
|
|
|
if (isBin(str)) {
|
|
|
|
biguint ret = 0;
|
|
|
|
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
|
2020-01-12 08:11:12 +01:00
|
|
|
if (str[i] != '1' && str[i] != '0')
|
|
|
|
break;
|
2014-04-15 06:32:25 +02:00
|
|
|
ret <<= 1;
|
|
|
|
if (str[i] == '1')
|
|
|
|
ret |= 1;
|
2014-04-14 20:30:38 +02:00
|
|
|
}
|
2014-04-15 06:32:25 +02:00
|
|
|
/* if (str[0] == '-')
|
|
|
|
ret = -ret; */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFloat(str)) {
|
2014-05-21 21:36:17 +02:00
|
|
|
// 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);
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
|
|
|
|
2021-02-13 19:10:45 +01:00
|
|
|
try {
|
|
|
|
const biguint ret = std::stoull(str, nullptr, 10);
|
|
|
|
return ret;
|
|
|
|
} catch (const std::out_of_range& e) {
|
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::toULongNumber: out_of_range: " + str + " (" + e.what() +")");
|
|
|
|
}
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2021-01-10 13:30:00 +01:00
|
|
|
unsigned int MathLib::encodeMultiChar(const std::string& str)
|
2015-11-28 16:37:26 +01:00
|
|
|
{
|
2018-03-30 20:57:25 +02:00
|
|
|
unsigned int retval = 0;
|
2019-09-19 20:29:33 +02:00
|
|
|
for (char it : str) {
|
|
|
|
retval = (retval << 8) | it;
|
2015-11-28 16:37:26 +01:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
MathLib::bigint MathLib::toLongNumber(const std::string & str)
|
|
|
|
{
|
|
|
|
// hexadecimal numbers:
|
2015-07-26 08:06:56 +02:00
|
|
|
if (isIntHex(str)) {
|
2021-02-13 19:10:45 +01:00
|
|
|
try {
|
2021-01-16 19:03:28 +01:00
|
|
|
const biguint ret = std::stoull(str, nullptr, 16);
|
2014-04-15 06:32:25 +02:00
|
|
|
return (bigint)ret;
|
2021-02-13 19:10:45 +01:00
|
|
|
} catch (const std::out_of_range& e) {
|
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::toLongNumber: out_of_range: " + str + " (" + e.what() +")");
|
2014-04-14 20:30:38 +02:00
|
|
|
}
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
// octal numbers:
|
|
|
|
if (isOct(str)) {
|
2021-02-13 19:10:45 +01:00
|
|
|
try {
|
|
|
|
const biguint ret = std::stoull(str, nullptr, 8);
|
|
|
|
return ret;
|
|
|
|
} catch (const std::out_of_range& e) {
|
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::toLongNumber: out_of_range: " + str + " (" + e.what() +")");
|
|
|
|
}
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// binary numbers:
|
|
|
|
if (isBin(str)) {
|
|
|
|
bigint ret = 0;
|
|
|
|
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
|
2020-01-12 08:11:12 +01:00
|
|
|
if (str[i] != '1' && str[i] != '0')
|
|
|
|
break;
|
2014-04-15 06:32:25 +02:00
|
|
|
ret <<= 1;
|
|
|
|
if (str[i] == '1')
|
|
|
|
ret |= 1;
|
|
|
|
}
|
|
|
|
if (str[0] == '-')
|
|
|
|
ret = -ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFloat(str)) {
|
2014-05-21 21:36:17 +02:00
|
|
|
// 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
|
2015-07-10 18:34:28 +02:00
|
|
|
const double doubleval = toDoubleNumber(str);
|
2014-05-21 21:36:17 +02:00
|
|
|
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);
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
|
|
|
|
2021-05-08 11:20:29 +02:00
|
|
|
if (isCharLiteral(str))
|
|
|
|
return simplecpp::characterLiteralToLL(str);
|
2015-11-20 10:10:38 +01:00
|
|
|
|
2021-02-13 19:10:45 +01:00
|
|
|
try {
|
|
|
|
const biguint ret = std::stoull(str, nullptr, 10);
|
2017-07-22 12:19:46 +02:00
|
|
|
return ret;
|
2021-02-13 19:10:45 +01:00
|
|
|
} catch (const std::out_of_range& e) {
|
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::toLongNumber: out_of_range: " + str + " (" + e.what() +")");
|
2017-07-22 12:19:46 +02:00
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 22:16:16 +01:00
|
|
|
// in-place conversion of (sub)string to double. Requires no heap.
|
|
|
|
static double myStod(const std::string& str, std::string::const_iterator from, std::string::const_iterator to, int base)
|
|
|
|
{
|
|
|
|
double result = 0.;
|
|
|
|
bool positivesign = true;
|
|
|
|
std::string::const_iterator it;
|
|
|
|
if ('+' == *from) {
|
|
|
|
it = from + 1;
|
|
|
|
} else if ('-' == *from) {
|
|
|
|
it = from + 1;
|
|
|
|
positivesign = false;
|
|
|
|
} else
|
|
|
|
it = from;
|
|
|
|
const std::size_t decimalsep = str.find('.', it-str.begin());
|
|
|
|
int distance;
|
|
|
|
if (std::string::npos == decimalsep) {
|
|
|
|
distance = to - it;
|
2021-08-07 20:51:18 +02:00
|
|
|
} else if (decimalsep > (to - str.begin()))
|
2018-12-06 22:16:16 +01:00
|
|
|
return 0.; // error handling??
|
|
|
|
else
|
|
|
|
distance = int(decimalsep)-(from - str.begin());
|
|
|
|
auto digitval = [&](char c) {
|
|
|
|
if ((10 < base) && (c > '9'))
|
|
|
|
return 10 + std::tolower(c) - 'a';
|
|
|
|
else
|
|
|
|
return c - '0';
|
|
|
|
};
|
|
|
|
for (; it!=to; ++it) {
|
|
|
|
if ('.' == *it)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
--distance;
|
|
|
|
result += digitval(*it)* std::pow(base, distance);
|
|
|
|
}
|
|
|
|
return (positivesign)?result:-result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Assuming a limited support of built-in hexadecimal floats (see C99, C++17) that is a fall-back implementation.
|
|
|
|
// Performance has been optimized WRT to heap activity, however the calculation part is not optimized.
|
2019-11-20 15:37:09 +01:00
|
|
|
static double floatHexToDoubleNumber(const std::string& str)
|
2018-12-06 22:16:16 +01:00
|
|
|
{
|
|
|
|
const std::size_t p = str.find_first_of("pP",3);
|
|
|
|
const double factor1 = myStod(str, str.begin() + 2, str.begin()+p, 16);
|
|
|
|
const bool suffix = (str.back() == 'f') || (str.back() == 'F') || (str.back() == 'l') || (str.back() == 'L');
|
|
|
|
const double exponent = myStod(str, str.begin() + p + 1, (suffix)?str.end()-1:str.end(), 10);
|
|
|
|
const double factor2 = std::pow(2, exponent);
|
|
|
|
return factor1 * factor2;
|
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
double MathLib::toDoubleNumber(const std::string &str)
|
|
|
|
{
|
2021-05-02 13:43:56 +02:00
|
|
|
if (isCharLiteral(str)) {
|
|
|
|
try {
|
|
|
|
return simplecpp::characterLiteralToLL(str);
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::toLongNumber: characterLiteralToLL(" + str + ") => " + e.what());
|
|
|
|
}
|
|
|
|
}
|
2015-07-26 08:06:56 +02:00
|
|
|
if (isIntHex(str))
|
2012-02-17 19:54:53 +01:00
|
|
|
return static_cast<double>(toLongNumber(str));
|
2010-04-02 20:42:06 +02:00
|
|
|
// nullcheck
|
2017-02-25 21:58:09 +01:00
|
|
|
if (isNullValue(str))
|
2010-04-02 20:42:06 +02:00
|
|
|
return 0.0;
|
2021-04-05 05:24:46 +02:00
|
|
|
#ifdef _LIBCPP_VERSION
|
2017-02-25 21:58:09 +01:00
|
|
|
if (isFloat(str)) // Workaround libc++ bug at http://llvm.org/bugs/show_bug.cgi?id=17782
|
2015-07-10 18:34:28 +02:00
|
|
|
// TODO : handle locale
|
2017-08-09 20:00:26 +02:00
|
|
|
return std::strtod(str.c_str(), nullptr);
|
2015-07-10 18:34:28 +02:00
|
|
|
#endif
|
2018-12-06 22:16:16 +01:00
|
|
|
if (isFloatHex(str))
|
2019-11-20 15:37:09 +01:00
|
|
|
return floatHexToDoubleNumber(str);
|
2010-04-02 20:42:06 +02:00
|
|
|
// otherwise, convert to double
|
2011-12-08 21:28:34 +01:00
|
|
|
std::istringstream istr(str);
|
2015-07-10 18:34:28 +02:00
|
|
|
istr.imbue(std::locale::classic());
|
2009-11-15 19:07:36 +01:00
|
|
|
double ret;
|
|
|
|
istr >> ret;
|
|
|
|
return ret;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 19:01:04 +01:00
|
|
|
template<> std::string MathLib::toString<double>(double value)
|
2012-09-11 16:50:42 +02:00
|
|
|
{
|
|
|
|
std::ostringstream result;
|
|
|
|
result.precision(12);
|
|
|
|
result << value;
|
|
|
|
if (result.str() == "-0")
|
|
|
|
return "0.0";
|
2015-08-15 20:24:26 +02:00
|
|
|
if (result.str().find('.') == std::string::npos)
|
2012-09-11 16:50:42 +02:00
|
|
|
return result.str() + ".0";
|
|
|
|
return result.str();
|
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isFloat(const std::string &str)
|
2015-07-26 19:21:53 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
return isDecimalFloat(str) || isFloatHex(str);
|
2015-07-26 19:21:53 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isDecimalFloat(const std::string &str)
|
2010-04-02 02:19:38 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
if (str.empty())
|
2014-02-20 16:56:49 +01:00
|
|
|
return false;
|
2019-07-17 10:43:18 +02:00
|
|
|
enum class State {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, BASE_DIGITS1, LEADING_DECIMAL, TRAILING_DECIMAL, BASE_DIGITS2, E, MANTISSA_PLUSMINUS, MANTISSA_DIGITS, SUFFIX_F, SUFFIX_L
|
2019-07-17 10:43:18 +02:00
|
|
|
} state = State::START;
|
2018-12-10 12:10:26 +01:00
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2014-02-24 18:52:31 +01:00
|
|
|
switch (state) {
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::START:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='.')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::LEADING_DECIMAL;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::BASE_DIGITS1;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::LEADING_DECIMAL:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (std::isdigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::BASE_DIGITS2;
|
2014-02-27 16:08:49 +01:00
|
|
|
else
|
|
|
|
return false;
|
2014-02-24 18:52:31 +01:00
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::BASE_DIGITS1:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='e' || *it=='E')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::E;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (*it=='.')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::TRAILING_DECIMAL;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (!std::isdigit(static_cast<unsigned char>(*it)))
|
2014-02-24 18:52:31 +01:00
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::TRAILING_DECIMAL:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='e' || *it=='E')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::E;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='f' || *it=='F')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_L;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::BASE_DIGITS2;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::BASE_DIGITS2:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='e' || *it=='E')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::E;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (*it=='f' || *it=='F')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_L;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (!std::isdigit(static_cast<unsigned char>(*it)))
|
2014-02-24 18:52:31 +01:00
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::E:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='+' || *it=='-')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::MANTISSA_PLUSMINUS;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::MANTISSA_DIGITS;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::MANTISSA_PLUSMINUS:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (!std::isdigit(static_cast<unsigned char>(*it)))
|
2014-02-24 18:52:31 +01:00
|
|
|
return false;
|
|
|
|
else
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::MANTISSA_DIGITS;
|
2014-02-24 18:52:31 +01:00
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::MANTISSA_DIGITS:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='f' || *it=='F')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_L;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (!std::isdigit(static_cast<unsigned char>(*it)))
|
2014-02-24 18:52:31 +01:00
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::SUFFIX_F:
|
2014-02-20 16:56:49 +01:00
|
|
|
return false;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::SUFFIX_L:
|
2014-11-17 06:53:32 +01:00
|
|
|
return false;
|
2014-02-24 18:52:31 +01:00
|
|
|
}
|
2014-02-20 16:56:49 +01:00
|
|
|
}
|
2019-07-17 10:43:18 +02:00
|
|
|
return (state==State::BASE_DIGITS2 || state==State::MANTISSA_DIGITS || state==State::TRAILING_DECIMAL || state==State::SUFFIX_F || state==State::SUFFIX_L);
|
2010-04-02 02:19:38 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isNegative(const std::string &str)
|
2010-04-02 02:19:38 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
if (str.empty())
|
2015-07-26 19:21:53 +02:00
|
|
|
return false;
|
2017-04-01 09:31:27 +02:00
|
|
|
return (str[0] == '-');
|
2012-02-17 19:54:53 +01:00
|
|
|
}
|
2010-04-02 07:32:03 +02:00
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isPositive(const std::string &str)
|
2013-09-29 15:11:09 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
if (str.empty())
|
2015-07-26 19:21:53 +02:00
|
|
|
return false;
|
2017-04-01 09:31:27 +02:00
|
|
|
return !MathLib::isNegative(str);
|
2013-09-29 15:11:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-20 15:37:09 +01:00
|
|
|
static bool isValidIntegerSuffixIt(std::string::const_iterator it, std::string::const_iterator end, bool supportMicrosoftExtensions=true)
|
2018-12-20 12:20:31 +01:00
|
|
|
{
|
2020-11-01 11:47:34 +01:00
|
|
|
enum class Status { START, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU, SUFFIX_I, SUFFIX_I6, SUFFIX_I64, SUFFIX_UI, SUFFIX_UI6, SUFFIX_UI64 } state = Status::START;
|
2018-12-20 12:20:31 +01:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
switch (state) {
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::START:
|
2018-12-20 12:20:31 +01:00
|
|
|
if (*it == 'u' || *it == 'U')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_U;
|
2018-12-20 12:20:31 +01:00
|
|
|
else if (*it == 'l' || *it == 'L')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_L;
|
2018-12-20 12:20:31 +01:00
|
|
|
else if (supportMicrosoftExtensions && (*it == 'i' || *it == 'I'))
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_I;
|
2018-12-20 12:20:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::SUFFIX_U:
|
2018-12-20 12:20:31 +01:00
|
|
|
if (*it == 'l' || *it == 'L')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_UL; // UL
|
2018-12-20 12:20:31 +01:00
|
|
|
else if (supportMicrosoftExtensions && (*it == 'i' || *it == 'I'))
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_UI;
|
2018-12-20 12:20:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::SUFFIX_UL:
|
2018-12-20 12:20:31 +01:00
|
|
|
if (*it == 'l' || *it == 'L')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_ULL; // ULL
|
2018-12-20 12:20:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::SUFFIX_L:
|
2018-12-20 12:20:31 +01:00
|
|
|
if (*it == 'u' || *it == 'U')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_LU; // LU
|
2018-12-20 12:20:31 +01:00
|
|
|
else if (*it == 'l' || *it == 'L')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_LL; // LL
|
2018-12-20 12:20:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::SUFFIX_LU:
|
2018-12-20 12:20:31 +01:00
|
|
|
return false;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::SUFFIX_LL:
|
2018-12-20 12:20:31 +01:00
|
|
|
if (*it == 'u' || *it == 'U')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_LLU; // LLU
|
2018-12-20 12:20:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::SUFFIX_I:
|
2018-12-20 12:20:31 +01:00
|
|
|
if (*it == '6')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_I6;
|
2018-12-20 12:20:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::SUFFIX_I6:
|
2018-12-20 12:20:31 +01:00
|
|
|
if (*it == '4')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_I64;
|
2018-12-20 12:20:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::SUFFIX_UI:
|
2018-12-20 12:20:31 +01:00
|
|
|
if (*it == '6')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_UI6;
|
2018-12-20 12:20:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::SUFFIX_UI6:
|
2018-12-20 12:20:31 +01:00
|
|
|
if (*it == '4')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::SUFFIX_UI64;
|
2018-12-20 12:20:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 11:47:34 +01:00
|
|
|
return ((state == Status::SUFFIX_U) ||
|
|
|
|
(state == Status::SUFFIX_L) ||
|
|
|
|
(state == Status::SUFFIX_UL) ||
|
|
|
|
(state == Status::SUFFIX_LU) ||
|
|
|
|
(state == Status::SUFFIX_LL) ||
|
|
|
|
(state == Status::SUFFIX_ULL) ||
|
|
|
|
(state == Status::SUFFIX_LLU) ||
|
|
|
|
(state == Status::SUFFIX_I64) ||
|
|
|
|
(state == Status::SUFFIX_UI64));
|
2018-12-20 12:20:31 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
// cppcheck-suppress unusedFunction
|
2018-12-20 12:20:31 +01:00
|
|
|
bool MathLib::isValidIntegerSuffix(const std::string& str, bool supportMicrosoftExtensions)
|
|
|
|
{
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(str.begin(), str.end(), supportMicrosoftExtensions);
|
2018-12-20 12:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-03-09 21:14:14 +01:00
|
|
|
/*! \brief Does the string represent an octal number?
|
|
|
|
* In case leading or trailing white space is provided, the function
|
|
|
|
* returns false.
|
|
|
|
* Additional information can be found here:
|
|
|
|
* http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
|
|
|
|
*
|
2017-04-01 09:31:27 +02:00
|
|
|
* \param str The string to check. In case the string is empty, the function returns false.
|
2014-03-09 21:14:14 +01:00
|
|
|
* \return Return true in case a octal number is provided and false otherwise.
|
|
|
|
**/
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isOct(const std::string& str)
|
2012-02-17 19:54:53 +01:00
|
|
|
{
|
2019-07-17 10:43:18 +02:00
|
|
|
enum class Status {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, OCTAL_PREFIX, DIGITS
|
2019-07-17 10:43:18 +02:00
|
|
|
} state = Status::START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2014-03-09 21:14:14 +01:00
|
|
|
switch (state) {
|
2019-07-17 10:43:18 +02:00
|
|
|
case Status::START:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (*it == '0')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = Status::OCTAL_PREFIX;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case Status::OCTAL_PREFIX:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isOctalDigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = Status::DIGITS;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case Status::DIGITS:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isOctalDigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = Status::DIGITS;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(it,str.end());
|
2014-03-09 21:14:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 10:43:18 +02:00
|
|
|
return state == Status::DIGITS;
|
2012-02-17 19:54:53 +01:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isIntHex(const std::string& str)
|
2012-02-17 19:54:53 +01:00
|
|
|
{
|
2020-11-01 11:47:34 +01:00
|
|
|
enum class Status {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, HEX_0, HEX_X, DIGIT
|
2020-11-01 11:47:34 +01:00
|
|
|
} state = Status::START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2014-03-11 20:02:04 +01:00
|
|
|
switch (state) {
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::START:
|
2014-03-11 20:02:04 +01:00
|
|
|
if (*it == '0')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::HEX_0;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::HEX_0:
|
2014-03-11 20:02:04 +01:00
|
|
|
if (*it == 'x' || *it == 'X')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::HEX_X;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::HEX_X:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::DIGIT;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
2018-12-19 14:39:04 +01:00
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::DIGIT:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
; // state = Status::DIGIT;
|
2014-03-14 11:46:41 +01:00
|
|
|
else
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(it,str.end());
|
2014-03-14 11:46:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 11:47:34 +01:00
|
|
|
return Status::DIGIT == state;
|
2014-03-14 11:46:41 +01:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isFloatHex(const std::string& str)
|
2015-07-26 08:06:56 +02:00
|
|
|
{
|
2020-11-01 11:47:34 +01:00
|
|
|
enum class Status {
|
2018-12-06 22:16:16 +01:00
|
|
|
START, HEX_0, HEX_X, WHOLE_NUMBER_DIGIT, POINT, FRACTION, EXPONENT_P, EXPONENT_SIGN, EXPONENT_DIGITS, EXPONENT_SUFFIX
|
2020-11-01 11:47:34 +01:00
|
|
|
} state = Status::START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2015-07-26 08:06:56 +02:00
|
|
|
switch (state) {
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::START:
|
2015-07-26 08:06:56 +02:00
|
|
|
if (*it == '0')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::HEX_0;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::HEX_0:
|
2015-07-26 08:06:56 +02:00
|
|
|
if (*it == 'x' || *it == 'X')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::HEX_X;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::HEX_X:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::WHOLE_NUMBER_DIGIT;
|
2018-12-06 22:16:16 +01:00
|
|
|
else if (*it == '.')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::POINT;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::WHOLE_NUMBER_DIGIT:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
; // state = Status::WHOLE_NUMBER_DIGITS;
|
2015-07-26 08:06:56 +02:00
|
|
|
else if (*it=='.')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::FRACTION;
|
2015-07-26 08:06:56 +02:00
|
|
|
else if (*it=='p' || *it=='P')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::EXPONENT_P;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::POINT:
|
|
|
|
case Status::FRACTION:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::FRACTION;
|
2018-12-06 22:16:16 +01:00
|
|
|
else if (*it == 'p' || *it == 'P')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::EXPONENT_P;
|
2018-12-06 22:16:16 +01:00
|
|
|
else
|
|
|
|
return false;
|
2015-07-26 08:06:56 +02:00
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::EXPONENT_P:
|
2018-12-06 22:16:16 +01:00
|
|
|
if (isdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::EXPONENT_DIGITS;
|
2018-12-06 22:16:16 +01:00
|
|
|
else if (*it == '+' || *it == '-')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::EXPONENT_SIGN;
|
2018-12-06 22:16:16 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::EXPONENT_SIGN:
|
2018-12-06 22:16:16 +01:00
|
|
|
if (isdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::EXPONENT_DIGITS;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::EXPONENT_DIGITS:
|
2018-12-06 22:16:16 +01:00
|
|
|
if (isdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
; // state = Status::EXPONENT_DIGITS;
|
2018-12-06 22:16:16 +01:00
|
|
|
else if (*it == 'f' || *it == 'F' || *it == 'l' || *it == 'L')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::EXPONENT_SUFFIX;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
2018-12-06 22:16:16 +01:00
|
|
|
return false;
|
2015-07-26 08:06:56 +02:00
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::EXPONENT_SUFFIX:
|
2018-12-06 22:16:16 +01:00
|
|
|
return false;
|
2015-07-26 08:06:56 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-01 11:47:34 +01:00
|
|
|
return (Status::EXPONENT_DIGITS == state) || (Status::EXPONENT_SUFFIX == state);
|
2015-07-26 08:06:56 +02:00
|
|
|
}
|
|
|
|
|
2012-02-17 19:54:53 +01:00
|
|
|
|
2014-03-10 17:23:07 +01:00
|
|
|
/*! \brief Does the string represent a binary number?
|
|
|
|
* In case leading or trailing white space is provided, the function
|
|
|
|
* returns false.
|
|
|
|
* Additional information can be found here:
|
|
|
|
* http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
|
|
|
|
*
|
2017-04-01 09:31:27 +02:00
|
|
|
* \param str The string to check. In case the string is empty, the function returns false.
|
2014-03-10 17:23:07 +01:00
|
|
|
* \return Return true in case a binary number is provided and false otherwise.
|
|
|
|
**/
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isBin(const std::string& str)
|
2012-09-10 21:13:32 +02:00
|
|
|
{
|
2020-11-01 11:47:34 +01:00
|
|
|
enum class Status {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, GNU_BIN_PREFIX_0, GNU_BIN_PREFIX_B, DIGIT
|
2020-11-01 11:47:34 +01:00
|
|
|
} state = Status::START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2014-03-10 17:23:07 +01:00
|
|
|
switch (state) {
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::START:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == '0')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::GNU_BIN_PREFIX_0;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::GNU_BIN_PREFIX_0:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == 'b' || *it == 'B')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::GNU_BIN_PREFIX_B;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::GNU_BIN_PREFIX_B:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == '0' || *it == '1')
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::DIGIT;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::DIGIT:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == '0' || *it == '1')
|
2020-11-01 11:47:34 +01:00
|
|
|
; // state = Status::DIGIT;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(it,str.end());
|
2014-03-10 17:23:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 11:47:34 +01:00
|
|
|
return state == Status::DIGIT;
|
2012-09-10 21:13:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isDec(const std::string & str)
|
2009-06-02 22:32:58 +02:00
|
|
|
{
|
2020-11-01 11:47:34 +01:00
|
|
|
enum class Status {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, DIGIT
|
2020-11-01 11:47:34 +01:00
|
|
|
} state = Status::START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2014-04-10 22:46:55 +02:00
|
|
|
switch (state) {
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::START:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::DIGIT;
|
2014-04-10 22:46:55 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2020-11-01 11:47:34 +01:00
|
|
|
case Status::DIGIT:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isdigit(static_cast<unsigned char>(*it)))
|
2020-11-01 11:47:34 +01:00
|
|
|
state = Status::DIGIT;
|
2014-04-10 22:46:55 +02:00
|
|
|
else
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(it,str.end());
|
2014-04-10 22:46:55 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-08-16 18:45:26 +02:00
|
|
|
}
|
2020-11-01 11:47:34 +01:00
|
|
|
return state == Status::DIGIT;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isInt(const std::string & str)
|
2014-04-11 22:15:25 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
return isDec(str) || isIntHex(str) || isOct(str) || isBin(str);
|
2014-04-11 22:15:25 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 14:47:58 +02:00
|
|
|
std::string MathLib::getSuffix(const std::string& value)
|
2015-06-15 19:01:56 +02:00
|
|
|
{
|
2016-07-26 13:19:28 +02:00
|
|
|
if (value.size() > 3 && value[value.size() - 3] == 'i' && value[value.size() - 2] == '6' && value[value.size() - 1] == '4') {
|
|
|
|
if (value[value.size() - 4] == 'u')
|
|
|
|
return "ULL";
|
|
|
|
return "LL";
|
2015-06-15 19:01:56 +02:00
|
|
|
}
|
2016-07-26 13:19:28 +02:00
|
|
|
bool isUnsigned = false;
|
|
|
|
unsigned int longState = 0;
|
|
|
|
for (std::size_t i = 1U; i < value.size(); ++i) {
|
2018-05-29 13:24:48 +02:00
|
|
|
const char c = value[value.size() - i];
|
2016-07-26 13:19:28 +02:00
|
|
|
if (c == 'u' || c == 'U')
|
|
|
|
isUnsigned = true;
|
|
|
|
else if (c == 'L' || c == 'l')
|
|
|
|
longState++;
|
|
|
|
else break;
|
2015-06-15 19:01:56 +02:00
|
|
|
}
|
2016-07-26 13:19:28 +02:00
|
|
|
if (longState == 0)
|
|
|
|
return isUnsigned ? "U" : "";
|
|
|
|
if (longState == 1)
|
|
|
|
return isUnsigned ? "UL" : "L";
|
|
|
|
if (longState == 2)
|
|
|
|
return isUnsigned ? "ULL" : "LL";
|
|
|
|
else return "";
|
|
|
|
}
|
2015-06-15 19:01:56 +02:00
|
|
|
|
2016-07-26 13:19:28 +02:00
|
|
|
static std::string intsuffix(const std::string & first, const std::string & second)
|
|
|
|
{
|
2017-05-05 14:47:58 +02:00
|
|
|
const std::string suffix1 = MathLib::getSuffix(first);
|
|
|
|
const std::string suffix2 = MathLib::getSuffix(second);
|
2016-07-26 13:19:28 +02:00
|
|
|
if (suffix1 == "ULL" || suffix2 == "ULL")
|
2015-06-15 19:01:56 +02:00
|
|
|
return "ULL";
|
|
|
|
if (suffix1 == "LL" || suffix2 == "LL")
|
|
|
|
return "LL";
|
2016-07-26 13:19:28 +02:00
|
|
|
if (suffix1 == "UL" || suffix2 == "UL")
|
2015-06-15 19:01:56 +02:00
|
|
|
return "UL";
|
|
|
|
if (suffix1 == "L" || suffix2 == "L")
|
|
|
|
return "L";
|
|
|
|
if (suffix1 == "U" || suffix2 == "U")
|
|
|
|
return "U";
|
|
|
|
|
|
|
|
return suffix1.empty() ? suffix2 : suffix1;
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
std::string MathLib::add(const std::string & first, const std::string & second)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) + value(second)).str();
|
|
|
|
#else
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) + toLongNumber(second)) + intsuffix(first, second);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2012-08-14 16:17:18 +02:00
|
|
|
|
|
|
|
double d1 = toDoubleNumber(first);
|
|
|
|
double d2 = toDoubleNumber(second);
|
2012-08-22 20:50:39 +02:00
|
|
|
|
|
|
|
int count = 0;
|
2013-09-21 18:48:48 +02:00
|
|
|
while (d1 > 100000.0 * d2 && toString(d1+d2)==first && ++count<5)
|
2012-08-14 16:17:18 +02:00
|
|
|
d2 *= 10.0;
|
2013-09-21 18:48:48 +02:00
|
|
|
while (d2 > 100000.0 * d1 && toString(d1+d2)==second && ++count<5)
|
2012-08-14 16:17:18 +02:00
|
|
|
d1 *= 10.0;
|
|
|
|
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(d1 + d2);
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::subtract(const std::string &first, const std::string &second)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) - value(second)).str();
|
|
|
|
#else
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) - toLongNumber(second)) + intsuffix(first, second);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2012-08-14 16:17:18 +02:00
|
|
|
|
2012-09-05 13:57:57 +02:00
|
|
|
if (first == second)
|
2021-08-07 20:51:18 +02:00
|
|
|
return "0.0";
|
2012-09-05 13:57:57 +02:00
|
|
|
|
2012-08-14 16:17:18 +02:00
|
|
|
double d1 = toDoubleNumber(first);
|
|
|
|
double d2 = toDoubleNumber(second);
|
2012-08-22 20:50:39 +02:00
|
|
|
|
|
|
|
int count = 0;
|
2013-09-21 18:48:48 +02:00
|
|
|
while (d1 > 100000.0 * d2 && toString(d1-d2)==first && ++count<5)
|
2012-08-14 16:17:18 +02:00
|
|
|
d2 *= 10.0;
|
2013-09-21 18:48:48 +02:00
|
|
|
while (d2 > 100000.0 * d1 && toString(d1-d2)==second && ++count<5)
|
2012-08-14 16:17:18 +02:00
|
|
|
d1 *= 10.0;
|
|
|
|
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(d1 - d2);
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2014-05-29 18:25:01 +02:00
|
|
|
std::string MathLib::incdec(const std::string & var, const std::string & op)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
if (op == "++")
|
|
|
|
return value(var).add(1).str();
|
|
|
|
else if (op == "--")
|
|
|
|
return value(var).add(-1).str();
|
|
|
|
#else
|
2014-05-29 18:25:01 +02:00
|
|
|
if (op == "++")
|
|
|
|
return MathLib::add(var, "1");
|
|
|
|
else if (op == "--")
|
|
|
|
return MathLib::subtract(var, "1");
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
2014-05-29 18:25:01 +02:00
|
|
|
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, std::string("Unexpected operation '") + op + "' in MathLib::incdec(). Please report this to Cppcheck developers.");
|
2014-05-29 18:25:01 +02:00
|
|
|
}
|
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
std::string MathLib::divide(const std::string &first, const std::string &second)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) / value(second)).str();
|
|
|
|
#else
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2013-09-29 15:49:02 +02:00
|
|
|
const bigint a = toLongNumber(first);
|
|
|
|
const bigint b = toLongNumber(second);
|
2012-09-06 20:15:32 +02:00
|
|
|
if (b == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2015-06-20 23:15:35 +02:00
|
|
|
if (a == std::numeric_limits<bigint>::min() && std::abs(b)<=1)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division overflow");
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) / b) + intsuffix(first, second);
|
2014-05-31 18:25:23 +02:00
|
|
|
} else if (isNullValue(second)) {
|
|
|
|
if (isNullValue(first))
|
2013-11-04 11:56:43 +01:00
|
|
|
return "nan.0";
|
2015-07-15 17:36:41 +02:00
|
|
|
return isPositive(first) ? "inf.0" : "-inf.0";
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toDoubleNumber(first) / toDoubleNumber(second));
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::multiply(const std::string &first, const std::string &second)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) * value(second)).str();
|
|
|
|
#else
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) * toLongNumber(second)) + intsuffix(first, second);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toDoubleNumber(first) * toDoubleNumber(second));
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2012-04-12 14:02:09 +02:00
|
|
|
std::string MathLib::mod(const std::string &first, const std::string &second)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) % value(second)).str();
|
|
|
|
#else
|
2012-04-12 14:02:09 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2015-04-07 17:21:28 +02:00
|
|
|
const bigint b = toLongNumber(second);
|
2012-09-06 20:15:32 +02:00
|
|
|
if (b == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) % b) + intsuffix(first, second);
|
2012-04-12 14:02:09 +02:00
|
|
|
}
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::fmod(toDoubleNumber(first),toDoubleNumber(second)));
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
2012-04-12 14:02:09 +02:00
|
|
|
}
|
|
|
|
|
2012-01-08 21:19:44 +01:00
|
|
|
std::string MathLib::calculate(const std::string &first, const std::string &second, char action)
|
2009-08-02 14:22:39 +02:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
switch (action) {
|
2009-08-02 21:11:17 +02:00
|
|
|
case '+':
|
2012-02-17 19:54:53 +01:00
|
|
|
return MathLib::add(first, second);
|
2009-08-02 21:11:17 +02:00
|
|
|
|
|
|
|
case '-':
|
2012-02-17 19:54:53 +01:00
|
|
|
return MathLib::subtract(first, second);
|
2009-08-02 21:11:17 +02:00
|
|
|
|
|
|
|
case '*':
|
2012-02-17 19:54:53 +01:00
|
|
|
return MathLib::multiply(first, second);
|
2009-08-02 21:11:17 +02:00
|
|
|
|
|
|
|
case '/':
|
2012-02-17 19:54:53 +01:00
|
|
|
return MathLib::divide(first, second);
|
2009-08-02 21:11:17 +02:00
|
|
|
|
2012-04-12 14:02:09 +02:00
|
|
|
case '%':
|
|
|
|
return MathLib::mod(first, second);
|
|
|
|
|
2012-05-16 18:48:33 +02:00
|
|
|
case '&':
|
2015-06-15 19:01:56 +02:00
|
|
|
return MathLib::toString(MathLib::toLongNumber(first) & MathLib::toLongNumber(second)) + intsuffix(first,second);
|
2012-05-16 18:48:33 +02:00
|
|
|
|
|
|
|
case '|':
|
2015-06-15 19:01:56 +02:00
|
|
|
return MathLib::toString(MathLib::toLongNumber(first) | MathLib::toLongNumber(second)) + intsuffix(first,second);
|
2012-05-16 18:48:33 +02:00
|
|
|
|
|
|
|
case '^':
|
2015-06-15 19:01:56 +02:00
|
|
|
return MathLib::toString(MathLib::toLongNumber(first) ^ MathLib::toLongNumber(second)) + intsuffix(first,second);
|
2012-05-16 18:48:33 +02:00
|
|
|
|
2009-08-02 21:11:17 +02:00
|
|
|
default:
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, std::string("Unexpected action '") + action + "' in MathLib::calculate(). Please report this to Cppcheck developers.");
|
2009-08-02 14:22:39 +02:00
|
|
|
}
|
|
|
|
}
|
2009-06-02 22:32:58 +02:00
|
|
|
|
|
|
|
std::string MathLib::sin(const std::string &tok)
|
|
|
|
{
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::sin(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string MathLib::cos(const std::string &tok)
|
|
|
|
{
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::cos(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::tan(const std::string &tok)
|
|
|
|
{
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::tan(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string MathLib::abs(const std::string &tok)
|
|
|
|
{
|
2020-02-19 07:51:39 +01:00
|
|
|
if (isNegative(tok))
|
|
|
|
return tok.substr(1, tok.length() - 1);
|
|
|
|
return tok;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2011-07-17 05:05:35 +02:00
|
|
|
bool MathLib::isEqual(const std::string &first, const std::string &second)
|
|
|
|
{
|
2013-01-16 15:37:07 +01:00
|
|
|
// this conversion is needed for formatting
|
2013-06-12 22:13:05 +02:00
|
|
|
// e.g. if first=0.1 and second=1.0E-1, the direct comparison of the strings would fail
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toDoubleNumber(first)) == toString(toDoubleNumber(second));
|
2011-07-17 05:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isNotEqual(const std::string &first, const std::string &second)
|
|
|
|
{
|
2011-07-24 21:35:21 +02:00
|
|
|
return !isEqual(first, second);
|
2011-07-17 05:05:35 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
// cppcheck-suppress unusedFunction
|
2009-06-02 22:32:58 +02:00
|
|
|
bool MathLib::isGreater(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) > toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
// cppcheck-suppress unusedFunction
|
2011-07-17 05:05:35 +02:00
|
|
|
bool MathLib::isGreaterEqual(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) >= toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
// cppcheck-suppress unusedFunction
|
2011-07-17 05:05:35 +02:00
|
|
|
bool MathLib::isLess(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) < toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isLessEqual(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) <= toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
2014-04-14 05:27:38 +02:00
|
|
|
/*! \brief Does the string represent the numerical value of 0?
|
|
|
|
* In case leading or trailing white space is provided, the function
|
|
|
|
* returns false.
|
|
|
|
* Requirement for this function:
|
|
|
|
* - This code is allowed to be slow because of simplicity of the code.
|
|
|
|
*
|
|
|
|
* \param[in] str The string to check. In case the string is empty, the function returns false.
|
|
|
|
* \return Return true in case the string represents a numerical null value.
|
|
|
|
**/
|
|
|
|
bool MathLib::isNullValue(const std::string &str)
|
2010-04-02 22:41:54 +02:00
|
|
|
{
|
2019-05-28 08:49:59 +02:00
|
|
|
if (str.empty() || (!std::isdigit(static_cast<unsigned char>(str[0])) && (str[0] != '.' && str[0] != '-' && str[0] != '+')))
|
2014-04-14 05:27:38 +02:00
|
|
|
return false; // Has to be a number
|
|
|
|
|
2020-02-09 11:16:08 +01:00
|
|
|
if (!isInt(str) && !isFloat(str))
|
|
|
|
return false;
|
2020-02-02 16:40:36 +01:00
|
|
|
bool isHex = isIntHex(str) || isFloatHex(str);
|
2019-09-19 20:29:33 +02:00
|
|
|
for (char i : str) {
|
|
|
|
if (std::isdigit(static_cast<unsigned char>(i)) && i != '0') // May not contain digits other than 0
|
2014-04-11 12:00:33 +02:00
|
|
|
return false;
|
2020-02-02 16:40:36 +01:00
|
|
|
if (i == 'p' || i == 'P' || (!isHex && (i == 'E' || i == 'e')))
|
2014-04-14 05:27:38 +02:00
|
|
|
return true;
|
2020-02-02 16:40:36 +01:00
|
|
|
if (isHex && isxdigit(i) && i != '0')
|
|
|
|
return false;
|
2014-04-11 12:00:33 +02:00
|
|
|
}
|
2014-04-14 05:27:38 +02:00
|
|
|
return true;
|
2010-04-02 22:41:54 +02:00
|
|
|
}
|
|
|
|
|
2009-09-27 16:04:10 +02:00
|
|
|
bool MathLib::isOctalDigit(char c)
|
|
|
|
{
|
2014-04-11 12:00:33 +02:00
|
|
|
return (c >= '0' && c <= '7');
|
2009-09-27 16:04:10 +02:00
|
|
|
}
|
2015-07-10 20:36:38 +02:00
|
|
|
|
2016-06-05 14:14:28 +02:00
|
|
|
bool MathLib::isDigitSeparator(const std::string& iCode, std::string::size_type iPos)
|
|
|
|
{
|
2016-06-04 22:55:55 +02:00
|
|
|
if (iPos == 0 || iPos >= iCode.size() || iCode[iPos] != '\'')
|
|
|
|
return false;
|
|
|
|
std::string::size_type i = iPos - 1;
|
|
|
|
while (std::isxdigit(iCode[i])) {
|
|
|
|
if (i == 0)
|
|
|
|
return true; // Only xdigits before '
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
if (i == iPos - 1) { // No xdigit before '
|
|
|
|
return false;
|
|
|
|
} else {
|
2016-06-05 14:14:28 +02:00
|
|
|
switch (iCode[i]) {
|
2016-06-04 22:55:55 +02:00
|
|
|
case ' ':
|
|
|
|
case '.':
|
|
|
|
case ',':
|
|
|
|
case 'x':
|
|
|
|
case '(':
|
|
|
|
case '{':
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '*':
|
|
|
|
case '%':
|
|
|
|
case '/':
|
|
|
|
case '&':
|
|
|
|
case '|':
|
|
|
|
case '^':
|
|
|
|
case '~':
|
|
|
|
case '=':
|
|
|
|
return true;
|
2016-07-01 23:22:16 +02:00
|
|
|
case '\'':
|
|
|
|
return isDigitSeparator(iCode, i);
|
2016-06-04 22:55:55 +02:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
MathLib::value operator+(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('+',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator-(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('-',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator*(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('*',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator/(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('/',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator%(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('%',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator&(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('&',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator|(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('|',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator^(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('^',v1,v2);
|
|
|
|
}
|
2016-03-21 21:20:02 +01:00
|
|
|
|
|
|
|
MathLib::value operator<<(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return v1.shiftLeft(v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator>>(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return v1.shiftRight(v2);
|
|
|
|
}
|