2009-05-30 07:48:12 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 Daniel Marjamäki and 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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "mathlib.h"
|
2012-01-08 21:19:44 +01:00
|
|
|
#include "errorlogger.h"
|
2009-06-02 22:32:58 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cmath>
|
2009-08-17 21:24:17 +02:00
|
|
|
#include <cctype>
|
2009-06-02 22:32:58 +02:00
|
|
|
|
2010-11-20 10:05:33 +01:00
|
|
|
MathLib::bigint MathLib::toLongNumber(const std::string &str)
|
2009-06-02 22:32:58 +02:00
|
|
|
{
|
2010-11-20 10:05:33 +01:00
|
|
|
// hexadecimal numbers:
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str.compare(0, 2, "0x") == 0
|
|
|
|
|| str.compare(0, 3, "+0x") == 0
|
2011-10-13 20:53:06 +02:00
|
|
|
|| str.compare(0, 3, "-0x") == 0) {
|
2010-11-20 10:05:33 +01:00
|
|
|
bigint ret = 0;
|
2010-11-21 11:06:28 +01:00
|
|
|
std::istringstream istr(str.substr((str[0]=='0') ? 2U : 3U));
|
2010-11-20 10:05:33 +01:00
|
|
|
istr >> std::hex >> ret;
|
|
|
|
return (str[0]=='-') ? -ret : ret;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2010-11-20 10:05:33 +01:00
|
|
|
|
|
|
|
// octal numbers:
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str.compare(0, 1, "0") == 0
|
|
|
|
|| str.compare(0, 2, "+0") == 0
|
2011-10-13 20:53:06 +02:00
|
|
|
|| str.compare(0, 2, "-0") == 0) {
|
2010-11-20 10:05:33 +01:00
|
|
|
bigint ret = 0;
|
2010-11-21 11:06:28 +01:00
|
|
|
std::istringstream istr(str.substr((str[0]=='0') ? 1U : 2U));
|
2010-11-20 10:05:33 +01:00
|
|
|
istr >> std::oct >> ret;
|
|
|
|
return (str[0]=='-') ? -ret : ret;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2010-11-20 10:05:33 +01:00
|
|
|
|
|
|
|
if (str.find_first_of("eE") != std::string::npos)
|
|
|
|
return static_cast<bigint>(std::atof(str.c_str()));
|
|
|
|
|
|
|
|
bigint ret = 0;
|
|
|
|
std::istringstream istr(str);
|
|
|
|
istr >> ret;
|
|
|
|
return ret;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double MathLib::toDoubleNumber(const std::string &str)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (str.compare(0, 2, "0x") == 0) {
|
2009-08-13 22:13:52 +02:00
|
|
|
return std::strtoul(str.c_str(), '\0', 16);
|
|
|
|
}
|
2010-04-02 20:42:06 +02:00
|
|
|
// nullcheck
|
2010-04-02 22:41:54 +02:00
|
|
|
else if (isNullValue(str))
|
2010-04-02 20:42:06 +02:00
|
|
|
return 0.0;
|
|
|
|
// otherwise, convert to double
|
2011-12-08 21:28:34 +01:00
|
|
|
std::istringstream istr(str);
|
2009-11-15 19:07:36 +01:00
|
|
|
double ret;
|
|
|
|
istr >> ret;
|
|
|
|
return ret;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 02:19:38 +02:00
|
|
|
bool MathLib::isFloat(const std::string &s)
|
|
|
|
{
|
2010-04-02 07:32:03 +02:00
|
|
|
// every number that contains a . is a float
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s.find("." , 0) != std::string::npos)
|
2010-04-02 07:32:03 +02:00
|
|
|
return true;
|
|
|
|
// scientific notation
|
|
|
|
else if (s.find("E-", 0) != std::string::npos
|
|
|
|
|| s.find("e-", 0) != std::string::npos)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2010-04-02 02:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isNegative(const std::string &s)
|
|
|
|
{
|
|
|
|
// remember position
|
|
|
|
unsigned long n = 0;
|
|
|
|
// eat up whitespace
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::isspace(s[n])) ++n;
|
2010-04-02 07:32:03 +02:00
|
|
|
// every negative number has a negative sign
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s[n] == '-')
|
2010-04-02 07:32:03 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2010-04-02 02:19:38 +02:00
|
|
|
}
|
|
|
|
|
2009-08-16 18:45:26 +02:00
|
|
|
bool MathLib::isInt(const std::string & s)
|
2009-06-02 22:32:58 +02:00
|
|
|
{
|
2009-08-16 18:45:26 +02:00
|
|
|
// perform prechecks:
|
|
|
|
// ------------------
|
|
|
|
// first check, if a point is found, it is an floating point value
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s.find(".", 0) != std::string::npos) return false;
|
2009-08-16 18:45:26 +02:00
|
|
|
// check for scientific notation e.g. NumberE-Number this is obvious an floating point value
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (s.find("E-", 0) != std::string::npos || s.find("e-", 0) != std::string::npos) return false;
|
2009-08-16 18:45:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
// prechecking has nothing found,...
|
|
|
|
// gather information
|
2011-10-13 20:53:06 +02:00
|
|
|
enum Representation {
|
2011-10-18 23:56:35 +02:00
|
|
|
eScientific = 0, // NumberE+Number or NumberENumber
|
|
|
|
eOctal, // starts with 0
|
|
|
|
eHex, // starts with 0x
|
|
|
|
eDefault // Numbers with a (possible) trailing u or U or l or L for unsigned or long datatypes
|
2009-08-16 18:45:26 +02:00
|
|
|
};
|
|
|
|
// create an instance
|
|
|
|
Representation Mode = eDefault;
|
|
|
|
|
|
|
|
|
|
|
|
// remember position
|
|
|
|
unsigned long n = 0;
|
|
|
|
// eat up whitespace
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::isspace(s[n])) ++n;
|
2009-08-16 18:45:26 +02:00
|
|
|
|
|
|
|
// determine type
|
2011-10-13 20:53:06 +02:00
|
|
|
if (s.find("E", 0) != std::string::npos) {
|
2009-08-16 19:43:45 +02:00
|
|
|
Mode = eScientific;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (s.find("0x", n, 2) != std::string::npos) {
|
2009-08-16 19:43:45 +02:00
|
|
|
Mode = eHex;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (s.length() > 1 && s[0] == '0' && std::isdigit(s[1])) {
|
2009-08-16 19:43:45 +02:00
|
|
|
Mode = eOctal;
|
|
|
|
}
|
2009-08-16 18:45:26 +02:00
|
|
|
|
|
|
|
// check sign
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s[n] == '-' || s[n] == '+') ++n;
|
2009-08-16 18:45:26 +02:00
|
|
|
|
|
|
|
// check scientific notation
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Mode == eScientific) {
|
2009-08-16 18:45:26 +02:00
|
|
|
// check digits
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::isdigit(s[n])) ++n;
|
2009-08-16 18:45:26 +02:00
|
|
|
|
|
|
|
// check scientific notation
|
2011-10-13 20:53:06 +02:00
|
|
|
if (std::tolower(s[n]) == 'e') {
|
2009-08-16 18:45:26 +02:00
|
|
|
++n;
|
|
|
|
// check positive exponent
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s[n] == '+') ++n;
|
2009-08-16 18:45:26 +02:00
|
|
|
// floating pointer number e.g. 124E-2
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s[n] == '-') return false;
|
2009-08-16 18:45:26 +02:00
|
|
|
// check digits of the exponent
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::isdigit(s[n])) ++n;
|
2009-08-16 18:45:26 +02:00
|
|
|
}
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2009-08-16 18:45:26 +02:00
|
|
|
// check hex notation
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Mode == eHex) {
|
2009-08-16 18:45:26 +02:00
|
|
|
++n; // 0
|
|
|
|
++n; // x
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::isxdigit(s[n]))
|
2009-08-16 18:45:26 +02:00
|
|
|
++n;
|
|
|
|
}
|
|
|
|
// check octal notation
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Mode == eOctal) {
|
2010-04-02 07:30:58 +02:00
|
|
|
while (isOctalDigit(s[n]))
|
2009-08-16 18:45:26 +02:00
|
|
|
++n;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Mode == eDefault) {
|
2010-05-12 23:49:09 +02:00
|
|
|
// starts with digit
|
|
|
|
bool bStartsWithDigit=false;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (std::isdigit(s[n])) {
|
2010-05-12 23:49:09 +02:00
|
|
|
bStartsWithDigit=true;
|
|
|
|
++n;
|
|
|
|
};
|
2009-08-16 18:45:26 +02:00
|
|
|
// unsigned or long
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n;
|
2010-05-14 18:40:37 +02:00
|
|
|
|
2010-07-17 16:42:17 +02:00
|
|
|
if (bStartsWithDigit==false)
|
2010-05-12 23:49:09 +02:00
|
|
|
return false;
|
2009-08-16 18:45:26 +02:00
|
|
|
}
|
|
|
|
// eat up whitespace
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::isspace(s[n]))
|
2009-08-16 18:45:26 +02:00
|
|
|
++n;
|
|
|
|
|
|
|
|
// if everything goes good, we are at the end of the string and no digits/character
|
|
|
|
// is here --> return true, but if something was found eg. 12E+12AA return false
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s[n])
|
2009-08-16 18:45:26 +02:00
|
|
|
return false;
|
2009-06-02 22:32:58 +02:00
|
|
|
return true;
|
2009-08-16 18:45:26 +02:00
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::add(const std::string & first, const std::string & second)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2010-11-20 10:05:33 +01:00
|
|
|
return toString<bigint>(toLongNumber(first) + toLongNumber(second));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
return toString<double>(toDoubleNumber(first) + toDoubleNumber(second));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::subtract(const std::string &first, const std::string &second)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2010-11-20 10:05:33 +01:00
|
|
|
return toString<bigint>(toLongNumber(first) - toLongNumber(second));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
return toString<double>(toDoubleNumber(first) - toDoubleNumber(second));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::divide(const std::string &first, const std::string &second)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2010-11-20 10:05:33 +01:00
|
|
|
return toString<bigint>(toLongNumber(first) / toLongNumber(second));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
return toString<double>(toDoubleNumber(first) / toDoubleNumber(second));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::multiply(const std::string &first, const std::string &second)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2010-11-20 10:05:33 +01:00
|
|
|
return toString<bigint>(toLongNumber(first) * toLongNumber(second));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
return toString<double>(toDoubleNumber(first) * toDoubleNumber(second));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
std::string result("0");
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
switch (action) {
|
2009-08-02 21:11:17 +02:00
|
|
|
case '+':
|
|
|
|
result = MathLib::add(first, second);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
result = MathLib::subtract(first, second);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
result = MathLib::multiply(first, second);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
result = MathLib::divide(first, second);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-01-08 21:19:44 +01:00
|
|
|
throw InternalError(0, std::string("Unexpected action '") + action + "' in MathLib::calculate(). Please report this to Cppcheck developers.");
|
2009-08-02 14:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2009-06-02 22:32:58 +02:00
|
|
|
|
|
|
|
std::string MathLib::sin(const std::string &tok)
|
|
|
|
{
|
2009-08-13 22:13:52 +02:00
|
|
|
return toString<double>(std::sin(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string MathLib::cos(const std::string &tok)
|
|
|
|
{
|
2009-08-13 22:13:52 +02:00
|
|
|
return toString<double>(std::cos(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::tan(const std::string &tok)
|
|
|
|
{
|
2009-08-13 22:13:52 +02:00
|
|
|
return toString<double>(std::tan(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string MathLib::abs(const std::string &tok)
|
|
|
|
{
|
2009-08-13 22:13:52 +02:00
|
|
|
return toString<double>(std::abs(toDoubleNumber(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)
|
|
|
|
{
|
2011-07-29 23:37:40 +02:00
|
|
|
// this conversion is needed for formating
|
|
|
|
// e.g. if first=0.1 and second=1.0E-1, the direct comparison of the strings whould fail
|
|
|
|
return toString<double>(toDoubleNumber(first)) == toString<double>(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
|
|
|
}
|
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
bool MathLib::isGreater(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) > toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
2011-07-17 05:05:35 +02:00
|
|
|
bool MathLib::isGreaterEqual(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) >= toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-04-02 22:41:54 +02:00
|
|
|
bool MathLib::isNullValue(const std::string &str)
|
|
|
|
{
|
2010-04-02 22:44:24 +02:00
|
|
|
return (str == "-0" || str == "-0.0"
|
2010-04-05 19:57:54 +02:00
|
|
|
|| str == "0"
|
2010-04-02 22:44:24 +02:00
|
|
|
|| str == "-0." || str == "-0E-00"
|
|
|
|
|| str == "-0E+00" || str == "+0E+00"
|
|
|
|
|| str == "+0E-00" || str == "+0"
|
|
|
|
|| str == "+0.0" || str == "+0."
|
2011-11-08 21:49:25 +01:00
|
|
|
|| str == "0.0"
|
2010-04-02 22:44:24 +02:00
|
|
|
|| str == "+0e+00" || str == "-0e+00"
|
|
|
|
|| str == "+0e-00" || str == "-0e-00"
|
2011-11-08 21:49:25 +01:00
|
|
|
|| str == "-0E-0");
|
2010-04-02 22:41:54 +02:00
|
|
|
}
|
|
|
|
|
2009-09-27 16:04:10 +02:00
|
|
|
bool MathLib::isOctalDigit(char c)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
|
2009-09-27 16:04:10 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-02 22:32:58 +02:00
|
|
|
|