From bd5f44ae610514e159f3320b3353faec19247769 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 7 Nov 2021 00:35:07 +0100 Subject: made commands maybe more readable, added ^ Signed-off-by: Daniel --- cmds.c | 30 +++++++++++++++++++++++------- main.c | 4 ++++ util.c | 1 + 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/cmds.c b/cmds.c index 07707df..4884ec8 100644 --- a/cmds.c +++ b/cmds.c @@ -6,29 +6,43 @@ #include #include +#define LAST stack.val[stack.count-1] +#define PREV stack.val[stack.count-2] +#define REDUCE stack.val[--stack.count] = 0 + stack fceil(stack stack) { - stack.val[stack.count-1] = ceil(stack.val[stack.count-1]); + LAST = ceil(PREV); return stack; } stack ffloor(stack stack) { - stack.val[stack.count-1] = floor(stack.val[stack.count-1]); + LAST = floor(LAST); return stack; } stack flogB(stack stack) { - stack.val[stack.count-2] = log2(stack.val[stack.count-2])/log2(stack.val[stack.count-1]); - stack.val[--stack.count] = 0; + PREV = log2(PREV)/log2(LAST); + REDUCE; return stack; } stack fnegate(stack stack) { - stack.val[stack.count-1] *= -1; + LAST *= -1; return stack; } stack fpop(stack stack) { - stack.val[--stack.count] = 0; + REDUCE; + return stack; +} + +stack fsqrt(stack stack) { + LAST = sqrt(LAST); + return stack; +} + +stack finv(stack stack) { + LAST = 1/LAST; return stack; } @@ -36,8 +50,10 @@ command CMD_LIST[] = { {"p", &fpop, "pop last element", 1}, {"ceil", &fceil, "truncate to the next integer", 1}, {"floor", &ffloor, "truncate to the previous integer", 1}, - {"log", &flogB, "calculate logarithm in base LAST_NUM of LAST_NUM-1", 2}, + {"log", &flogB, "calculate logarithm using last element as a base and the previous as the argument", 2}, {"neg", &fnegate, "change last element's sign", 1}, + {"sqrt", &fsqrt, "calculate the square root", 1}, + {"inv", &finv, "invert last number", 1}, }; int compare(const void *s1, const void *s2) { diff --git a/main.c b/main.c index 4f9dd2e..b4c092f 100644 --- a/main.c +++ b/main.c @@ -68,6 +68,10 @@ int main() { s.stk.val[s.stk.count-2] = s.stk.val[s.stk.count-2]/s.stk.val[s.stk.count-1]; s.stk.val[s.stk.count--] = 0; /* lower count by 1 */ s.last_op = '/'; + } else if (operator == '^') { + s.stk.val[s.stk.count-2] = pow(s.stk.val[s.stk.count-2],s.stk.val[s.stk.count-1]); + s.stk.val[s.stk.count--] = 0; /* lower count by 1 */ + s.last_op = '^'; } else; } else if (t == FUNCTION) { /* check for special commands, else pass to exec() */ diff --git a/util.c b/util.c index a6c831f..7d13e3b 100644 --- a/util.c +++ b/util.c @@ -7,6 +7,7 @@ const char OPERATOR_LIST[] = { '-', '*', '/', + '^', 0 }; -- cgit 1.4.1-2-gfad0