mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 02:32:06 +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:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user