forked from mruby/mruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadfloat.c
More file actions
94 lines (78 loc) · 1.74 KB
/
readfloat.c
File metadata and controls
94 lines (78 loc) · 1.74 KB
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
#include <mruby.h>
#ifndef MRB_NO_FLOAT
#include <string.h>
#include <math.h>
MRB_API mrb_bool
mrb_read_float(const char *str, char **endp, double *fp)
{
const char *p = str;
const char *a = p;
double res = 0.0, frac = 0.0, div = 1.0;
int sign = 1;
int digits = 0;
// Skip whitespace
while (ISSPACE((unsigned char)*p)) p++;
// Handle sign
if (*p == '-') { sign = -1; p++; }
else if (*p == '+') p++;
// Parse integer part
while (ISDIGIT(*p)) {
res = res * 10.0 + (*p - '0');
digits++;
a = ++p;
}
// Parse fractional part
if (*p == '.') {
p++;
while (ISDIGIT(*p)) {
frac = frac * 10.0 + (*p++ - '0');
div *= 10.0;
digits++;
}
a = p;
}
// If no digits were found, return 0
if (digits == 0) {
if (endp) *endp = (char*)str;
*fp = 0.0;
return FALSE;
}
// Combine integer and fractional parts
res += frac / div;
res *= sign;
// Handle exponent
if ((*p | 32) == 'e') {
int e = 0;
sign = 1;
p++;
if (*p == '-') { sign = -1; p++; }
else if (*p == '+') p++;
// If no digits follow 'e', ignore the exponent part
if (!ISDIGIT(*p)) goto done;
while (ISDIGIT(*p)) {
if (e < 10000) // 10000 is big enough to get Infinity
e = e * 10 + (*p - '0');
p++;
}
res *= pow(10.0, sign * e);
a = p;
}
// Set endp
done:
if (endp) *endp = (char*)a;
*fp = res;
// strtod(3) stores ERANGE to errno for overflow/underflow
// mruby does not require those checks
#if 0
// Check for underflow after applying the exponent
if (res != 0.0 && fabs(res) < DBL_MIN) {
return FALSE;
}
// Check if the result is infinity or NaN
if (isinf(res) || isnan(res)) {
return FALSE;
}
#endif
return TRUE;
}
#endif