2009-05-30 07:48:12 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2014-02-15 07:45:39 +01:00
|
|
|
* Copyright (C) 2007-2014 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>
|
2013-05-09 18:50:24 +02:00
|
|
|
#include <limits>
|
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:
|
2012-02-17 19:54:53 +01:00
|
|
|
if (isHex(str)) {
|
2012-11-27 17:37:49 +01:00
|
|
|
if (str[0] == '-') {
|
|
|
|
bigint ret = 0;
|
|
|
|
std::istringstream istr(str);
|
|
|
|
istr >> std::hex >> ret;
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
unsigned long long ret = 0;
|
|
|
|
std::istringstream istr(str);
|
|
|
|
istr >> std::hex >> ret;
|
|
|
|
return (bigint)ret;
|
|
|
|
}
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2010-11-20 10:05:33 +01:00
|
|
|
|
|
|
|
// octal numbers:
|
2012-02-17 19:54:53 +01:00
|
|
|
if (isOct(str)) {
|
2010-11-20 10:05:33 +01:00
|
|
|
bigint ret = 0;
|
2012-02-17 15:47:08 +01:00
|
|
|
std::istringstream istr(str);
|
2010-11-20 10:05:33 +01:00
|
|
|
istr >> std::oct >> ret;
|
2012-02-17 15:47:08 +01:00
|
|
|
return ret;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2010-11-20 10:05:33 +01:00
|
|
|
|
2012-09-10 21:13:32 +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++) {
|
|
|
|
ret <<= 1;
|
|
|
|
if (str[i] == '1')
|
|
|
|
ret |= 1;
|
|
|
|
}
|
|
|
|
if (str[0] == '-')
|
|
|
|
ret = -ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-20 16:56:49 +01:00
|
|
|
if (isFloat(str))
|
2010-11-20 10:05:33 +01:00
|
|
|
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)
|
|
|
|
{
|
2012-02-17 19:54:53 +01:00
|
|
|
if (isHex(str))
|
|
|
|
return static_cast<double>(toLongNumber(str));
|
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
|
|
|
}
|
|
|
|
|
2013-09-21 18:48:48 +02:00
|
|
|
template<> std::string MathLib::toString(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";
|
|
|
|
if (result.str().find(".") == std::string::npos)
|
|
|
|
return result.str() + ".0";
|
|
|
|
return result.str();
|
|
|
|
}
|
|
|
|
|
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;
|
2014-02-20 16:56:49 +01:00
|
|
|
// scientific notation (without dot!)
|
|
|
|
std::string::const_iterator it=s.begin();
|
|
|
|
if (*it=='+' || *it=='-') { // mantissa sign char
|
|
|
|
++it;
|
|
|
|
if (it == s.end())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!std::isdigit(*it))
|
|
|
|
return false;
|
|
|
|
++it;
|
|
|
|
while (std::isdigit(*it)) { // number
|
|
|
|
++it;
|
|
|
|
if (it == s.end())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (*it!='e' && *it!='E')
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
if (it == s.end())
|
|
|
|
return false; // incomplete exponent number
|
|
|
|
if (*it=='+' || *it=='-') { // exponent sign char
|
|
|
|
++it;
|
|
|
|
if (it == s.end())
|
|
|
|
return false; // incomplete exponent number
|
|
|
|
}
|
|
|
|
|
|
|
|
while (std::isdigit(*it)) { // number
|
|
|
|
++it;
|
|
|
|
if (it == s.end())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (it==s.end())
|
|
|
|
return true;
|
|
|
|
if ((*it=='f' || *it=='F') && (it+1)==s.end()) // trailing 'f'/'F' to indicate a float literal (as opposed to a double literal)
|
|
|
|
return true;
|
|
|
|
return false;
|
2010-04-02 02:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isNegative(const std::string &s)
|
|
|
|
{
|
|
|
|
// remember position
|
2012-02-17 19:54:53 +01:00
|
|
|
std::string::size_type n = 0;
|
2010-04-02 02:19:38 +02:00
|
|
|
// 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
|
2013-08-07 16:27:37 +02:00
|
|
|
return (s[n] == '-');
|
2012-02-17 19:54:53 +01:00
|
|
|
}
|
2010-04-02 07:32:03 +02:00
|
|
|
|
2013-09-29 15:11:09 +02:00
|
|
|
bool MathLib::isPositive(const std::string &s)
|
|
|
|
{
|
|
|
|
return !MathLib::isNegative(s);
|
|
|
|
}
|
|
|
|
|
2012-02-17 19:54:53 +01:00
|
|
|
bool MathLib::isOct(const std::string& str)
|
|
|
|
{
|
2013-09-29 15:49:02 +02:00
|
|
|
const bool sign = str[0]=='-' || str[0]=='+';
|
2013-08-07 16:27:37 +02:00
|
|
|
return (str[sign?1:0] == '0' && (str.size() == 1 || isOctalDigit(str[sign?2:1])) && !isFloat(str));
|
2012-02-17 19:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isHex(const std::string& str)
|
|
|
|
{
|
2013-09-29 15:49:02 +02:00
|
|
|
const bool sign = str[0]=='-' || str[0]=='+';
|
2013-08-07 16:27:37 +02:00
|
|
|
return (str.compare(sign?1:0, 2, "0x") == 0 || str.compare(sign?1:0, 2, "0X") == 0);
|
2010-04-02 02:19:38 +02:00
|
|
|
}
|
|
|
|
|
2012-09-10 21:13:32 +02:00
|
|
|
bool MathLib::isBin(const std::string& str)
|
|
|
|
{
|
2013-09-29 15:49:02 +02:00
|
|
|
const bool sign = str[0]=='-' || str[0]=='+';
|
2013-08-07 16:27:37 +02:00
|
|
|
return ((str.compare(sign?1:0, 2, "0b") == 0 || str.compare(sign?1:0, 2, "0B") == 0) && str.find_first_not_of("10bB", 1) == std::string::npos);
|
2012-09-10 21:13:32 +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
|
2014-02-20 16:56:49 +01:00
|
|
|
const std::string charsToIndicateAFloat=".eE";
|
|
|
|
if (s.find_last_of(charsToIndicateAFloat) != 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 {
|
2012-02-17 19:54:53 +01:00
|
|
|
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
|
2014-02-20 16:56:49 +01:00
|
|
|
if (isHex(s)) {
|
2009-08-16 19:43:45 +02:00
|
|
|
Mode = eHex;
|
2012-02-17 19:54:53 +01:00
|
|
|
} else if (isOct(s)) {
|
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
|
|
|
|
2014-02-20 16:56:49 +01:00
|
|
|
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;
|
2012-02-17 19:54:53 +01:00
|
|
|
|
|
|
|
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
|
2009-08-16 18:45:26 +02:00
|
|
|
}
|
|
|
|
// check octal notation
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Mode == eOctal) {
|
2012-02-17 19:54:53 +01:00
|
|
|
++n; // 0
|
2010-04-02 07:30:58 +02:00
|
|
|
while (isOctalDigit(s[n]))
|
2009-08-16 18:45:26 +02:00
|
|
|
++n;
|
2012-02-17 19:54:53 +01:00
|
|
|
|
|
|
|
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
|
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;
|
|
|
|
};
|
2010-05-14 18:40:37 +02:00
|
|
|
|
2012-02-17 19:54:53 +01:00
|
|
|
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
|
|
|
|
|
|
|
|
if (!bStartsWithDigit)
|
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
|
2013-11-09 16:07:28 +01:00
|
|
|
// is here --> return true, but if something was found e.g. 12E+12AA return false
|
2013-08-07 16:27:37 +02:00
|
|
|
return (n >= s.length());
|
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)) {
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toLongNumber(first) + toLongNumber(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);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toLongNumber(first) - toLongNumber(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)
|
|
|
|
return "0.0" ;
|
|
|
|
|
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);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2013-09-29 15:49:02 +02:00
|
|
|
const bigint a = toLongNumber(first);
|
|
|
|
const bigint b = toLongNumber(second);
|
2013-05-09 18:50:24 +02:00
|
|
|
if (a == std::numeric_limits<bigint>::min())
|
|
|
|
throw InternalError(0, "Internal Error: Division overflow");
|
2012-09-06 20:15:32 +02:00
|
|
|
if (b == 0)
|
|
|
|
throw InternalError(0, "Internal Error: Division by zero");
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toLongNumber(first) / b);
|
2013-07-19 09:27:33 +02:00
|
|
|
} else if (second == "0.0") {
|
2013-11-04 11:56:43 +01:00
|
|
|
if (first=="0.0" || first=="+0.0")
|
|
|
|
return "nan.0";
|
|
|
|
if (first=="-0.0")
|
|
|
|
return "-nan.0";
|
2013-11-02 22:58:23 +01:00
|
|
|
return (first[0] == '-') ? "-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));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toLongNumber(first) * toLongNumber(second));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toDoubleNumber(first) * toDoubleNumber(second));
|
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)
|
|
|
|
{
|
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2012-09-06 20:15:32 +02:00
|
|
|
bigint b = toLongNumber(second);
|
|
|
|
if (b == 0)
|
|
|
|
throw InternalError(0, "Internal Error: Division by zero");
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toLongNumber(first) % b);
|
2012-04-12 14:02:09 +02:00
|
|
|
}
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::fmod(toDoubleNumber(first),toDoubleNumber(second)));
|
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 '&':
|
2013-09-21 18:48:48 +02:00
|
|
|
return MathLib::toString(MathLib::toLongNumber(first) & MathLib::toLongNumber(second));
|
2012-05-16 18:48:33 +02:00
|
|
|
|
|
|
|
case '|':
|
2013-09-21 18:48:48 +02:00
|
|
|
return MathLib::toString(MathLib::toLongNumber(first) | MathLib::toLongNumber(second));
|
2012-05-16 18:48:33 +02:00
|
|
|
|
|
|
|
case '^':
|
2013-09-21 18:48:48 +02:00
|
|
|
return MathLib::toString(MathLib::toLongNumber(first) ^ MathLib::toLongNumber(second));
|
2012-05-16 18:48:33 +02:00
|
|
|
|
2009-08-02 21:11:17 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(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)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2012-02-17 19:54:53 +01:00
|
|
|
return (str == "-0" || str == "0" || str == "+0"
|
|
|
|
|| str == "-0.0" || str == "0.0" || str == "+0.0"
|
|
|
|
|| str == "-0." || str == "+0."
|
|
|
|
|| str == "-0E-00" || str == "-0E+00" || str == "+0E+00" || str == "+0E-00"
|
|
|
|
|| str == "-0e-00" || str == "-0e+00" || str == "+0e+00" || str == "+0e-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)
|
|
|
|
{
|
2013-08-07 16:27:37 +02:00
|
|
|
return (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7');
|
2009-09-27 16:04:10 +02:00
|
|
|
}
|