Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF 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 | |
Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 30 | #include "config.h" |
Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 31 | |
| 32 | #include <ctype.h> |
| 33 | |
| 34 | double atof(s) |
| 35 | char *s; |
| 36 | { |
| 37 | double a = 0.0; |
| 38 | int e = 0; |
| 39 | int c; |
| 40 | while ((c = *s++) != '\0' && isdigit(c)) { |
| 41 | a = a*10.0 + (c - '0'); |
| 42 | } |
| 43 | if (c == '.') { |
| 44 | while ((c = *s++) != '\0' && isdigit(c)) { |
| 45 | a = a*10.0 + (c - '0'); |
| 46 | e = e-1; |
| 47 | } |
| 48 | } |
| 49 | if (c == 'e' || c == 'E') { |
| 50 | int sign = 1; |
| 51 | int i = 0; |
| 52 | c = *s++; |
| 53 | if (c == '+') |
| 54 | c = *s++; |
| 55 | else if (c == '-') { |
| 56 | c = *s++; |
| 57 | sign = -1; |
| 58 | } |
| 59 | while (isdigit(c)) { |
| 60 | i = i*10 + (c - '0'); |
| 61 | c = *s++; |
| 62 | } |
| 63 | e += i*sign; |
| 64 | } |
| 65 | while (e > 0) { |
| 66 | a *= 10.0; |
| 67 | e--; |
| 68 | } |
| 69 | while (e < 0) { |
| 70 | a *= 0.1; |
| 71 | e++; |
| 72 | } |
| 73 | return a; |
| 74 | } |