changeset 6:8676cf44c307

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
author Jonathan Pevarnek <pevarnj@gmail.com>
date Sun, 06 Mar 2011 01:38:01 -0500
parents 348c59c36703
children 089efc170bb0
files Makefile include/math.h include/operations.h src/math.c src/operations.c
diffstat 5 files changed, 110 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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
--- /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
--- 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 <std.h>
 #include <stack.h>
 
-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
--- /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;
+}
--- 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 <operations.h>
 #include <std.h>
 #include <stack.h>
+#include <math.h>
 
-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);
+}