2022-04-15 11:00:55 +02:00
|
|
|
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
2021-11-01 15:46:28 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
2022-04-15 11:00:55 +02:00
|
|
|
#ifndef unconst
|
|
|
|
#define unconst(__v, __t) __extension__({union { const __t __cp; __t __p; } __q; __q.__cp = __v; __q.__p; })
|
|
|
|
#endif
|
2021-11-01 15:46:28 +01:00
|
|
|
|
2022-04-15 11:00:55 +02:00
|
|
|
double strtod(const char* s, char** sret)
|
2021-11-01 15:46:28 +01:00
|
|
|
{
|
2022-04-15 11:00:55 +02:00
|
|
|
long double r; /* result */
|
|
|
|
int e; /* exponent */
|
|
|
|
long double d; /* scale */
|
|
|
|
int sign; /* +- 1.0 */
|
2021-11-01 15:46:28 +01:00
|
|
|
int esign;
|
|
|
|
int i;
|
2022-04-15 11:00:55 +02:00
|
|
|
int flags = 0;
|
2021-11-01 15:46:28 +01:00
|
|
|
|
|
|
|
r = 0.0;
|
|
|
|
sign = 1.0;
|
|
|
|
e = 0;
|
|
|
|
esign = 1;
|
|
|
|
|
|
|
|
while ((*s == ' ') || (*s == '\t'))
|
|
|
|
s++;
|
2022-04-15 11:00:55 +02:00
|
|
|
|
2021-11-01 15:46:28 +01:00
|
|
|
if (*s == '+')
|
|
|
|
s++;
|
2022-04-15 11:00:55 +02:00
|
|
|
else if (*s == '-') {
|
2021-11-01 15:46:28 +01:00
|
|
|
sign = -1;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
2022-04-15 11:00:55 +02:00
|
|
|
while ((*s >= '0') && (*s <= '9')) {
|
2021-11-01 15:46:28 +01:00
|
|
|
flags |= 1;
|
|
|
|
r *= 10.0;
|
|
|
|
r += *s - '0';
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
2022-04-15 11:00:55 +02:00
|
|
|
if (*s == '.') {
|
2021-11-01 15:46:28 +01:00
|
|
|
d = 0.1L;
|
|
|
|
s++;
|
2022-04-15 11:00:55 +02:00
|
|
|
|
|
|
|
while ((*s >= '0') && (*s <= '9')) {
|
2021-11-01 15:46:28 +01:00
|
|
|
flags |= 2;
|
|
|
|
r += d * (*s - '0');
|
|
|
|
s++;
|
|
|
|
d *= 0.1L;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 11:00:55 +02:00
|
|
|
if (flags == 0) {
|
2021-11-01 15:46:28 +01:00
|
|
|
if (sret)
|
2022-04-15 11:00:55 +02:00
|
|
|
*sret = unconst(s, char*);
|
2021-11-01 15:46:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-15 11:00:55 +02:00
|
|
|
if ((*s == 'e') || (*s == 'E')) {
|
2021-11-01 15:46:28 +01:00
|
|
|
s++;
|
|
|
|
if (*s == '+')
|
|
|
|
s++;
|
2022-04-15 11:00:55 +02:00
|
|
|
else if (*s == '-') {
|
2021-11-01 15:46:28 +01:00
|
|
|
s++;
|
|
|
|
esign = -1;
|
|
|
|
}
|
2022-04-15 11:00:55 +02:00
|
|
|
|
|
|
|
if ((*s < '0') || (*s > '9')) {
|
2021-11-01 15:46:28 +01:00
|
|
|
if (sret)
|
2022-04-15 11:00:55 +02:00
|
|
|
*sret = unconst(s, char*);
|
2021-11-01 15:46:28 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2022-04-15 11:00:55 +02:00
|
|
|
while ((*s >= '0') && (*s <= '9')) {
|
2021-11-01 15:46:28 +01:00
|
|
|
e *= 10;
|
|
|
|
e += *s - '0';
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (esign < 0)
|
|
|
|
for (i = 1; i <= e; i++)
|
|
|
|
r *= 0.1L;
|
|
|
|
else
|
|
|
|
for (i = 1; i <= e; i++)
|
|
|
|
r *= 10.0;
|
|
|
|
|
|
|
|
if (sret)
|
2022-04-15 11:00:55 +02:00
|
|
|
*sret = unconst(s, char*);
|
2021-11-01 15:46:28 +01:00
|
|
|
return r * sign;
|
2022-04-15 11:00:55 +02:00
|
|
|
}
|