Mercurial > sos > sos
diff src/std.c @ 5:348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Wed, 02 Mar 2011 15:43:05 -0500 |
parents | 90cd3d9a6ca3 |
children | 25b2b501a5fa |
line wrap: on
line diff
--- a/src/std.c Wed Mar 02 00:37:32 2011 -0500 +++ b/src/std.c Wed Mar 02 15:43:05 2011 -0500 @@ -28,19 +28,25 @@ int n = (int) x; //integer part double d = x - (double) n; //fractional part; itoa(n, a); - if(prec) { - while(*a && *++a); - int i; - *a++ = '.'; + 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; - *a++ = ((int) d) + '0'; - d -= (int) d; + 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; + } } - a--; //move back to the last digit -// while(*a == '0') a--; //TODO figure out why this "works" even with the other version of init.c - while(*a == '0' || *a == '.') a--; //get to the first non-zero decimal digit - *(a + 1) = '\0'; + *a = '\0'; } return ret; } @@ -64,8 +70,11 @@ int n = atoi(a); double x = 0; double dec = .1; - while(*a++ != '.'); - while(*a != '\0') { + while(*a != '.') { + if(*a == '\0') return n; + a++; + } a++; //a will be immediately after the decimal point + while(*a >= '0' && *a <= '9') { //goes through the decimal part x += (*a - '0')*dec; dec *= .1; a++;