-
Notifications
You must be signed in to change notification settings - Fork 1
/
atof.c
79 lines (69 loc) · 1.67 KB
/
atof.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
/* atof:convert string s to double */
double atof_offset(const char s[], int start_index)
{
double val, power, exponent, exponent_modifier, base_and_exponent;
int i, sign;
/* skip white space */
for (i = start_index; isspace(s[i]); i++) {
;
}
sign=(s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-') {
i++;
}
for (val = 0.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '0');
}
if (s[i]== '.') {
i++;
}
for (power = 1.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '0');
power *= 10;
}
base_and_exponent = 1;
if (s[i]== 'e') {
i++;
exponent = atof_offset(s, i);
if (exponent < 0) {
exponent_modifier = 0.1;
exponent = -exponent;
} else {
exponent_modifier = 10;
}
/* printf("Exponent: %f\n", exponent); */
for(int j = 0; j < exponent; j++) {
base_and_exponent *= exponent_modifier;
/* printf("base and mod: %f\n", base_and_exponent); */
}
}
return sign * val * base_and_exponent/ power;
}
double atof(const char s[])
{
double atof_offset(const char s[], int start_index);
return atof_offset(s, 0);
}
void test_atof(const char *s, double expected)
{
double result = atof(s);
printf("%25f\n%25f\n\n", expected, result);
/* comparing floats is non-trivial, eyeball it. */
/* assert(expected == result); */
}
int main()
{
printf("Done!\n");
test_atof("0",0);
test_atof("1", 1);
test_atof("-1", -1);
test_atof("-123", -123);
test_atof("3.14", 3.14);
test_atof("123.45", 123.45);
test_atof("123.45e6", 123.45e6);
test_atof("123.45e-2", 123.45e-2);
test_atof("123.45e-6", 123.45e-6);
}