mathlib: Fixed todo 3/2=1
This commit is contained in:
parent
23f00e64cb
commit
7cddc52d8f
@ -30,64 +30,104 @@
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
|
||||
double MathLib::toNumber(const std::string &str)
|
||||
long MathLib::toLongNumber(const std::string &str)
|
||||
{
|
||||
if (strncmp(str.c_str(), "0x", 2) == 0)
|
||||
{
|
||||
return strtoul(str.c_str(), '\0', 16);
|
||||
}
|
||||
if (strncmp(str.c_str(), "0", 1) == 0)
|
||||
{
|
||||
return strtoul(str.c_str(), '\0', 8);
|
||||
}
|
||||
return atol(str.c_str());
|
||||
}
|
||||
|
||||
double MathLib::toDoubleNumber(const std::string &str)
|
||||
{
|
||||
return atof(str.c_str());
|
||||
}
|
||||
|
||||
std::string MathLib::toString(double d)
|
||||
template <typename T>
|
||||
std::string MathLib::toString(T d)
|
||||
{
|
||||
std::ostringstream result;
|
||||
result << d;
|
||||
return result.str();
|
||||
}
|
||||
|
||||
bool MathLib::isInt(const std::string & str)
|
||||
{
|
||||
if (str.find(".", 0) != std::string::npos || str.find("e", 0) != std::string::npos
|
||||
|| str.find("E", 0) != std::string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string MathLib::add(const std::string & first, const std::string & second)
|
||||
{
|
||||
return toString(toNumber(first) + toNumber(second));
|
||||
if (MathLib::isInt(first) && MathLib::isInt(second))
|
||||
{
|
||||
return toString<long>(toLongNumber(first) + toLongNumber(second));
|
||||
}
|
||||
return toString<double>(toDoubleNumber(first) + toDoubleNumber(second));
|
||||
}
|
||||
|
||||
std::string MathLib::subtract(const std::string &first, const std::string &second)
|
||||
{
|
||||
return toString(toNumber(first) - toNumber(second));
|
||||
if (MathLib::isInt(first) && MathLib::isInt(second))
|
||||
{
|
||||
return toString<long>(toLongNumber(first) - toLongNumber(second));
|
||||
}
|
||||
return toString<double>(toDoubleNumber(first) - toDoubleNumber(second));
|
||||
}
|
||||
|
||||
std::string MathLib::divide(const std::string &first, const std::string &second)
|
||||
{
|
||||
return toString(toNumber(first) / toNumber(second));
|
||||
if (MathLib::isInt(first) && MathLib::isInt(second))
|
||||
{
|
||||
return toString<long>(toLongNumber(first) / toLongNumber(second));
|
||||
}
|
||||
return toString<double>(toDoubleNumber(first) / toDoubleNumber(second));
|
||||
}
|
||||
|
||||
std::string MathLib::multiply(const std::string &first, const std::string &second)
|
||||
{
|
||||
return toString(toNumber(first) * toNumber(second));
|
||||
if (MathLib::isInt(first) && MathLib::isInt(second))
|
||||
{
|
||||
return toString<long>(toLongNumber(first) * toLongNumber(second));
|
||||
}
|
||||
return toString<double>(toDoubleNumber(first) * toDoubleNumber(second));
|
||||
}
|
||||
|
||||
|
||||
std::string MathLib::sin(const std::string &tok)
|
||||
{
|
||||
return toString(::sin(toNumber(tok)));
|
||||
return toString<double>(::sin(toDoubleNumber(tok)));
|
||||
}
|
||||
|
||||
|
||||
std::string MathLib::cos(const std::string &tok)
|
||||
{
|
||||
return toString(::cos(toNumber(tok)));
|
||||
return toString<double>(::cos(toDoubleNumber(tok)));
|
||||
}
|
||||
|
||||
std::string MathLib::tan(const std::string &tok)
|
||||
{
|
||||
return toString(::tan(toNumber(tok)));
|
||||
return toString<double>(::tan(toDoubleNumber(tok)));
|
||||
}
|
||||
|
||||
|
||||
std::string MathLib::abs(const std::string &tok)
|
||||
{
|
||||
return toString(::abs(toNumber(tok)));
|
||||
return toString<double>(::abs(toDoubleNumber(tok)));
|
||||
}
|
||||
|
||||
bool MathLib::isGreater(const std::string &first, const std::string &second)
|
||||
{
|
||||
return toNumber(first) > toNumber(second);
|
||||
return toDoubleNumber(first) > toDoubleNumber(second);
|
||||
}
|
||||
|
||||
|
||||
|
@ -26,8 +26,13 @@
|
||||
class MathLib
|
||||
{
|
||||
private:
|
||||
static double toNumber(const std::string & str);
|
||||
static std::string toString(double d);
|
||||
static long toLongNumber(const std::string & str);
|
||||
static double toDoubleNumber(const std::string & str);
|
||||
|
||||
template<typename T>
|
||||
static std::string toString(T d);
|
||||
|
||||
static bool isInt(const std::string & str);
|
||||
public:
|
||||
static std::string add(const std::string & first, const std::string & second);
|
||||
static std::string subtract(const std::string & first, const std::string & second);
|
||||
|
@ -38,9 +38,11 @@ private:
|
||||
void calculate()
|
||||
{
|
||||
ASSERT_EQUALS(std::string("256"), MathLib::add("0xff", "1"));
|
||||
ASSERT_EQUALS(std::string("0.003"), MathLib::multiply("1e-3", "3"));
|
||||
ASSERT_EQUALS(std::string("-0.003"), MathLib::multiply("-1e-3", "3"));
|
||||
ASSERT_EQUALS(std::string("5"), MathLib::divide("25.5", "5.1"));
|
||||
TODO_ASSERT_EQUALS(std::string("1"), MathLib::divide("3", "2"));
|
||||
ASSERT_EQUALS(std::string("-11.96"), MathLib::multiply("-2.3", "5.2"));
|
||||
ASSERT_EQUALS(std::string("7"), MathLib::divide("21.", "3"));
|
||||
ASSERT_EQUALS(std::string("1"), MathLib::divide("3", "2"));
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user