# HG changeset patch # User Jonathan Pevarnek # Date 1299393481 18000 # Node ID 8676cf44c307c2a576026803d4027a0c21a4b114 # Parent 348c59c3670373fc01d2f3650e8f0d5d5c53158a Added functions pow, dup, and drop pow pulls two numbers from the stack and raises the second pulled to the first pulled. This was implemented using a exp function I wrote previously and a log function I just wrote. If an integer power is requestion, it uses the standard O(log(n)) algorithm... This function can be called using '**' drop drops the top value of the stack and dup duplicates it... I also modified the make file so that it supports header file dependencies diff -r 348c59c36703 -r 8676cf44c307 Makefile --- a/Makefile Wed Mar 02 15:43:05 2011 -0500 +++ b/Makefile Sun Mar 06 01:38:01 2011 -0500 @@ -8,7 +8,7 @@ CFLAGS=-g -fno-strict-aliasing -fno-builtin -nostdinc -nostdlib -Wall -m64 -I include/ -O2 LDFLAGS=-m elf64_s390 -OBJS=src/init.o src/io.o src/std.o src/stack.o src/operations.o +OBJS=src/init.o src/io.o src/std.o src/stack.o src/operations.o src/math.o .PHONY: all build clean tags @@ -64,3 +64,14 @@ ipl/%.o: ipl/%.S $(AS) -m64 -o $@ $< + +depend: + makedepend -I include src/*.c + +# DO NOT DELETE + +src/init.o: include/std.h include/operations.h include/stack.h +src/operations.o: include/operations.h include/std.h include/stack.h +src/operations.o: include/math.h +src/stack.o: include/stack.h include/std.h +src/std.o: include/std.h diff -r 348c59c36703 -r 8676cf44c307 include/math.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/math.h Sun Mar 06 01:38:01 2011 -0500 @@ -0,0 +1,10 @@ +#ifndef __MATH_H +#define __MATH_H + +double math_iPow(double base, unsigned int exp); +double math_exp(double exp); +double math_log(double num); +double math_pow(double base, double exp); +double math_abs(double num); + +#endif //__MATH_H diff -r 348c59c36703 -r 8676cf44c307 include/operations.h --- a/include/operations.h Wed Mar 02 15:43:05 2011 -0500 +++ b/include/operations.h Sun Mar 06 01:38:01 2011 -0500 @@ -4,7 +4,7 @@ #include #include -enum Operation {PRINT, ADD, SUB, MULT, DIV, DUP, MAXOP}; +enum Operation {PRINT, ADD, SUB, MULT, DIV, POW, DUP, DROP, MAXOP}; extern const char operationNames[MAXOP][10]; extern void (*operation[MAXOP])(struct Stack*); @@ -13,6 +13,8 @@ void op_sub(struct Stack *stack); void op_mult(struct Stack *stack); void op_div(struct Stack *stack); +void op_pow(struct Stack *stack); void op_dup(struct Stack *stack); +void op_drop(struct Stack *stack); #endif //__OPERATIONS_H diff -r 348c59c36703 -r 8676cf44c307 src/math.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/math.c Sun Mar 06 01:38:01 2011 -0500 @@ -0,0 +1,70 @@ +double math_iPow(double base, int exp) +{ + double ans = 1; + short isNeg = 0; + if(exp < 0) { + isNeg = 1; + exp *= -1; + } + while(exp > 0) { + if(exp%2) { + exp -= 1; + ans *= base; + } else { + exp /= 2; + base *= base; + } + } + return isNeg ? 1/ans : ans; +} + +double math_exp(double exp) +{ + unsigned int i; //a counter for the loop + double ans = 1; + double prev = -1; + double term = 1; + short isNeg = 0; + if(exp < 0) { + isNeg = 1; + exp *= -1; + } + for(i = 1; ans - prev; i++) { + prev = ans; + term *= exp/i; + ans += term; + } + return isNeg ? 1/ans : ans; +} + +double math_log(double num) +{ + unsigned int denom = 1; + double euler = 2.71828182845904; + double ans = 0; + double prev = -1; + double term = -1; + while(num <= -1 || num >= 1) { + num /= euler; + ans += 1; + } + for(; ans - prev; denom++) { + prev = ans; + term *= (-1*(num - 1)); + ans += term/denom; + } + return ans; +} + +double math_pow(double base, double exp) +{ + if(exp == (int) exp) + return math_iPow(base, (int) exp); + return math_exp(exp*math_log(base)); +} + +double math_abs(double num) +{ + if(num < 0) return num*-1; + return num; +} diff -r 348c59c36703 -r 8676cf44c307 src/operations.c --- a/src/operations.c Wed Mar 02 15:43:05 2011 -0500 +++ b/src/operations.c Sun Mar 06 01:38:01 2011 -0500 @@ -1,10 +1,11 @@ #include #include #include +#include -const char operationNames[MAXOP][10] = {".", "+", "-", "*", "/", "dup"}; +const char operationNames[MAXOP][10] = {".", "+", "-", "*", "/", "**", "dup", "drop"}; void (*operation[MAXOP])(struct Stack*) = {op_print, op_add, op_sub, op_mult, - op_div, op_dup}; + op_div, op_pow, op_dup, op_drop}; void op_print(struct Stack *stack) { @@ -42,9 +43,21 @@ push(stack, sec/first); } +void op_pow(struct Stack *stack) +{ + eltType first = pop(stack); + eltType sec = pop(stack); + push(stack, math_pow(sec, first)); +} + void op_dup(struct Stack *stack) { eltType val = pop(stack); push(stack, val); push(stack, val); } + +void op_drop(struct Stack *stack) +{ + pop(stack); +}