Skip to content

Commit

Permalink
[Rules] Add missing functions (sin/cos etc)
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Jan 9, 2021
1 parent 098eb51 commit 1eb73d8
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 17 deletions.
123 changes: 110 additions & 13 deletions src/src/Helpers/Rules_calculate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ bool RulesCalculate_t::is_unary_operator(char c)
case UnaryOperator::ArcSin:
case UnaryOperator::ArcCos:
case UnaryOperator::ArcTan:
case UnaryOperator::Sin_d:
case UnaryOperator::Cos_d:
case UnaryOperator::Tan_d:
case UnaryOperator::ArcSin_d:
case UnaryOperator::ArcCos_d:
case UnaryOperator::ArcTan_d:
return true;
}
return false;
Expand Down Expand Up @@ -105,32 +111,83 @@ double RulesCalculate_t::apply_operator(char op, double first, double second)

double RulesCalculate_t::apply_unary_operator(char op, double first)
{
double ret = 0.0;
const UnaryOperator un_op = static_cast<UnaryOperator>(op);

switch (un_op) {
case UnaryOperator::Not:
return (approximatelyEqual(round(first), 0)) ? 1 : 0;
case UnaryOperator::Log:
return log10(first);
case UnaryOperator::Ln:
// FIXME TD-er: Must implement log and ln
break;
// FIXME TD-er: Must implement rad/deg conversions.
return log(first);
case UnaryOperator::Sqrt:
return sqrt(first);
default:
break;
}

#ifdef USE_TRIGONOMETRIC_FUNCTIONS_RULES
const bool useDegree = angleDegree(un_op);

// First the trigonometric functions with angle as output
switch (un_op) {
case UnaryOperator::ArcSin:
case UnaryOperator::ArcSin_d:
ret = asin(first);
return useDegree ? degrees(ret) : ret;
case UnaryOperator::ArcCos:
case UnaryOperator::ArcCos_d:
ret = acos(first);
return useDegree ? degrees(ret) : ret;
case UnaryOperator::ArcTan:
case UnaryOperator::ArcTan_d:
ret = atan(first);
return useDegree ? degrees(ret) : ret;
default:
break;
}

// Now the trigonometric functions with angle as input
if (useDegree) {
first = radians(first);
}

switch (un_op) {
case UnaryOperator::Sin:
case UnaryOperator::Sin_d:
return sin(first);
case UnaryOperator::Cos:
case UnaryOperator::Cos_d:
return cos(first);
case UnaryOperator::Tan:
case UnaryOperator::Tan_d:
return tan(first);
default:
break;
}
#else // ifdef USE_TRIGONOMETRIC_FUNCTIONS_RULES

switch (op) {
case UnaryOperator::Sin:
case UnaryOperator::Sin_d:
case UnaryOperator::Cos:
case UnaryOperator::Cos_d:
case UnaryOperator::Tan:
case UnaryOperator::Tan_d:
case UnaryOperator::ArcSin:
return asin(first);
case UnaryOperator::ArcSin_d:
case UnaryOperator::ArcCos:
return acos(first);
case UnaryOperator::ArcCos_d:
case UnaryOperator::ArcTan:
return atan(first);
case UnaryOperator::ArcTan_d:
addLog(LOG_LEVEL_ERROR, F("USE_TRIGONOMETRIC_FUNCTIONS_RULES not defined in build"));
break;
default:
break;
}
return 0;
#endif // ifdef USE_TRIGONOMETRIC_FUNCTIONS_RULES
return ret;
}

/*
Expand Down Expand Up @@ -418,6 +475,22 @@ void preProcessReplace(String& input, UnaryOperator op) {
input.replace(find, replace);
}

bool angleDegree(UnaryOperator op)
{
switch (op) {
case UnaryOperator::Sin_d:
case UnaryOperator::Cos_d:
case UnaryOperator::Tan_d:
case UnaryOperator::ArcSin_d:
case UnaryOperator::ArcCos_d:
case UnaryOperator::ArcTan_d:
return true;
default:
break;
}
return false;
}

String toString(UnaryOperator op)
{
String find;
Expand All @@ -435,24 +508,34 @@ String toString(UnaryOperator op)
find = F("sqrt");
break;
case UnaryOperator::Sin:
case UnaryOperator::Sin_d:
find = F("sin");
break;
case UnaryOperator::Cos:
case UnaryOperator::Cos_d:
find = F("cos");
break;
case UnaryOperator::Tan:
case UnaryOperator::Tan_d:
find = F("tan");
break;
case UnaryOperator::ArcSin:
case UnaryOperator::ArcSin_d:
find = F("asin");
break;
case UnaryOperator::ArcCos:
case UnaryOperator::ArcCos_d:
find = F("acos");
break;
case UnaryOperator::ArcTan:
case UnaryOperator::ArcTan_d:
find = F("atan");
break;
}

if (angleDegree(op)) {
find += F("_d");
}
return find;
}

Expand All @@ -464,12 +547,26 @@ String RulesCalculate_t::preProces(const String& input)
preProcessReplace(preprocessed, UnaryOperator::Log);
preProcessReplace(preprocessed, UnaryOperator::Ln);
preProcessReplace(preprocessed, UnaryOperator::Sqrt);
preProcessReplace(preprocessed, UnaryOperator::Sin);
preProcessReplace(preprocessed, UnaryOperator::Cos);
preProcessReplace(preprocessed, UnaryOperator::Tan);
preProcessReplace(preprocessed, UnaryOperator::ArcSin);
preProcessReplace(preprocessed, UnaryOperator::ArcCos);
preProcessReplace(preprocessed, UnaryOperator::ArcTan);
#ifdef USE_TRIGONOMETRIC_FUNCTIONS_RULES
if (preprocessed.indexOf(F("sin")) != -1) {
preProcessReplace(preprocessed, UnaryOperator::Sin);
preProcessReplace(preprocessed, UnaryOperator::ArcSin);
preProcessReplace(preprocessed, UnaryOperator::Sin_d);
preProcessReplace(preprocessed, UnaryOperator::ArcSin_d);
}
if (preprocessed.indexOf(F("cos")) != -1) {
preProcessReplace(preprocessed, UnaryOperator::Cos);
preProcessReplace(preprocessed, UnaryOperator::ArcCos);
preProcessReplace(preprocessed, UnaryOperator::Cos_d);
preProcessReplace(preprocessed, UnaryOperator::ArcCos_d);
}
if (preprocessed.indexOf(F("tan")) != -1) {
preProcessReplace(preprocessed, UnaryOperator::Tan);
preProcessReplace(preprocessed, UnaryOperator::ArcTan);
preProcessReplace(preprocessed, UnaryOperator::Tan_d);
preProcessReplace(preprocessed, UnaryOperator::ArcTan_d);
}
#endif
return preprocessed;
}

Expand Down
15 changes: 11 additions & 4 deletions src/src/Helpers/Rules_calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,28 @@ enum class UnaryOperator {
Ln,
Sqrt,
Sin,
Sin_d,
Cos,
Cos_d,
Tan,
Tan_d,
ArcSin,
ArcSin_d,
ArcCos,
ArcTan
ArcCos_d,
ArcTan,
ArcTan_d
};

void preProcessReplace(String& input, UnaryOperator op);

void preProcessReplace(String & input,
UnaryOperator op);
bool angleDegree(UnaryOperator op);
String toString(UnaryOperator op);

class RulesCalculate_t {
private:

double globalstack[STACK_SIZE];
double globalstack[STACK_SIZE];
double *sp = globalstack - 1;
double *sp_max = &globalstack[STACK_SIZE - 1];

Expand Down

0 comments on commit 1eb73d8

Please sign in to comment.