~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/math/FortranFormat.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Revision: 7715 $ $Author: egonw $ $Date: 2007-01-13 20:37:41 +0100 (Sat, 13 Jan 2007) $
 
2
 *
 
3
 * Copyright (C) 1998-2007  Dan Gezelter
 
4
 *
 
5
 * Contact: cdk-devel@lists.sourceforge.net
 
6
 *
 
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.
 
15
 *
 
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.
 
20
 *
 
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.
 
24
 */
 
25
package org.openscience.cdk.math;
 
26
 
 
27
 
 
28
/**
 
29
 * Converts a String representation of a Fortran double to a double.
 
30
 *
 
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
 
35
 * barf.
 
36
 *
 
37
 * @author Dan Gezelter
 
38
 *
 
39
 * @cdk.module standard
 
40
 */
 
41
public class FortranFormat {
 
42
    /**
 
43
     * Converts a string of digits to an double
 
44
     *
 
45
     * @param s a string denoting a double
 
46
     */
 
47
    public static double atof(String s) {  
 
48
        int i = 0;
 
49
        int sign = 1;
 
50
        double r = 0; // integer part
 
51
        double p = 1; // exponent of fractional part
 
52
        int state = 0; // 0 = int part, 1 = frac part
 
53
        
 
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')
 
60
                {  if (state == 0)
 
61
                    r = r * 10 + ch - '0';
 
62
                else if (state == 1)
 
63
                    {  p = p / 10;
 
64
                    r = r + p * (ch - '0');
 
65
                    }
 
66
                }
 
67
            else if (ch == '.') 
 
68
                {  if (state == 0) state = 1; 
 
69
                else return sign * r;
 
70
                }
 
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);
 
74
                }
 
75
            else return sign * r;
 
76
            i++;
 
77
            }
 
78
        return sign * r;
 
79
    }
 
80
 
 
81
    private static long parseLong(String s, int base) {  
 
82
        int i = 0;
 
83
        int sign = 1;
 
84
        long r = 0;
 
85
        
 
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 ;
 
97
            else 
 
98
                return r * sign;
 
99
            i++;
 
100
            }
 
101
        return r * sign;      
 
102
    }
 
103
    
 
104
}