-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_to_float.c
141 lines (134 loc) · 3.42 KB
/
string_to_float.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <errno.h>
#include <fenv.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include "string_to_float.h"
double string_to_float_s(char *str, size_t len)
{
errno = 0;
if(!str) {
errno = EINVAL;
return -INFINITY;
}
size_t i = 0, n_integer = 0, n_fraction = 0, n_exponent = 0;
double integer = 0, integer_sign = 1;
double fraction = 0;
double exponent = 0, exponent_sign = 1;
double result = 0;
/* skip leading whitespace */
while(i < len && isspace((int)str[i])) {
i++;
}
/* get number of digits of int part */
size_t nd = i;
if(str[nd] == 'n' || str[nd] == 'N') {
nd ++;
if (str[nd] == 'a' || str[nd] == 'A') {
nd++;
if (str[nd] == 'n' || str[nd] == 'N') {
return NAN;
} else {
goto error;
}
} else {
goto error;
}
}
/* integer sign */
if(str[nd] == '+') {
nd++;
i++;
} else if(str[nd] == '-') {
nd++;
i++;
integer_sign *= -1;
}
/* skip integer digits */
while(nd < len && isdigit((int)str[nd])) {
nd++;
}
/* decode digit */
double f = 1.0;
for(size_t k = 0; k < nd - i; k++) {
double digit = str[nd - k - 1] - '0';
integer += (digit * f);
n_integer++;
f *= 10.0;
}
i = nd;
/* check for comma */
if(i < len && str[i] == '.') {
i++; /* skip '.' */
/* decode digits */
double f = 0.1;
while(i < len && isdigit((int)str[i])) {
double digit = str[i] - '0';
//printf("%s\n", &str[i]);
fraction += (digit * f);
n_fraction++;
f *= 0.1;
i++;
}
}
/* check for exponent */
if(i < len && (str[i] == 'e' || str[i] == 'E')) {
/* required to have beginning as an integer or fraction! */
if(!n_integer && !n_fraction) {
goto error;
}
i++; /* skip 'e' */
if(i < len) {
if(str[i] == '+') {
i++; /* skip '+' */
} else if(str[i] == '-') {
i++; /* skip '-' */
exponent_sign *= -1;
}
}
size_t ne = i;
/* skip exponent digits */
while(ne < len && isdigit((int)str[ne])) {
ne++;
}
/* decode digit */
double f = 1.0;
for(size_t k = 0; k < ne - i; k++) {
double digit = str[ne - k - 1] - '0';
exponent += (digit * f);
n_exponent++;
f *= 10.0;
}
if(!n_exponent) {
goto error;
}
i = ne;
}
/* skip trailing whitespace */
while(i < len && isspace((int)str[i])) {
i++;
}
/* error */
if(errno || ((i < len) && str[i])) {
goto error;
}
/* success */
result = integer_sign * (integer + fraction) * pow(10, exponent_sign * exponent);
//printf("%f = %f * (%f + %f) * pow(10, %f)\n", result, integer_sign, integer, fraction, exponent);
return result;
/* please agree with me that a goto error is nice to have :) */
error:
errno = EINVAL;
return NAN;
}
double string_to_float(char *str)
{
if(!str) {
errno = EINVAL;
return -INFINITY;
}
size_t len = strlen(str);
return string_to_float_s(str, len);
}