Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 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 Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 16 | #include "config.h" |
Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 17 | |
| 18 | #include <ctype.h> |
| 19 | |
| 20 | double atof(s) |
| 21 | char *s; |
| 22 | { |
| 23 | double a = 0.0; |
| 24 | int e = 0; |
| 25 | int c; |
| 26 | while ((c = *s++) != '\0' && isdigit(c)) { |
| 27 | a = a*10.0 + (c - '0'); |
| 28 | } |
| 29 | if (c == '.') { |
| 30 | while ((c = *s++) != '\0' && isdigit(c)) { |
| 31 | a = a*10.0 + (c - '0'); |
| 32 | e = e-1; |
| 33 | } |
| 34 | } |
| 35 | if (c == 'e' || c == 'E') { |
| 36 | int sign = 1; |
| 37 | int i = 0; |
| 38 | c = *s++; |
| 39 | if (c == '+') |
| 40 | c = *s++; |
| 41 | else if (c == '-') { |
| 42 | c = *s++; |
| 43 | sign = -1; |
| 44 | } |
| 45 | while (isdigit(c)) { |
| 46 | i = i*10 + (c - '0'); |
| 47 | c = *s++; |
| 48 | } |
| 49 | e += i*sign; |
| 50 | } |
| 51 | while (e > 0) { |
| 52 | a *= 10.0; |
| 53 | e--; |
| 54 | } |
| 55 | while (e < 0) { |
| 56 | a *= 0.1; |
| 57 | e++; |
| 58 | } |
| 59 | return a; |
| 60 | } |