Rename private member variables
This commit is contained in:
parent
610b26bfbc
commit
aa17b1f79a
142
lib/mathlib.cpp
142
lib/mathlib.cpp
|
@ -41,36 +41,36 @@
|
||||||
const int MathLib::bigint_bits = 64;
|
const int MathLib::bigint_bits = 64;
|
||||||
|
|
||||||
MathLib::value::value(const std::string &s) :
|
MathLib::value::value(const std::string &s) :
|
||||||
intValue(0), doubleValue(0), isUnsigned(false)
|
mIntValue(0), mDoubleValue(0), mIsUnsigned(false)
|
||||||
{
|
{
|
||||||
if (MathLib::isFloat(s)) {
|
if (MathLib::isFloat(s)) {
|
||||||
type = MathLib::value::FLOAT;
|
mType = MathLib::value::FLOAT;
|
||||||
doubleValue = MathLib::toDoubleNumber(s);
|
mDoubleValue = MathLib::toDoubleNumber(s);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!MathLib::isInt(s))
|
if (!MathLib::isInt(s))
|
||||||
throw InternalError(nullptr, "Invalid value: " + s);
|
throw InternalError(nullptr, "Invalid value: " + s);
|
||||||
|
|
||||||
type = MathLib::value::INT;
|
mType = MathLib::value::INT;
|
||||||
intValue = MathLib::toLongNumber(s);
|
mIntValue = MathLib::toLongNumber(s);
|
||||||
|
|
||||||
if (isIntHex(s) && intValue < 0)
|
if (isIntHex(s) && mIntValue < 0)
|
||||||
isUnsigned = true;
|
mIsUnsigned = true;
|
||||||
|
|
||||||
// read suffix
|
// read suffix
|
||||||
if (s.size() >= 2U) {
|
if (s.size() >= 2U) {
|
||||||
for (std::size_t i = s.size() - 1U; i > 0U; --i) {
|
for (std::size_t i = s.size() - 1U; i > 0U; --i) {
|
||||||
const char c = s[i];
|
const char c = s[i];
|
||||||
if (c == 'u' || c == 'U')
|
if (c == 'u' || c == 'U')
|
||||||
isUnsigned = true;
|
mIsUnsigned = true;
|
||||||
else if (c == 'l' || c == 'L') {
|
else if (c == 'l' || c == 'L') {
|
||||||
if (type == MathLib::value::INT)
|
if (mType == MathLib::value::INT)
|
||||||
type = MathLib::value::LONG;
|
mType = MathLib::value::LONG;
|
||||||
else if (type == MathLib::value::LONG)
|
else if (mType == MathLib::value::LONG)
|
||||||
type = MathLib::value::LONGLONG;
|
mType = MathLib::value::LONGLONG;
|
||||||
} else if (i > 2U && c == '4' && s[i-1] == '6' && s[i-2] == 'i')
|
} else if (i > 2U && c == '4' && s[i-1] == '6' && s[i-2] == 'i')
|
||||||
type = MathLib::value::LONGLONG;
|
mType = MathLib::value::LONGLONG;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,14 +78,14 @@ MathLib::value::value(const std::string &s) :
|
||||||
std::string MathLib::value::str() const
|
std::string MathLib::value::str() const
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
if (type == MathLib::value::FLOAT) {
|
if (mType == MathLib::value::FLOAT) {
|
||||||
if (ISNAN(doubleValue))
|
if (ISNAN(mDoubleValue))
|
||||||
return "nan.0";
|
return "nan.0";
|
||||||
if (ISINF(doubleValue))
|
if (ISINF(mDoubleValue))
|
||||||
return (doubleValue > 0) ? "inf.0" : "-inf.0";
|
return (mDoubleValue > 0) ? "inf.0" : "-inf.0";
|
||||||
|
|
||||||
ostr.precision(9);
|
ostr.precision(9);
|
||||||
ostr << std::fixed << doubleValue;
|
ostr << std::fixed << mDoubleValue;
|
||||||
|
|
||||||
// remove trailing zeros
|
// remove trailing zeros
|
||||||
std::string ret(ostr.str());
|
std::string ret(ostr.str());
|
||||||
|
@ -98,13 +98,13 @@ std::string MathLib::value::str() const
|
||||||
return ret.substr(0, pos+1);
|
return ret.substr(0, pos+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isUnsigned)
|
if (mIsUnsigned)
|
||||||
ostr << static_cast<biguint>(intValue) << "U";
|
ostr << static_cast<biguint>(mIntValue) << "U";
|
||||||
else
|
else
|
||||||
ostr << intValue;
|
ostr << mIntValue;
|
||||||
if (type == MathLib::value::LONG)
|
if (mType == MathLib::value::LONG)
|
||||||
ostr << "L";
|
ostr << "L";
|
||||||
else if (type == MathLib::value::LONGLONG)
|
else if (mType == MathLib::value::LONGLONG)
|
||||||
ostr << "LL";
|
ostr << "LL";
|
||||||
return ostr.str();
|
return ostr.str();
|
||||||
}
|
}
|
||||||
|
@ -112,16 +112,16 @@ std::string MathLib::value::str() const
|
||||||
void MathLib::value::promote(const MathLib::value &v)
|
void MathLib::value::promote(const MathLib::value &v)
|
||||||
{
|
{
|
||||||
if (isInt() && v.isInt()) {
|
if (isInt() && v.isInt()) {
|
||||||
if (type < v.type) {
|
if (mType < v.mType) {
|
||||||
type = v.type;
|
mType = v.mType;
|
||||||
isUnsigned = v.isUnsigned;
|
mIsUnsigned = v.mIsUnsigned;
|
||||||
} else if (type == v.type) {
|
} else if (mType == v.mType) {
|
||||||
isUnsigned |= v.isUnsigned;
|
mIsUnsigned |= v.mIsUnsigned;
|
||||||
}
|
}
|
||||||
} else if (!isFloat()) {
|
} else if (!isFloat()) {
|
||||||
isUnsigned = false;
|
mIsUnsigned = false;
|
||||||
doubleValue = intValue;
|
mDoubleValue = mIntValue;
|
||||||
type = MathLib::value::FLOAT;
|
mType = MathLib::value::FLOAT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,16 +133,16 @@ MathLib::value MathLib::value::calc(char op, const MathLib::value &v1, const Mat
|
||||||
if (temp.isFloat()) {
|
if (temp.isFloat()) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case '+':
|
case '+':
|
||||||
temp.doubleValue += v2.getDoubleValue();
|
temp.mDoubleValue += v2.getDoubleValue();
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
temp.doubleValue -= v2.getDoubleValue();
|
temp.mDoubleValue -= v2.getDoubleValue();
|
||||||
break;
|
break;
|
||||||
case '*':
|
case '*':
|
||||||
temp.doubleValue *= v2.getDoubleValue();
|
temp.mDoubleValue *= v2.getDoubleValue();
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
temp.doubleValue /= v2.getDoubleValue();
|
temp.mDoubleValue /= v2.getDoubleValue();
|
||||||
break;
|
break;
|
||||||
case '%':
|
case '%':
|
||||||
case '&':
|
case '&':
|
||||||
|
@ -152,37 +152,37 @@ MathLib::value MathLib::value::calc(char op, const MathLib::value &v1, const Mat
|
||||||
default:
|
default:
|
||||||
throw InternalError(nullptr, "Unhandled calculation");
|
throw InternalError(nullptr, "Unhandled calculation");
|
||||||
}
|
}
|
||||||
} else if (temp.isUnsigned) {
|
} else if (temp.mIsUnsigned) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case '+':
|
case '+':
|
||||||
temp.intValue += (unsigned long long)v2.intValue;
|
temp.mIntValue += (unsigned long long)v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
temp.intValue -= (unsigned long long)v2.intValue;
|
temp.mIntValue -= (unsigned long long)v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '*':
|
case '*':
|
||||||
temp.intValue *= (unsigned long long)v2.intValue;
|
temp.mIntValue *= (unsigned long long)v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
if (v2.intValue == 0)
|
if (v2.mIntValue == 0)
|
||||||
throw InternalError(nullptr, "Internal Error: Division by zero");
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
||||||
if (v1.intValue == std::numeric_limits<bigint>::min() && std::abs(v2.intValue)<=1)
|
if (v1.mIntValue == std::numeric_limits<bigint>::min() && std::abs(v2.mIntValue)<=1)
|
||||||
throw InternalError(nullptr, "Internal Error: Division overflow");
|
throw InternalError(nullptr, "Internal Error: Division overflow");
|
||||||
temp.intValue /= (unsigned long long)v2.intValue;
|
temp.mIntValue /= (unsigned long long)v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '%':
|
case '%':
|
||||||
if (v2.intValue == 0)
|
if (v2.mIntValue == 0)
|
||||||
throw InternalError(nullptr, "Internal Error: Division by zero");
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
||||||
temp.intValue %= (unsigned long long)v2.intValue;
|
temp.mIntValue %= (unsigned long long)v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '&':
|
case '&':
|
||||||
temp.intValue &= (unsigned long long)v2.intValue;
|
temp.mIntValue &= (unsigned long long)v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '|':
|
case '|':
|
||||||
temp.intValue |= (unsigned long long)v2.intValue;
|
temp.mIntValue |= (unsigned long long)v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '^':
|
case '^':
|
||||||
temp.intValue ^= (unsigned long long)v2.intValue;
|
temp.mIntValue ^= (unsigned long long)v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw InternalError(nullptr, "Unhandled calculation");
|
throw InternalError(nullptr, "Unhandled calculation");
|
||||||
|
@ -190,34 +190,34 @@ MathLib::value MathLib::value::calc(char op, const MathLib::value &v1, const Mat
|
||||||
} else {
|
} else {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case '+':
|
case '+':
|
||||||
temp.intValue += v2.intValue;
|
temp.mIntValue += v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
temp.intValue -= v2.intValue;
|
temp.mIntValue -= v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '*':
|
case '*':
|
||||||
temp.intValue *= v2.intValue;
|
temp.mIntValue *= v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
if (v2.intValue == 0)
|
if (v2.mIntValue == 0)
|
||||||
throw InternalError(nullptr, "Internal Error: Division by zero");
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
||||||
if (v1.intValue == std::numeric_limits<bigint>::min() && std::abs(v2.intValue)<=1)
|
if (v1.mIntValue == std::numeric_limits<bigint>::min() && std::abs(v2.mIntValue)<=1)
|
||||||
throw InternalError(nullptr, "Internal Error: Division overflow");
|
throw InternalError(nullptr, "Internal Error: Division overflow");
|
||||||
temp.intValue /= v2.intValue;
|
temp.mIntValue /= v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '%':
|
case '%':
|
||||||
if (v2.intValue == 0)
|
if (v2.mIntValue == 0)
|
||||||
throw InternalError(nullptr, "Internal Error: Division by zero");
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
||||||
temp.intValue %= v2.intValue;
|
temp.mIntValue %= v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '&':
|
case '&':
|
||||||
temp.intValue &= v2.intValue;
|
temp.mIntValue &= v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '|':
|
case '|':
|
||||||
temp.intValue |= v2.intValue;
|
temp.mIntValue |= v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
case '^':
|
case '^':
|
||||||
temp.intValue ^= v2.intValue;
|
temp.mIntValue ^= v2.mIntValue;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw InternalError(nullptr, "Unhandled calculation");
|
throw InternalError(nullptr, "Unhandled calculation");
|
||||||
|
@ -233,24 +233,24 @@ int MathLib::value::compare(const MathLib::value &v) const
|
||||||
temp.promote(v);
|
temp.promote(v);
|
||||||
|
|
||||||
if (temp.isFloat()) {
|
if (temp.isFloat()) {
|
||||||
if (temp.doubleValue < v.getDoubleValue())
|
if (temp.mDoubleValue < v.getDoubleValue())
|
||||||
return -1;
|
return -1;
|
||||||
if (temp.doubleValue > v.getDoubleValue())
|
if (temp.mDoubleValue > v.getDoubleValue())
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (temp.isUnsigned) {
|
if (temp.mIsUnsigned) {
|
||||||
if ((unsigned long long)intValue < (unsigned long long)v.intValue)
|
if ((unsigned long long)mIntValue < (unsigned long long)v.mIntValue)
|
||||||
return -1;
|
return -1;
|
||||||
if ((unsigned long long)intValue > (unsigned long long)v.intValue)
|
if ((unsigned long long)mIntValue > (unsigned long long)v.mIntValue)
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intValue < v.intValue)
|
if (mIntValue < v.mIntValue)
|
||||||
return -1;
|
return -1;
|
||||||
if (intValue > v.intValue)
|
if (mIntValue > v.mIntValue)
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -259,9 +259,9 @@ MathLib::value MathLib::value::add(int v) const
|
||||||
{
|
{
|
||||||
MathLib::value temp(*this);
|
MathLib::value temp(*this);
|
||||||
if (temp.isInt())
|
if (temp.isInt())
|
||||||
temp.intValue += v;
|
temp.mIntValue += v;
|
||||||
else
|
else
|
||||||
temp.doubleValue += v;
|
temp.mDoubleValue += v;
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,10 +270,10 @@ MathLib::value MathLib::value::shiftLeft(const MathLib::value &v) const
|
||||||
if (!isInt() || !v.isInt())
|
if (!isInt() || !v.isInt())
|
||||||
throw InternalError(nullptr, "Shift operand is not integer");
|
throw InternalError(nullptr, "Shift operand is not integer");
|
||||||
MathLib::value ret(*this);
|
MathLib::value ret(*this);
|
||||||
if (v.intValue >= MathLib::bigint_bits) {
|
if (v.mIntValue >= MathLib::bigint_bits) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
ret.intValue <<= v.intValue;
|
ret.mIntValue <<= v.mIntValue;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,10 +282,10 @@ MathLib::value MathLib::value::shiftRight(const MathLib::value &v) const
|
||||||
if (!isInt() || !v.isInt())
|
if (!isInt() || !v.isInt())
|
||||||
throw InternalError(nullptr, "Shift operand is not integer");
|
throw InternalError(nullptr, "Shift operand is not integer");
|
||||||
MathLib::value ret(*this);
|
MathLib::value ret(*this);
|
||||||
if (v.intValue >= MathLib::bigint_bits) {
|
if (v.mIntValue >= MathLib::bigint_bits) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
ret.intValue >>= v.intValue;
|
ret.mIntValue >>= v.mIntValue;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,10 +38,10 @@ public:
|
||||||
/** @brief value class */
|
/** @brief value class */
|
||||||
class value {
|
class value {
|
||||||
private:
|
private:
|
||||||
long long intValue;
|
long long mIntValue;
|
||||||
double doubleValue;
|
double mDoubleValue;
|
||||||
enum { INT, LONG, LONGLONG, FLOAT } type;
|
enum { INT, LONG, LONGLONG, FLOAT } mType;
|
||||||
bool isUnsigned;
|
bool mIsUnsigned;
|
||||||
|
|
||||||
void promote(const value &v);
|
void promote(const value &v);
|
||||||
|
|
||||||
|
@ -49,14 +49,14 @@ public:
|
||||||
explicit value(const std::string &s);
|
explicit value(const std::string &s);
|
||||||
std::string str() const;
|
std::string str() const;
|
||||||
bool isInt() const {
|
bool isInt() const {
|
||||||
return type != FLOAT;
|
return mType != FLOAT;
|
||||||
}
|
}
|
||||||
bool isFloat() const {
|
bool isFloat() const {
|
||||||
return type == FLOAT;
|
return mType == FLOAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
double getDoubleValue() const {
|
double getDoubleValue() const {
|
||||||
return isFloat() ? doubleValue : (double)intValue;
|
return isFloat() ? mDoubleValue : (double)mIntValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
static value calc(char op, const value &v1, const value &v2);
|
static value calc(char op, const value &v1, const value &v2);
|
||||||
|
|
Loading…
Reference in New Issue