blob: db4f706a117ca51c6c1741106b42a6baa0cdbdd0 [file] [log] [blame]
Guido van Rossum2b7e04a1995-02-19 15:54:36 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossum2b7e04a1995-02-19 15:54:36 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum2b7e04a1995-02-19 15:54:36 +000014
15******************************************************************/
16
17/* Just in case you haven't got an atof() around...
18 This one doesn't check for bad syntax or overflow,
19 and is slow and inaccurate.
20 But it's good enough for the occasional string literal... */
21
Guido van Rossum2b7e04a1995-02-19 15:54:36 +000022#include "config.h"
Guido van Rossum2b7e04a1995-02-19 15:54:36 +000023
24#include <ctype.h>
25
26double atof(s)
27 char *s;
28{
29 double a = 0.0;
30 int e = 0;
31 int c;
32 while ((c = *s++) != '\0' && isdigit(c)) {
33 a = a*10.0 + (c - '0');
34 }
35 if (c == '.') {
36 while ((c = *s++) != '\0' && isdigit(c)) {
37 a = a*10.0 + (c - '0');
38 e = e-1;
39 }
40 }
41 if (c == 'e' || c == 'E') {
42 int sign = 1;
43 int i = 0;
44 c = *s++;
45 if (c == '+')
46 c = *s++;
47 else if (c == '-') {
48 c = *s++;
49 sign = -1;
50 }
51 while (isdigit(c)) {
52 i = i*10 + (c - '0');
53 c = *s++;
54 }
55 e += i*sign;
56 }
57 while (e > 0) {
58 a *= 10.0;
59 e--;
60 }
61 while (e < 0) {
62 a *= 0.1;
63 e++;
64 }
65 return a;
66}