[muparser] Remove MathImpl

This layer of indirection is silly.
This commit is contained in:
ridiculousfish 2017-12-18 11:11:04 -08:00
parent f0de6e0852
commit 1f456c71f7
3 changed files with 25 additions and 64 deletions

View file

@ -3,44 +3,4 @@
#include <cmath>
namespace mu {
//-----------------------------------------------------------------------------------------------
//
// Standard math functions with dummy overload for integer types
//
//-----------------------------------------------------------------------------------------------
/** \brief A template class for providing wrappers for essential math functions.
This template is spezialized for several types in order to provide a unified interface
for parser internal math function calls regardless of the data type.
*/
template <typename T>
struct MathImpl {
static T Sin(T v) { return sin(v); }
static T Cos(T v) { return cos(v); }
static T Tan(T v) { return tan(v); }
static T ASin(T v) { return asin(v); }
static T ACos(T v) { return acos(v); }
static T ATan(T v) { return atan(v); }
static T ATan2(T v1, T v2) { return atan2(v1, v2); }
static T Sinh(T v) { return sinh(v); }
static T Cosh(T v) { return cosh(v); }
static T Tanh(T v) { return tanh(v); }
static T ASinh(T v) { return log(v + sqrt(v * v + 1)); }
static T ACosh(T v) { return log(v + sqrt(v * v - 1)); }
static T ATanh(T v) { return ((T)0.5 * log((1 + v) / (1 - v))); }
static T Log(T v) { return log(v); }
static T Log2(T v) { return log(v) / log((T)2); } // Logarithm base 2
static T Log10(T v) { return log10(v); } // Logarithm base 10
static T Exp(T v) { return exp(v); }
static T Abs(T v) { return (v >= 0) ? v : -v; }
static T Sqrt(T v) { return sqrt(v); }
static T Rint(T v) { return floor(v + (T)0.5); }
static T Sign(T v) { return (T)((v < 0) ? -1 : (v > 0) ? 1 : 0); }
static T Pow(T v1, T v2) { return std::pow(v1, v2); }
};
}
#endif

View file

@ -49,21 +49,19 @@ namespace mu {
//---------------------------------------------------------------------------
// Trigonometric function
ValueOrError Parser::Sin(value_type v) { return MathImpl<value_type>::Sin(v); }
ValueOrError Parser::Cos(value_type v) { return MathImpl<value_type>::Cos(v); }
ValueOrError Parser::Tan(value_type v) { return MathImpl<value_type>::Tan(v); }
ValueOrError Parser::ASin(value_type v) { return MathImpl<value_type>::ASin(v); }
ValueOrError Parser::ACos(value_type v) { return MathImpl<value_type>::ACos(v); }
ValueOrError Parser::ATan(value_type v) { return MathImpl<value_type>::ATan(v); }
ValueOrError Parser::ATan2(value_type v1, value_type v2) {
return MathImpl<value_type>::ATan2(v1, v2);
}
ValueOrError Parser::Sinh(value_type v) { return MathImpl<value_type>::Sinh(v); }
ValueOrError Parser::Cosh(value_type v) { return MathImpl<value_type>::Cosh(v); }
ValueOrError Parser::Tanh(value_type v) { return MathImpl<value_type>::Tanh(v); }
ValueOrError Parser::ASinh(value_type v) { return MathImpl<value_type>::ASinh(v); }
ValueOrError Parser::ACosh(value_type v) { return MathImpl<value_type>::ACosh(v); }
ValueOrError Parser::ATanh(value_type v) { return MathImpl<value_type>::ATanh(v); }
ValueOrError Parser::Sin(value_type v) { return std::sin(v); }
ValueOrError Parser::Cos(value_type v) { return std::cos(v); }
ValueOrError Parser::Tan(value_type v) { return std::tan(v); }
ValueOrError Parser::ASin(value_type v) { return std::asin(v); }
ValueOrError Parser::ACos(value_type v) { return std::acos(v); }
ValueOrError Parser::ATan(value_type v) { return std::atan(v); }
ValueOrError Parser::ATan2(value_type v1, value_type v2) { return std::atan2(v1, v2); }
ValueOrError Parser::Sinh(value_type v) { return std::sinh(v); }
ValueOrError Parser::Cosh(value_type v) { return std::cosh(v); }
ValueOrError Parser::Tanh(value_type v) { return std::tanh(v); }
ValueOrError Parser::ASinh(value_type v) { return std::asinh(v); }
ValueOrError Parser::ACosh(value_type v) { return std::acosh(v); }
ValueOrError Parser::ATanh(value_type v) { return std::atanh(v); }
//---------------------------------------------------------------------------
// Logarithm functions
@ -74,7 +72,7 @@ ValueOrError Parser::Log2(value_type v) {
if (v <= 0) return ParserError(ecDOMAIN_ERROR, _T("Log2"));
#endif
return MathImpl<value_type>::Log2(v);
return std::log2(v);
}
// Logarithm base 10
@ -83,7 +81,7 @@ ValueOrError Parser::Log10(value_type v) {
if (v <= 0) return ParserError(ecDOMAIN_ERROR, _T("Log10"));
#endif
return MathImpl<value_type>::Log10(v);
return std::log10(v);
}
// Logarithm base e (natural logarithm)
@ -92,22 +90,25 @@ ValueOrError Parser::Ln(value_type v) {
if (v <= 0) return ParserError(ecDOMAIN_ERROR, _T("Ln"));
#endif
return MathImpl<value_type>::Log(v);
return std::log(v);
}
//---------------------------------------------------------------------------
// misc
ValueOrError Parser::Exp(value_type v) { return MathImpl<value_type>::Exp(v); }
ValueOrError Parser::Abs(value_type v) { return MathImpl<value_type>::Abs(v); }
ValueOrError Parser::Exp(value_type v) { return std::exp(v); }
ValueOrError Parser::Abs(value_type v) { return std::abs(v); }
ValueOrError Parser::Sqrt(value_type v) {
#ifdef MUP_MATH_EXCEPTIONS
if (v < 0) return ParserError(ecDOMAIN_ERROR, _T("sqrt"));
#endif
return MathImpl<value_type>::Sqrt(v);
return std::sqrt(v);
}
ValueOrError Parser::Rint(value_type v) { return std::floor(v + 0.5); }
ValueOrError Parser::Sign(value_type v) {
// return 1, 0, -1 according to whether v is positive, zero, negative.
return (v > 0) - (v < 0);
}
ValueOrError Parser::Rint(value_type v) { return MathImpl<value_type>::Rint(v); }
ValueOrError Parser::Sign(value_type v) { return MathImpl<value_type>::Sign(v); }
//---------------------------------------------------------------------------
/** \brief Callback for the unary minus operator.

View file

@ -788,7 +788,7 @@ ValueOrError ParserBase::ExecuteRPN() const {
case cmPOW:
--sidx;
Stack[sidx] = MathImpl<value_type>::Pow(Stack[sidx], Stack[1 + sidx]);
Stack[sidx] = std::pow(Stack[sidx], Stack[1 + sidx]);
continue;
case cmLAND: