Add incredibly bad atof

This commit is contained in:
K. Lange 2018-05-02 19:43:33 +09:00
parent 2a4914057c
commit 300d5b78ca
3 changed files with 43 additions and 2 deletions

View File

@ -214,7 +214,6 @@ int main(int argc, char * argv[]) {
case 'n':
no_repeat = 1;
break;
#if 0
case 'i':
initer = atof(optarg);
break;
@ -230,7 +229,6 @@ int main(int argc, char * argv[]) {
case 'C':
cony = atof(optarg);
break;
#endif
case 'W':
width = atoi(optarg);
break;

View File

@ -19,4 +19,6 @@ extern int abs(int j);
extern int putenv(char * name);
extern int setenv(const char *name, const char *value, int overwrite);
extern double atof(const char * nptr);
#define NULL 0

41
libc/stdlib/atof.c Normal file
View File

@ -0,0 +1,41 @@
/* Really bad atof */
#include <stdlib.h>
double atof(const char * nptr) {
int sign = 1;
if (*nptr == '-') {
sign = -1;
nptr++;
}
long long decimal_part = 0;
while (*nptr && *nptr != '.') {
if (*nptr < '0' || *nptr > '9') {
return 0.0;
}
decimal_part *= 10LL;
decimal_part += (long long)(*nptr - '0');
nptr++;
}
double sub_part = 0;
double multiplier = 0.1;
if (*nptr == '.') {
nptr++;
while (*nptr) {
if (*nptr < '0' || *nptr > '9') {
return ((double)decimal_part) * (double)(sign);
}
sub_part += multiplier * (*nptr - '0');
multiplier *= 0.1;
nptr++;
}
}
return ((double)decimal_part + sub_part) * (double)(sign);
}