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

@@ -21,6 +21,20 @@
#include "extern/err.h"
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;
}
static int32_t asl(int32_t value, int32_t shiftamt); // Forward decl for below
static int32_t asr(int32_t value, int32_t shiftamt)
{
@@ -237,6 +251,18 @@ static int32_t computeRPNExpr(struct Patch const *patch,
case RPN_UNSUB:
value = -popRPN();
break;
case RPN_EXP:
value = popRPN();
if (value < 0) {
if (!isError)
error(patch->src, patch->lineNo, "Exponent by negative");
isError = true;
popRPN();
value = 0;
} else {
value = exponent(popRPN(), value);
}
break;
case RPN_OR:
value = popRPN() | popRPN();