Update mathematical functions (#675)

Document the existing `ROUND`, `CEIL`, and `FLOOR` functions
Also update the trig function docs for searchability

Implement `POW` and `LOG`
Addresses part of #675

Implement ** for integer exponents
** has higher precedence than -, like Python, so -3**4 == -(3**4) == 81
This commit is contained in:
Rangi
2021-01-01 19:39:20 -05:00
committed by GitHub
parent 7bb6f71f0b
commit 895ec5564d
11 changed files with 148 additions and 12 deletions

View File

@@ -292,6 +292,20 @@ static int32_t shift(int32_t shiftee, int32_t amount)
}
}
static int32_t exponent(int32_t base, int32_t power)
{
int32_t result = 1;
while (power) {
if (power % 2)
result *= base;
power /= 2;
base *= base;
}
return result;
}
struct Symbol const *rpn_SymbolOf(struct Expression const *expr)
{
if (!rpn_isSymbol(expr))
@@ -415,6 +429,15 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
else
expr->nVal = src1->nVal % src2->nVal;
break;
case RPN_EXP:
if (src2->nVal < 0)
fatalerror("Exponentiation by negative power\n");
if (src1->nVal == INT32_MIN && src2->nVal == -1)
expr->nVal = 0;
else
expr->nVal = exponent(src1->nVal, src2->nVal);
break;
case RPN_UNSUB:
case RPN_UNNOT: