blob: 35d3feb425cf3e630bcf44cc158d8ddb9a2c4aec [file] [log] [blame]
Guido van Rossum2b7e04a1995-02-19 15:54:36 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum2b7e04a1995-02-19 15:54:36 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum2b7e04a1995-02-19 15:54:36 +00009******************************************************************/
10
11/* Just in case you haven't got an atof() around...
12 This one doesn't check for bad syntax or overflow,
13 and is slow and inaccurate.
14 But it's good enough for the occasional string literal... */
15
Guido van Rossum2b7e04a1995-02-19 15:54:36 +000016#include "config.h"
Guido van Rossum2b7e04a1995-02-19 15:54:36 +000017
18#include <ctype.h>
19
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000020double atof(char *s)
Guido van Rossum2b7e04a1995-02-19 15:54:36 +000021{
22 double a = 0.0;
23 int e = 0;
24 int c;
25 while ((c = *s++) != '\0' && isdigit(c)) {
26 a = a*10.0 + (c - '0');
27 }
28 if (c == '.') {
29 while ((c = *s++) != '\0' && isdigit(c)) {
30 a = a*10.0 + (c - '0');
31 e = e-1;
32 }
33 }
34 if (c == 'e' || c == 'E') {
35 int sign = 1;
36 int i = 0;
37 c = *s++;
38 if (c == '+')
39 c = *s++;
40 else if (c == '-') {
41 c = *s++;
42 sign = -1;
43 }
44 while (isdigit(c)) {
45 i = i*10 + (c - '0');
46 c = *s++;
47 }
48 e += i*sign;
49 }
50 while (e > 0) {
51 a *= 10.0;
52 e--;
53 }
54 while (e < 0) {
55 a *= 0.1;
56 e++;
57 }
58 return a;
59}