Mercurial > sos > sos
comparison src/std.c @ 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 |
comparison
equal
deleted
inserted
replaced
26:c1ad124f2aaf | 27:1a070e843bf6 |
---|---|
1 #include <std.h> | 1 #include <std.h> |
2 | 2 |
3 char* itoa(long long n, char *a, unsigned short base) | 3 double abs(double num) |
4 { | |
5 if(num < 0) return num*-1; | |
6 else return num; | |
7 } | |
8 | |
9 char* itoa(s64 n, char *a, unsigned short base) | |
4 { | 10 { |
5 char *ret = a; | 11 char *ret = a; |
6 if(n < 0) { | 12 if(n < 0) { |
7 *a++ = '-'; | 13 *a++ = '-'; |
8 n *= -1; | 14 n *= -1; |
25 } | 31 } |
26 | 32 |
27 char* ftoa(double x, char *a, unsigned int prec) | 33 char* ftoa(double x, char *a, unsigned int prec) |
28 { | 34 { |
29 char *ret = a; | 35 char *ret = a; |
30 int n = (int) x; //integer part | 36 if(x == INF || x == -INF) { |
31 double d = x - (double) n; //fractional part; | 37 strcpy(a, "INF"); |
32 itoa(n, a, 10); | 38 } else if(x != x) { //NAN != NAN |
33 if(prec) { //only do the decimal part if decimal parts were asked for | 39 strcpy(a, "NAN"); |
34 while(*a && *++a); //get to the null character from itoa | 40 } else { |
35 int i; //counter variable for the for loop | 41 s64 n = (s64) x; //integer part |
36 *a++ = '.'; //put the decimal in place | 42 double d = abs(x - (double) n); //fractional part; |
37 for(i = 0; i < prec; i++) { | 43 if(d != 0) sPrint("WORKING\n"); |
38 d = d*10; //the integer part is the decimal digit | 44 itoa(n, a, 10); |
39 *a++ = ((int) d) + '0'; //add the integer part of d to the string | 45 if(prec) { //only do the decimal part if decimal parts were asked for |
40 d -= (int) d; //chop off the integer part | 46 while(*a && *++a); //get to the null character from itoa |
41 } a--; //move back to the last digit | 47 int i; //counter variable for the for loop |
42 while(*a != '.') { | 48 *a++ = '.'; //put the decimal in place |
43 if(*a == '0') { | 49 for(i = 0; i < prec; i++) { |
44 a--; | 50 d *= 10; //the integer part is the decimal digit |
45 continue; | 51 char aoeu[43]; |
46 } else if(*a > '0' && *a <= '9') { | 52 sPrint(itoa((int) d, aoeu, 10)); |
47 a++; | 53 sPrint("\n"); |
48 break; | 54 *a++ = ((int) d) + '0'; //add the integer part of d to the string |
55 d -= (int) d; //chop off the integer part | |
56 } a--; //move back to the last digit | |
57 while(*a != '.') { | |
58 if(*a == '0') { | |
59 a--; | |
60 continue; | |
61 } else if(*a > '0' && *a <= '9') { | |
62 a++; | |
63 break; | |
64 } else { | |
65 sPrint("ERROR: SOMETHING IS VERY WRONG\n"); | |
66 break; | |
67 } | |
49 } | 68 } |
69 *a = '\0'; | |
50 } | 70 } |
51 *a = '\0'; | |
52 } | 71 } |
53 return ret; | 72 return ret; |
54 } | 73 } |
55 | 74 |
56 int atoi(char *a) | 75 s64 atoi(char *a) |
57 { | 76 { |
58 short neg = 0; | 77 short neg = 0; |
59 int n = 0; | 78 s64 n = 0; |
60 if(*a == '-') { | 79 if(*a == '-') { |
61 neg = 1; | 80 neg = 1; |
62 a++; | 81 a++; |
63 } else if(*a == '+') a++; | 82 } else if(*a == '+') a++; |
64 while(*a >= '0' && *a <= '9') | 83 while(*a >= '0' && *a <= '9') |
67 return n; | 86 return n; |
68 } | 87 } |
69 | 88 |
70 double atof(char *a) | 89 double atof(char *a) |
71 { | 90 { |
72 int n = atoi(a); | 91 s64 n = atoi(a); |
73 double x = 0; | 92 double x = 0; |
74 double dec = .1; | 93 double dec = .1; |
94 short neg = 0; | |
95 if(*a == '-') { | |
96 neg = 1; | |
97 a++; | |
98 } | |
75 while(*a != '.') { | 99 while(*a != '.') { |
76 if(!(*a >= '0' && *a <= '9')) return n; | 100 if(!(*a >= '0' && *a <= '9') && *a != '-') return n; |
77 a++; | 101 a++; |
78 } a++; //a will be immediately after the decimal point | 102 } a++; //a will be immediately after the decimal point |
79 while(*a >= '0' && *a <= '9') { //goes through the decimal part | 103 while(*a >= '0' && *a <= '9') { //goes through the decimal part |
80 x += (*a - '0')*dec; | 104 x += (*a - '0')*dec; |
81 dec *= .1; | 105 dec *= .1; |
82 a++; | 106 a++; |
83 } | 107 } |
108 if(neg) x*=-1; | |
84 return n + x; | 109 return n + x; |
85 } | 110 } |
86 | 111 |
87 void sPrint(char *a) | 112 void sPrint(char *a) |
88 { | 113 { |
90 while(*b && *++b); | 115 while(*b && *++b); |
91 do { | 116 do { |
92 putline(a, (b - a > CON_LEN)?CON_LEN:(b - a)); | 117 putline(a, (b - a > CON_LEN)?CON_LEN:(b - a)); |
93 a += CON_LEN; | 118 a += CON_LEN; |
94 } while(a < b); | 119 } while(a < b); |
120 } | |
121 | |
122 void strcpy(char *dest, const char *src) | |
123 { | |
124 while((*dest++ = *src++)); | |
95 } | 125 } |
96 | 126 |
97 int strcmp(const char *a, const char *b) | 127 int strcmp(const char *a, const char *b) |
98 { | 128 { |
99 while(1) { | 129 while(1) { |
191 | 221 |
192 void printHeader(Header* head) | 222 void printHeader(Header* head) |
193 { | 223 { |
194 char buffer[20]; | 224 char buffer[20]; |
195 sPrint("Memory address is: "); | 225 sPrint("Memory address is: "); |
196 sPrint(itoa((size_t)head, buffer, 16)); | 226 sPrint(itoa((intptr_t)head, buffer, 16)); |
197 sPrint("\n"); | 227 sPrint("\n"); |
198 sPrint("The size of the block is: "); | 228 sPrint("The size of the block is: "); |
199 sPrint(itoa(head->size, buffer, 10)); | 229 sPrint(itoa(head->size, buffer, 10)); |
200 sPrint("\n"); | 230 sPrint("\n"); |
201 sPrint("Next memory address is: "); | 231 sPrint("Next memory address is: "); |
202 sPrint(itoa((size_t)head->next, buffer, 16)); | 232 sPrint(itoa((intptr_t)head->next, buffer, 16)); |
203 sPrint("\n\n"); | 233 sPrint("\n\n"); |
204 } | 234 } |
205 | 235 |
206 void printMem() | 236 void printMem() |
207 { | 237 { |