2009-04-09 16:17:59 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam,
|
|
|
|
* Leandro Penz, Kimmo Varis, Vesa Pikki, Nguyen Duong Tuan
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-04-06 19:08:13 +02:00
|
|
|
#include "mathlib.h"
|
2009-04-06 18:27:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cmath>
|
|
|
|
|
2009-04-06 19:08:13 +02:00
|
|
|
double MathLib::toNumber(const std::string &str)
|
2009-04-06 18:27:14 +02:00
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return atof(str.c_str());
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
2009-04-06 19:08:13 +02:00
|
|
|
std::string MathLib::toString(double d)
|
2009-04-06 18:27:14 +02:00
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
std::ostringstream result;
|
|
|
|
result << d;
|
|
|
|
return result.str();
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::add(const std::string & first, const std::string & second)
|
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return toString(toNumber(first) + toNumber(second));
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
2009-04-06 19:08:13 +02:00
|
|
|
std::string MathLib::subtract(const std::string &first, const std::string &second)
|
2009-04-06 18:27:14 +02:00
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return toString(toNumber(first) - toNumber(second));
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::divide(const std::string &first, const std::string &second)
|
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return toString(toNumber(first) / toNumber(second));
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::multiply(const std::string &first, const std::string &second)
|
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return toString(toNumber(first) * toNumber(second));
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string MathLib::sin(const std::string &tok)
|
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return toString(::sin(toNumber(tok)));
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string MathLib::cos(const std::string &tok)
|
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return toString(::cos(toNumber(tok)));
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::tan(const std::string &tok)
|
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return toString(::tan(toNumber(tok)));
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string MathLib::abs(const std::string &tok)
|
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return toString(::abs(toNumber(tok)));
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
2009-04-06 19:08:13 +02:00
|
|
|
bool MathLib::isGreater(const std::string &first, const std::string &second)
|
2009-04-06 18:27:14 +02:00
|
|
|
{
|
2009-04-06 19:08:13 +02:00
|
|
|
return toNumber(first) > toNumber(second);
|
2009-04-06 18:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|