blob: 894490a47b98c382d43f7574aba17d54afb58d21 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Just in case you haven't got an atof() around...
26 This one doesn't check for bad syntax or overflow,
27 and is slow and inaccurate.
28 But it's good enough for the occasional string literal... */
29
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#include <ctype.h>
35
36double atof(s)
37 char *s;
38{
39 double a = 0.0;
40 int e = 0;
41 int c;
42 while ((c = *s++) != '\0' && isdigit(c)) {
43 a = a*10.0 + (c - '0');
44 }
45 if (c == '.') {
46 while ((c = *s++) != '\0' && isdigit(c)) {
47 a = a*10.0 + (c - '0');
48 e = e-1;
49 }
50 }
51 if (c == 'e' || c == 'E') {
52 int sign = 1;
53 int i = 0;
54 c = *s++;
55 if (c == '+')
56 c = *s++;
57 else if (c == '-') {
58 c = *s++;
59 sign = -1;
60 }
61 while (isdigit(c)) {
62 i = i*10 + (c - '0');
63 c = *s++;
64 }
65 e += i*sign;
66 }
67 while (e > 0) {
68 a *= 10.0;
69 e--;
70 }
71 while (e < 0) {
72 a *= 0.1;
73 e++;
74 }
75 return a;
76}