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 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 7 | Copyright (c) 2000, BeOpen.com. |
| 8 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 9 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 10 | All rights reserved. |
Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 12 | See the file "Misc/COPYRIGHT" for information on usage and |
| 13 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 14 | |
| 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 Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 22 | #include "config.h" |
Guido van Rossum | 2b7e04a | 1995-02-19 15:54:36 +0000 | [diff] [blame] | 23 | |
| 24 | #include <ctype.h> |
| 25 | |
| 26 | double 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 | } |