public static double parseDouble()

in util/src/main/java/com/epam/deltix/util/text/CharSequenceParser.java [194:312]


    public static double  parseDouble (final CharSequence sc, final int startIncl, final int endExcl) {
        if (startIncl > endExcl)
            throw new IllegalArgumentException ("Illegal range: " + startIncl + ".." + endExcl);
        
        if (startIncl == endExcl)
            throw new NumberFormatException ("Empty string");
        
        int                 pos = startIncl;
        long                numerator = 0;        
        double              denominator = 1;
        long                sign = 0;
        boolean             dotSeen = false;
        boolean             overflow = false;
        char                ch = sc.charAt (pos);
        
        if (ch == '+' || ch == '-') {
            if (ch == '-')
                sign = LONG_SIGN_BIT;
            
            pos++;
            
            checkNotAtEnd (pos, endExcl, sc, startIncl);
            
            ch = sc.charAt (pos);
        }
        
        for (;;) {
            if (ch != ',') {
                if (!dotSeen && ch == '.')
                    dotSeen = true;
                else if (ch == 'e' || ch == 'E') {
                    pos++;
                    
                    checkNotAtEnd (pos, endExcl, sc, startIncl);
                    
                    ch = sc.charAt (pos);
                    
                    boolean     negativeExp = false;
                        
                    if (ch == '-') {
                        pos++;
                                                
                        negativeExp = true;
                    }
                    else if (ch == '+')
                        pos++;
                    
                    checkNotAtEnd (pos, endExcl, sc, startIncl);
                        
                    int     exp = parseInt (sc, pos, endExcl);
                    
                    for (int ii = 0; ii < exp; ii++)
                        if (negativeExp)
                            denominator *= 10;
                        else
                            denominator /= 10;
                }
                else {
                    final int       digit = ch - '0';

                    if (digit < 0 || digit > 9) {
                        if (Util.equals (sc, "NaN"))
                            return (Double.NaN);

                        throw new NumberFormatException (
                            "Illegal digit at position " + (pos + 1) + " in: " + sc.subSequence (startIncl, endExcl).toString ());
                    }

                    if (overflow) {
                        //  Stop shifting the numerator
                        if (!dotSeen)
                            denominator *= 0.1;
                    }
                    else {
                        numerator = numerator * 10 + digit;

                        if (dotSeen)
                            denominator *= 10;

                        if (numerator >= DOUBLE_ASSUMED_BIT)
                            overflow = true;
                    }
                }
            }
            pos++;
            
            if (pos == endExcl)
                break;
            
            ch = sc.charAt (pos);
        }   
        
        if (numerator == 0)
            return (0.0);                
        
        // Build the double first, ignoring the denominator
        long    exp = DOUBLE_NORM_EXP;        
        
        if (overflow) 
            while ((numerator & DOUBLE_OVERFLOW_BITMASK) != 0) {
                exp++;
                numerator >>>= 1;
            }  
        else
            while ((numerator & DOUBLE_ASSUMED_BIT) == 0) {
                exp--;
                numerator <<= 1;
            }                  
        
        numerator &= DOUBLE_MANTISSA_BITMASK;
        
        final long      bits = sign | (exp << DOUBLE_MANTISSA_WIDTH) | numerator;
        double          result = Double.longBitsToDouble (bits);
            
        if (denominator != 1)
            result /= denominator;
        
        return (result);
    }