mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 10:42:07 +00:00
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:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user