1
/* $Revision: 7715 $ $Author: egonw $ $Date: 2007-01-13 20:37:41 +0100 (Sat, 13 Jan 2007) $
3
* Copyright (C) 1998-2007 Dan Gezelter
5
* Contact: cdk-devel@lists.sourceforge.net
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public License
9
* as published by the Free Software Foundation; either version 2.1
10
* of the License, or (at your option) any later version.
11
* All we ask is that proper credit is given for our work, which includes
12
* - but is not limited to - adding the above copyright notice to the beginning
13
* of your source code files, and to any copyright notice that you may distribute
14
* with programs based on this work.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25
package org.openscience.cdk.math;
29
* Converts a String representation of a Fortran double to a double.
31
* <p>A modified version of the atof method provided in the Core Java
32
* books by Cay S. Horstmann & Gary Cornell. The main difference
33
* here is that we scan for fortran double precision characters
34
* ('D' and 'd') which often cause the C versions of atof to
37
* @author Dan Gezelter
39
* @cdk.module standard
41
public class FortranFormat {
43
* Converts a string of digits to an double
45
* @param s a string denoting a double
47
public static double atof(String s) {
50
double r = 0; // integer part
51
double p = 1; // exponent of fractional part
52
int state = 0; // 0 = int part, 1 = frac part
54
while (i < s.length() && Character.isWhitespace(s.charAt(i))) i++;
55
if (i < s.length() && s.charAt(i) == '-') { sign = -1; i++; }
56
else if (i < s.length() && s.charAt(i) == '+') { i++; }
57
while (i < s.length())
58
{ char ch = s.charAt(i);
59
if ('0' <= ch && ch <= '9')
61
r = r * 10 + ch - '0';
64
r = r + p * (ch - '0');
68
{ if (state == 0) state = 1;
71
else if (ch == 'e' || ch == 'E' || ch == 'd' || ch == 'D')
72
{ long e = (int)parseLong(s.substring(i + 1), 10);
73
return sign * r * Math.pow(10, e);
81
private static long parseLong(String s, int base) {
86
while (i < s.length() && Character.isWhitespace(s.charAt(i))) i++;
87
if (i < s.length() && s.charAt(i) == '-') { sign = -1; i++; }
88
else if (i < s.length() && s.charAt(i) == '+') { i++; }
89
while (i < s.length())
90
{ char ch = s.charAt(i);
91
if ('0' <= ch && ch < '0' + base)
92
r = r * base + ch - '0';
93
else if ('A' <= ch && ch < 'A' + base - 10)
94
r = r * base + ch - 'A' + 10 ;
95
else if ('a' <= ch && ch < 'a' + base - 10)
96
r = r * base + ch - 'a' + 10 ;