public
Created

  • Download Gist
strtod.c
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
/* vi:set ts=8 sts=4 sw=4: */
/*
* strtod implementation.
* author: Yasuhiro Matsumoto
* license: public domain
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <math.h>
 
char*
skipwhite(q)
char *q;
{
char *p = q;
while (isspace(*p))
++p;
return p;
}
#define vim_isdigit(x) isdigit(x)
 
double
vim_strtod(str, end)
const char *str;
char **end;
{
double d = 0.0;
int sign;
const char *p, *a;
 
a = p = str;
p = skipwhite(p);
 
/* decimal part */
sign = 1;
if (*p == '-')
{
sign = -1;
++p;
} else if (*p == '+')
++p;
if (vim_isdigit(*p))
{
d = (double)(*p++ - '0');
while (*p && vim_isdigit(*p))
{
d = d * 10.0 + (double)(*p - '0');
++p;
}
a = p;
} else if (*p != '.')
goto done;
d *= sign;
 
/* fraction part */
if (*p == '.')
{
double f = 0.0;
double base = 0.1;
++p;
 
if (vim_isdigit(*p))
{
while (*p && vim_isdigit(*p))
{
f += base * (*p - '0') ;
base /= 10.0;
++p;
}
}
d += f * sign;
a = p;
}
 
/* exponential part */
if ((*p == 'E') || (*p == 'e'))
{
double e = 0.0;
++p;
 
sign = 1;
if (*p == '-')
{
sign = -1;
++p;
} else if (*p == '+')
++p;
 
if (vim_isdigit(*p))
{
while (*p == '0')
++p;
e = (double)(*p++ - '0');
while (*p && vim_isdigit(*p))
e = e * 10 + (double)(*p++ - '0');
}
else if (!vim_isdigit(*(a-1)))
{
a = str;
goto done;
}
else if (*p == 0)
goto done;
 
d *= pow(10.0, e * sign);
a = p;
}
else if (p > str && !vim_isdigit(*(p-1)))
{
a = str;
goto done;
}
 
done:
if (end)
*end = (char*)a;
return d;
}
 
void
test(char* str)
{
double d1, d2, d3;
char *e1, *e2, *e3;
 
printf("CASE: %s\n", str);
setlocale(LC_ALL, "Polish");
e1 = NULL;
d1 = vim_strtod(str, &e1);
setlocale(LC_ALL, "");
e2 = NULL;
d2 = vim_strtod(str, &e2);
e3 = NULL;
d3 = strtod(str, &e3);
if (d1 != d2 || d1 != d3 || e1 != e2 || e1 != e3) {
printf("ERR: %s\n", str);
printf(" E1 %f, %g, %s\n", d1, d1, e1 ? e1 : "");
printf(" E2 %f, %g, %s\n", d2, d2, e2 ? e2 : "");
printf(" E3 %f, %g, %s\n", d3, d3, e3 ? e3 : "");
}
}
 
int
main()
{
test(".1");
test(" .");
test(" 1.2e3");
test(" +1.2e3");
test("1.2e3");
test("+1.2e3");
test("+1.e3");
test("-1.2e3");
test("-1.2e3.5");
test("-1.2e");
test("--1.2e3.5");
test("--1-.2e3.5");
test("-a");
test("a");
test(".1e");
test(".1e3");
test(".1e-3");
test(".1e-");
test(" .e-");
test(" .e");
test(" e");
test(" e0");
test(" ee");
test(" -e");
test(" .9");
test(" ..9");
test("009");
test("0.09e02");
test("0.9999999999999999999999999999999999");
/* PHP (slashdot.jp): http://opensource.slashdot.jp/story/11/01/08/0527259/PHP%E3%81%AE%E6%B5%AE%E5%8B%95%E5%B0%8F%E6%95%B0%E7%82%B9%E5%87%A6%E7%90%86%E3%81%AB%E7%84%A1%E9%99%90%E3%83%AB%E3%83%BC%E3%83%97%E3%81%AE%E3%83%90%E3%82%B0 */
test("2.2250738585072011e-308");
/* Gauche: http://blog.practical-scheme.net/gauche/20110203-bitten-by-floating-point-numbers-again */
test("2.2250738585072012e-308");
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.