changeset 27:1a070e843bf6

Fixed bugs with printing out and reading in numbers If infinity or NAN is passed to ftoa, it will set the character buffer to read either "INF" or "NAN" Negative decimals are now correctly read in by atof. abs() is now a part of the standard library. The math_ prefix has been dropped from all functions but the trivial binary operations. The strcpy function has been added.
author Jonathan Pevarnek <pevarnj@gmail.com>
date Tue, 29 Mar 2011 22:05:03 -0400
parents c1ad124f2aaf
children cced4d365c5e
files Makefile include/math.h include/std.h include/system.h src/dynamic.c src/math.c src/operations.c src/std.c
diffstat 8 files changed, 90 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun Mar 20 14:17:24 2011 -0400
+++ b/Makefile	Tue Mar 29 22:05:03 2011 -0400
@@ -82,6 +82,7 @@
 
 # DO NOT DELETE
 
+src/date.o: include/tod.h include/std.h
 src/dynamic.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
--- a/include/math.h	Sun Mar 20 14:17:24 2011 -0400
+++ b/include/math.h	Tue Mar 29 22:05:03 2011 -0400
@@ -1,11 +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);
+double iPow(double base, int exponent);
+double exp(double exp);
+double log(double num);
+double pow(double base, double exponent);
 
 double math_add(double first, double sec);
 double math_sub(double first, double sec);
--- a/include/std.h	Sun Mar 20 14:17:24 2011 -0400
+++ b/include/std.h	Tue Mar 29 22:05:03 2011 -0400
@@ -1,16 +1,17 @@
 #ifndef __STD_H
 #define __STD_H
 
-typedef unsigned int size_t;
+double abs(double num);
 
 char* ftoa(double x, char *a, unsigned int prec);
 char* itoa(long long n, char *a, unsigned short base);
-int atoi(char *a);
+s64 atoi(char *a);
 double atof(char *a);
 
 void sPrint(char *a);
 char* sGet(char *a, unsigned int n);
 
+void strcpy(char *dest, const char *src);
 int strcmp(const char *a, const char *b);
 
 int arrayLookup(char *text, const char array[][10], int last);
--- a/include/system.h	Sun Mar 20 14:17:24 2011 -0400
+++ b/include/system.h	Tue Mar 29 22:05:03 2011 -0400
@@ -5,6 +5,12 @@
 #define CON_LEN 132
 #define HEAP_START 0x200000
 
+#define INF __builtin_inff()
+#define NAN __builtin_nanf("")
+
+typedef unsigned long long size_t;
+typedef unsigned long long intptr_t;
+
 typedef unsigned long long u64;
 typedef signed long long s64;
 
--- a/src/dynamic.c	Sun Mar 20 14:17:24 2011 -0400
+++ b/src/dynamic.c	Tue Mar 29 22:05:03 2011 -0400
@@ -65,6 +65,9 @@
 	b = malloc(400);
 	printMem();
 	sPrint("DONE\n");
+	free(a);
+	free(c);
+	free(d);
 
 	for(;;)
 		sGet(input, 0);
--- a/src/math.c	Sun Mar 20 14:17:24 2011 -0400
+++ b/src/math.c	Tue Mar 29 22:05:03 2011 -0400
@@ -1,24 +1,24 @@
-double math_iPow(double base, int exp)
+double iPow(double base, int exponent)
 {
 	double ans = 1;
 	short isNeg = 0;
-	if(exp < 0) {
+	if(exponent < 0) {
 		isNeg = 1;
-		exp *= -1;
+		exponent *= -1;
 	}
-	while(exp > 0) {
-		if(exp%2) {
-			exp -= 1;
+	while(exponent > 0) {
+		if(exponent%2) {
+			exponent -= 1;
 			ans *= base;
 		} else {
-			exp /= 2;
+			exponent /= 2;
 			base *= base;
 		}
 	}
 	return isNeg ? 1/ans : ans;
 }
 
-double math_exp(double exp)
+double exp(double exp)
 {
 	unsigned int i; //a counter for the loop
 	double ans = 1;
@@ -37,7 +37,7 @@
 	return isNeg ? 1/ans : ans;
 }
 
-double math_log(double num)
+double log(double num)
 {
 	unsigned int denom = 1;
 	double euler = 2.71828182845904;
@@ -56,17 +56,11 @@
 	return ans;
 }
 
-double math_pow(double base, double exp)
+double pow(double base, double exponent)
 {
-	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;
+	if(exponent == (int) exponent)
+		return iPow(base, (int) exponent);
+	return exp(exponent*log(base));
 }
 
 double math_add(double first, double sec)
--- a/src/operations.c	Sun Mar 20 14:17:24 2011 -0400
+++ b/src/operations.c	Tue Mar 29 22:05:03 2011 -0400
@@ -67,7 +67,7 @@
 
 void op_pow(Stack *stack)
 {
-	op_math2(stack, math_pow);
+	op_math2(stack, pow);
 }
 
 void op_dup(Stack *stack)
@@ -92,12 +92,12 @@
 
 void op_log(Stack *stack)
 {
-	op_math1(stack, math_log);
+	op_math1(stack, log);
 }
 
 void op_exp(Stack *stack)
 {
-	op_math1(stack, math_exp);
+	op_math1(stack, exp);
 }
 
 void op_swap(Stack *stack)
--- a/src/std.c	Sun Mar 20 14:17:24 2011 -0400
+++ b/src/std.c	Tue Mar 29 22:05:03 2011 -0400
@@ -1,6 +1,12 @@
 #include <std.h>
 
-char* itoa(long long n, char *a, unsigned short base)
+double abs(double num)
+{
+	if(num < 0) return num*-1;
+	else return num;
+}
+
+char* itoa(s64 n, char *a, unsigned short base)
 {
 	char *ret = a;
 	if(n < 0) {
@@ -27,36 +33,49 @@
 char* ftoa(double x, char *a, unsigned int prec)
 {
 	char *ret = a;
-	int n = (int) x; //integer part
-	double d = x - (double) n; //fractional part;
-	itoa(n, a, 10);
-	if(prec) { //only do the decimal part if decimal parts were asked for
-		while(*a && *++a); //get to the null character from itoa
-		int i; //counter variable for the for loop
-		*a++ = '.'; //put the decimal in place
-		for(i = 0; i < prec; i++) {
-			d = d*10; //the integer part is the decimal digit
-			*a++ = ((int) d) + '0'; //add the integer part of d to the string
-			d -= (int) d; //chop off the integer part
-		} a--; //move back to the last digit
-		while(*a != '.') {
-			if(*a == '0') {
-				a--;
-				continue;
-			} else if(*a > '0' && *a <= '9') {
-				a++;
-				break;
+	if(x == INF || x == -INF) {
+		strcpy(a, "INF");
+	} else if(x != x) { //NAN != NAN
+		strcpy(a, "NAN");
+	} else {
+		s64 n = (s64) x; //integer part
+		double d = abs(x - (double) n); //fractional part;
+		if(d != 0) sPrint("WORKING\n");
+		itoa(n, a, 10);
+		if(prec) { //only do the decimal part if decimal parts were asked for
+			while(*a && *++a); //get to the null character from itoa
+			int i; //counter variable for the for loop
+			*a++ = '.'; //put the decimal in place
+			for(i = 0; i < prec; i++) {
+				d *= 10; //the integer part is the decimal digit
+				char aoeu[43];
+				sPrint(itoa((int) d, aoeu, 10));
+				sPrint("\n");
+				*a++ = ((int) d) + '0'; //add the integer part of d to the string
+				d -= (int) d; //chop off the integer part
+			} a--; //move back to the last digit
+			while(*a != '.') {
+				if(*a == '0') {
+					a--;
+					continue;
+				} else if(*a > '0' && *a <= '9') {
+					a++;
+					break;
+				} else {
+					sPrint("ERROR: SOMETHING IS VERY WRONG\n");
+					break;
+				}
 			}
+			*a = '\0';
 		}
-		*a = '\0';
 	}
 	return ret;
 }
 
-int atoi(char *a)
+s64 atoi(char *a)
 {
 	short neg = 0;
-	int n = 0;
+	s64 n = 0;
 	if(*a == '-') {
 		neg = 1;
 		a++;
@@ -69,11 +88,16 @@
 
 double atof(char *a)
 {
-	int n = atoi(a);
+	s64 n = atoi(a);
 	double x = 0;
 	double dec = .1;
+	short neg = 0;
+	if(*a == '-') {
+		neg = 1;
+		a++;
+	}
 	while(*a != '.') {
-		if(!(*a >= '0' && *a <= '9')) return n;
+		if(!(*a >= '0' && *a <= '9') && *a != '-') return n;
 		a++;
 	} a++; //a will be immediately after the decimal point
 	while(*a >= '0' && *a <= '9') { //goes through the decimal part
@@ -81,6 +105,7 @@
 		dec *= .1;
 		a++;
 	}
+	if(neg) x*=-1;
 	return n + x;
 }
 
@@ -94,6 +119,11 @@
 	} while(a < b);
 }
 
+void strcpy(char *dest, const char *src)
+{
+	while((*dest++ = *src++));
+}
+
 int strcmp(const char *a, const char *b)
 {
 	while(1) {
@@ -193,13 +223,13 @@
 {
 	char buffer[20];
 	sPrint("Memory address is: ");
-	sPrint(itoa((size_t)head, buffer, 16));
+	sPrint(itoa((intptr_t)head, buffer, 16));
 	sPrint("\n");
 	sPrint("The size of the block is: ");
 	sPrint(itoa(head->size, buffer, 10));
 	sPrint("\n");
 	sPrint("Next memory address is: ");
-	sPrint(itoa((size_t)head->next, buffer, 16));
+	sPrint(itoa((intptr_t)head->next, buffer, 16));
 	sPrint("\n\n");
 }