jdk/src/share/classes/sun/misc/FloatingDecimal.java
author alanb
Fri, 22 Feb 2013 14:04:06 +0000
changeset 16023 58ecc1b8327b
parent 15255 0b53a05e0b07
child 18143 b6ef7bd945ce
permissions -rw-r--r--
8008290: (profiles) Startup regression due to additional checking of JAR file manifests Reviewed-by: lancea, chegar, iris, mchung, sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.misc.DoubleConsts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.misc.FloatConsts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
9521
e18f95eadb3c 7039369: Limit range of strictfp in FloatingDecimal
darcy
parents: 9276
diff changeset
    32
public class FloatingDecimal{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    boolean     isExceptional;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    boolean     isNegative;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    int         decExponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    char        digits[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    int         nDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    int         bigIntExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    int         bigIntNBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    boolean     mustSetRoundDir = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    boolean     fromHex = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    int         roundDir = 0; // set by doubleValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    44
    /*
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    45
     * The fields below provides additional information about the result of
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    46
     * the binary to decimal digits conversion done in dtoa() and roundup()
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    47
     * methods. They are changed if needed by those two methods.
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    48
     */
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    49
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    50
    // True if the dtoa() binary to decimal conversion was exact.
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    51
    boolean     exactDecimalConversion = false;
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    52
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    53
    // True if the result of the binary to decimal conversion was rounded-up
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    54
    // at the end of the conversion process, i.e. roundUp() method was called.
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    55
    boolean     decimalDigitsRoundedUp = false;
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    56
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private     FloatingDecimal( boolean negSign, int decExponent, char []digits, int n,  boolean e )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        isNegative = negSign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        isExceptional = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.decExponent = decExponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.digits = digits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.nDigits = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Constants of the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Most are IEEE-754 related.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * (There are more really boring constants at the end.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static final long   signMask = 0x8000000000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static final long   expMask  = 0x7ff0000000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static final long   fractMask= ~(signMask|expMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final int    expShift = 52;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static final int    expBias  = 1023;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static final long   fractHOB = ( 1L<<expShift ); // assumed High-Order bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static final long   expOne   = ((long)expBias)<<expShift; // exponent of 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static final int    maxSmallBinExp = 62;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static final int    minSmallBinExp = -( 63 / 3 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static final int    maxDecimalDigits = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final int    maxDecimalExponent = 308;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final int    minDecimalExponent = -324;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static final int    bigDecimalExponent = 324; // i.e. abs(minDecimalExponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static final long   highbyte = 0xff00000000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static final long   highbit  = 0x8000000000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static final long   lowbytes = ~highbyte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static final int    singleSignMask =    0x80000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final int    singleExpMask  =    0x7f800000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static final int    singleFractMask =   ~(singleSignMask|singleExpMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static final int    singleExpShift  =   23;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static final int    singleFractHOB  =   1<<singleExpShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static final int    singleExpBias   =   127;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static final int    singleMaxDecimalDigits = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static final int    singleMaxDecimalExponent = 38;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static final int    singleMinDecimalExponent = -45;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static final int    intDecimalDigits = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * count number of bits from high-order 1 bit to low-order 1 bit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    countBits( long v ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // the strategy is to shift until we get a non-zero sign bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // then shift until we have no bits left, counting the difference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // we do byte shifting as a hack. Hope it helps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if ( v == 0L ) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        while ( ( v & highbyte ) == 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            v <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        while ( v > 0L ) { // i.e. while ((v&highbit) == 0L )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            v <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while (( v & lowbytes ) != 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            v <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            n += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        while ( v != 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            v <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            n += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Keep big powers of 5 handy for future reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static FDBigInt b5p[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static synchronized FDBigInt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    big5pow( int p ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        assert p >= 0 : p; // negative power of 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if ( b5p == null ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            b5p = new FDBigInt[ p+1 ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }else if (b5p.length <= p ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            FDBigInt t[] = new FDBigInt[ p+1 ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.arraycopy( b5p, 0, t, 0, b5p.length );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            b5p = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if ( b5p[p] != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return b5p[p];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        else if ( p < small5pow.length )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return b5p[p] = new FDBigInt( small5pow[p] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        else if ( p < long5pow.length )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return b5p[p] = new FDBigInt( long5pow[p] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            // construct the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            // recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            int q, r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // in order to compute 5^p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // compute its square root, 5^(p/2) and square.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // or, let q = p / 2, r = p -q, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            // 5^p = 5^(q+r) = 5^q * 5^r
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            q = p >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            r = p - q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            FDBigInt bigq =  b5p[q];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if ( bigq == null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                bigq = big5pow ( q );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if ( r < small5pow.length ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                return (b5p[p] = bigq.mult( small5pow[r] ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                FDBigInt bigr = b5p[ r ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if ( bigr == null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    bigr = big5pow( r );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                return (b5p[p] = bigq.mult( bigr ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // a common operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static FDBigInt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    multPow52( FDBigInt v, int p5, int p2 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if ( p5 != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if ( p5 < small5pow.length ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                v = v.mult( small5pow[p5] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                v = v.mult( big5pow( p5 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if ( p2 != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            v.lshiftMe( p2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    // another common operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private static FDBigInt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    constructPow52( int p5, int p2 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        FDBigInt v = new FDBigInt( big5pow( p5 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if ( p2 != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            v.lshiftMe( p2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Make a floating double into a FDBigInt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * This could also be structured as a FDBigInt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * constructor, but we'd have to build a lot of knowledge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * about floating-point representation into it, and we don't want to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * AS A SIDE EFFECT, THIS METHOD WILL SET THE INSTANCE VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * bigIntExp and bigIntNBits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private FDBigInt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    doubleToBigInt( double dval ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        long lbits = Double.doubleToLongBits( dval ) & ~signMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int binexp = (int)(lbits >>> expShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        lbits &= fractMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if ( binexp > 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            lbits |= fractHOB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            assert lbits != 0L : lbits; // doubleToBigInt(0.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            binexp +=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            while ( (lbits & fractHOB ) == 0L){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                lbits <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                binexp -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        binexp -= expBias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int nbits = countBits( lbits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * We now know where the high-order 1 bit is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * and we know how many there are.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int lowOrderZeros = expShift+1-nbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        lbits >>>= lowOrderZeros;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        bigIntExp = binexp+1-nbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        bigIntNBits = nbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return new FDBigInt( lbits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Compute a number that is the ULP of the given value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * for purposes of addition/subtraction. Generally easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * More difficult if subtracting and the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * is a normalized a power of 2, as the ULP changes at these points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
9521
e18f95eadb3c 7039369: Limit range of strictfp in FloatingDecimal
darcy
parents: 9276
diff changeset
   254
    private static double ulp( double dval, boolean subtracting ){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        long lbits = Double.doubleToLongBits( dval ) & ~signMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int binexp = (int)(lbits >>> expShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        double ulpval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if ( subtracting && ( binexp >= expShift ) && ((lbits&fractMask) == 0L) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // for subtraction from normalized, powers of 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // use next-smaller exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            binexp -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if ( binexp > expShift ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            ulpval = Double.longBitsToDouble( ((long)(binexp-expShift))<<expShift );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else if ( binexp == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            ulpval = Double.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            ulpval = Double.longBitsToDouble( 1L<<(binexp-1) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if ( subtracting ) ulpval = - ulpval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return ulpval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Round a double to a float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * In addition to the fraction bits of the double,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * look at the class instance variable roundDir,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * which should help us avoid double-rounding error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * roundDir was set in hardValueOf if the estimate was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * close enough, but not exact. It tells us which direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * of rounding is preferred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    stickyRound( double dval ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        long lbits = Double.doubleToLongBits( dval );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        long binexp = lbits & expMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if ( binexp == 0L || binexp == expMask ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // what we have here is special.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // don't worry, the right thing will happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return (float) dval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        lbits += (long)roundDir; // hack-o-matic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return (float)Double.longBitsToDouble( lbits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * This is the easy subcase --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * all the significant bits, after scaling, are held in lvalue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * negSign and decExponent tell us what processing and scaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * has already been done. Exceptional cases have already been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * stripped out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * In particular:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * lvalue is a finite number (not Inf, nor NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * lvalue > 0L (not zero, nor negative).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * The only reason that we develop the digits here, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * calling on Long.toString() is that we can do it a little faster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * and besides want to treat trailing 0s specially. If Long.toString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * changes, we should re-evaluate this strategy!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    developLongDigits( int decExponent, long lvalue, long insignificant ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        char digits[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        int  ndigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        int  digitno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        int  c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // Discard non-significant low-order bits, while rounding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // up to insignificant value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        for ( i = 0; insignificant >= 10L; i++ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            insignificant /= 10L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if ( i != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            long pow10 = long5pow[i] << i; // 10^i == 5^i * 2^i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            long residue = lvalue % pow10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            lvalue /= pow10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            decExponent += i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if ( residue >= (pow10>>1) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // round up based on the low-order bits we're discarding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                lvalue++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if ( lvalue <= Integer.MAX_VALUE ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            assert lvalue > 0L : lvalue; // lvalue <= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // even easier subcase!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // can do int arithmetic rather than long!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            int  ivalue = (int)lvalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            ndigits = 10;
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
   341
            digits = perThreadBuffer.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            digitno = ndigits-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            c = ivalue%10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            ivalue /= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            while ( c == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                decExponent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                c = ivalue%10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                ivalue /= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            while ( ivalue != 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                digits[digitno--] = (char)(c+'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                decExponent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                c = ivalue%10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                ivalue /= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            digits[digitno] = (char)(c+'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // same algorithm as above (same bugs, too )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // but using long arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            ndigits = 20;
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
   361
            digits = perThreadBuffer.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            digitno = ndigits-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            c = (int)(lvalue%10L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            lvalue /= 10L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            while ( c == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                decExponent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                c = (int)(lvalue%10L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                lvalue /= 10L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            while ( lvalue != 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                digits[digitno--] = (char)(c+'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                decExponent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                c = (int)(lvalue%10L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                lvalue /= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            digits[digitno] = (char)(c+'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        char result [];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        ndigits -= digitno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        result = new char[ ndigits ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        System.arraycopy( digits, digitno, result, 0, ndigits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        this.digits = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        this.decExponent = decExponent+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        this.nDigits = ndigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    // add one to the least significant digit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    // in the unlikely event there is a carry out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    // deal with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    // assert that this will only happen where there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    // is only one digit, e.g. (float)1e-44 seems to do it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    roundup(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        int q = digits[ i = (nDigits-1)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if ( q == '9' ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            while ( q == '9' && i > 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                digits[i] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                q = digits[--i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if ( q == '9' ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                // carryout! High-order 1, rest 0s, larger exp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                decExponent += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                digits[0] = '1';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            // else fall through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        digits[i] = (char)(q+1);
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   412
        decimalDigitsRoundedUp = true;
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   413
    }
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   414
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   415
    public boolean digitsRoundedUp() {
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   416
        return decimalDigitsRoundedUp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * FIRST IMPORTANT CONSTRUCTOR: DOUBLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public FloatingDecimal( double d )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        long    dBits = Double.doubleToLongBits( d );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        long    fractBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        int     binExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        int     nSignificantBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // discover and delete sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if ( (dBits&signMask) != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            isNegative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            dBits ^= signMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            isNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        // Begin to unpack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // Discover obvious special cases of NaN and Infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        binExp = (int)( (dBits&expMask) >> expShift );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        fractBits = dBits&fractMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if ( binExp == (int)(expMask>>expShift) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            isExceptional = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            if ( fractBits == 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                digits =  infinity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                digits = notANumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                isNegative = false; // NaN has no sign!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            nDigits = digits.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        isExceptional = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // Finish unpacking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // Normalize denormalized numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // Insert assumed high-order bit for normalized numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // Subtract exponent bias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if ( binExp == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if ( fractBits == 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                // not a denorm, just a 0!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                decExponent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                digits = zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                nDigits = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            while ( (fractBits&fractHOB) == 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                fractBits <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                binExp -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            nSignificantBits = expShift + binExp +1; // recall binExp is  - shift count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            binExp += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            fractBits |= fractHOB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            nSignificantBits = expShift+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        binExp -= expBias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        // call the routine that actually does all the hard work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        dtoa( binExp, fractBits, nSignificantBits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * SECOND IMPORTANT CONSTRUCTOR: SINGLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public FloatingDecimal( float f )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        int     fBits = Float.floatToIntBits( f );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        int     fractBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        int     binExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        int     nSignificantBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // discover and delete sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if ( (fBits&singleSignMask) != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            isNegative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            fBits ^= singleSignMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            isNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // Begin to unpack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        // Discover obvious special cases of NaN and Infinity.
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
   498
        binExp = (fBits&singleExpMask) >> singleExpShift;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        fractBits = fBits&singleFractMask;
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
   500
        if ( binExp == (singleExpMask>>singleExpShift) ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            isExceptional = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            if ( fractBits == 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                digits =  infinity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                digits = notANumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                isNegative = false; // NaN has no sign!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            nDigits = digits.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        isExceptional = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // Finish unpacking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // Normalize denormalized numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        // Insert assumed high-order bit for normalized numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        // Subtract exponent bias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if ( binExp == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if ( fractBits == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                // not a denorm, just a 0!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                decExponent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                digits = zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                nDigits = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            while ( (fractBits&singleFractHOB) == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                fractBits <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                binExp -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            nSignificantBits = singleExpShift + binExp +1; // recall binExp is  - shift count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            binExp += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            fractBits |= singleFractHOB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            nSignificantBits = singleExpShift+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        binExp -= singleExpBias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        // call the routine that actually does all the hard work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        dtoa( binExp, ((long)fractBits)<<(expShift-singleExpShift), nSignificantBits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    dtoa( int binExp, long fractBits, int nSignificantBits )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        int     nFractBits; // number of significant bits of fractBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        int     nTinyBits;  // number of these to the right of the point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        int     decExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // Examine number. Determine if it is an easy case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // which we can do pretty trivially using float/long conversion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        // or whether we must do real work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        nFractBits = countBits( fractBits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        nTinyBits = Math.max( 0, nFractBits - binExp - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if ( binExp <= maxSmallBinExp && binExp >= minSmallBinExp ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            // Look more closely at the number to decide if,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            // with scaling by 10^nTinyBits, the result will fit in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            // a long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if ( (nTinyBits < long5pow.length) && ((nFractBits + n5bits[nTinyBits]) < 64 ) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                 * We can do this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                 * take the fraction bits, which are normalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                 * (a) nTinyBits == 0: Shift left or right appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                 *     to align the binary point at the extreme right, i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                 *     where a long int point is expected to be. The integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                 *     result is easily converted to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                 * (b) nTinyBits > 0: Shift right by expShift-nFractBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                 *     which effectively converts to long and scales by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                 *     2^nTinyBits. Then multiply by 5^nTinyBits to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                 *     complete the scaling. We know this won't overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                 *     because we just counted the number of bits necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                 *     in the result. The integer you get from this can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                 *     then be converted to a string pretty easily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                long halfULP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                if ( nTinyBits == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    if ( binExp > nSignificantBits ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        halfULP = 1L << ( binExp-nSignificantBits-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        halfULP = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    if ( binExp >= expShift ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                        fractBits <<= (binExp-expShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        fractBits >>>= (expShift-binExp) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    developLongDigits( 0, fractBits, halfULP );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                 * The following causes excess digits to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                 * out in the single-float case. Our manipulation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                 * halfULP here is apparently not correct. If we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                 * better understand how this works, perhaps we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                 * use this special case again. But for the time being,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                 * we do not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                 * else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                 *     fractBits >>>= expShift+1-nFractBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                 *     fractBits *= long5pow[ nTinyBits ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                 *     halfULP = long5pow[ nTinyBits ] >> (1+nSignificantBits-nFractBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                 *     developLongDigits( -nTinyBits, fractBits, halfULP );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                 *     return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         * This is the hard case. We are going to compute large positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * integers B and S and integer decExp, s.t.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         *      d = ( B / S ) * 10^decExp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         *      1 <= B / S < 10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * Obvious choices are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         *      decExp = floor( log10(d) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         *      B      = d * 2^nTinyBits * 10^max( 0, -decExp )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         *      S      = 10^max( 0, decExp) * 2^nTinyBits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         * (noting that nTinyBits has already been forced to non-negative)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         * I am also going to compute a large positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         *      M      = (1/2^nSignificantBits) * 2^nTinyBits * 10^max( 0, -decExp )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * i.e. M is (1/2) of the ULP of d, scaled like B.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * When we iterate through dividing B/S and picking off the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         * quotient bits, we will know when to stop when the remainder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         * is <= M.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         * We keep track of powers of 2 and powers of 5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
         * Estimate decimal exponent. (If it is small-ish,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         * we could double-check.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * First, scale the mantissa bits such that 1 <= d2 < 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * We are then going to estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         *          log10(d2) ~=~  (d2-1.5)/1.5 + log(1.5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * and so we can estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         *      log10(d) ~=~ log10(d2) + binExp * log10(2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * take the floor and call it decExp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * FIXME -- use more precise constants here. It costs no more.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        double d2 = Double.longBitsToDouble(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            expOne | ( fractBits &~ fractHOB ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        decExp = (int)Math.floor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            (d2-1.5D)*0.289529654D + 0.176091259 + (double)binExp * 0.301029995663981 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        int B2, B5; // powers of 2 and powers of 5, respectively, in B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        int S2, S5; // powers of 2 and powers of 5, respectively, in S
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        int M2, M5; // powers of 2 and powers of 5, respectively, in M
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        int Bbits; // binary digits needed to represent B, approx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        int tenSbits; // binary digits needed to represent 10*S, approx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        FDBigInt Sval, Bval, Mval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        B5 = Math.max( 0, -decExp );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        B2 = B5 + nTinyBits + binExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        S5 = Math.max( 0, decExp );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        S2 = S5 + nTinyBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        M5 = B5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        M2 = B2 - nSignificantBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         * the long integer fractBits contains the (nFractBits) interesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * bits from the mantissa of d ( hidden 1 added if necessary) followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * by (expShift+1-nFractBits) zeros. In the interest of compactness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         * I will shift out those zeros before turning fractBits into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         * FDBigInt. The resulting whole number will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         *      d * 2^(nFractBits-1-binExp).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        fractBits >>>= (expShift+1-nFractBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        B2 -= nFractBits-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int common2factor = Math.min( B2, S2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        B2 -= common2factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        S2 -= common2factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        M2 -= common2factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         * HACK!! For exact powers of two, the next smallest number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * is only half as far away as we think (because the meaning of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * ULP changes at power-of-two bounds) for this reason, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * hack M2. Hope this works.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if ( nFractBits == 1 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            M2 -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if ( M2 < 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            // oops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            // since we cannot scale M down far enough,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            // we must scale the other values up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            B2 -= M2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            S2 -= M2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            M2 =  0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
         * Construct, Scale, iterate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
         * Some day, we'll write a stopping test that takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * account of the asymmetry of the spacing of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         * numbers below perfect powers of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * 26 Sept 96 is not that day.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         * So we use a symmetric test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        char digits[] = this.digits = new char[18];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        int  ndigit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        boolean low, high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        long lowDigitDifference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        int  q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         * Detect the special cases where all the numbers we are about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         * to compute will fit in int or long integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         * In these cases, we will avoid doing FDBigInt arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         * We use the same algorithms, except that we "normalize"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         * our FDBigInts before iterating. This is to make division easier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         * as it makes our fist guess (quotient of high-order words)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         * more accurate!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         * Some day, we'll write a stopping test that takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         * account of the asymmetry of the spacing of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         * numbers below perfect powers of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
         * 26 Sept 96 is not that day.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
         * So we use a symmetric test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        Bbits = nFractBits + B2 + (( B5 < n5bits.length )? n5bits[B5] : ( B5*3 ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        tenSbits = S2+1 + (( (S5+1) < n5bits.length )? n5bits[(S5+1)] : ( (S5+1)*3 ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if ( Bbits < 64 && tenSbits < 64){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            if ( Bbits < 32 && tenSbits < 32){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                // wa-hoo! They're all ints!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                int b = ((int)fractBits * small5pow[B5] ) << B2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                int s = small5pow[S5] << S2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                int m = small5pow[M5] << M2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                int tens = s * 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                 * Unroll the first iteration. If our decExp estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                 * was too high, our first quotient will be zero. In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                 * case, we discard it and decrement decExp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                ndigit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                q = b / s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                b = 10 * ( b % s );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                m *= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                low  = (b <  m );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                high = (b+m > tens );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                assert q < 10 : q; // excessively large digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                if ( (q == 0) && ! high ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    // oops. Usually ignore leading zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    decExp--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    digits[ndigit++] = (char)('0' + q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                 * HACK! Java spec sez that we always have at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                 * one digit after the . in either F- or E-form output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                 * Thus we will need more than one digit if we're using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                 * E-form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                 */
4052
0464046ac1f9 4428022: System.out.println(0.001) outputs 0.0010
darcy
parents: 2170
diff changeset
   749
                if ( decExp < -3 || decExp >= 8 ){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    high = low = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                while( ! low && ! high ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    q = b / s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    b = 10 * ( b % s );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                    m *= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    assert q < 10 : q; // excessively large digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    if ( m > 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                        low  = (b <  m );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                        high = (b+m > tens );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                        // hack -- m might overflow!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                        // in this case, it is certainly > b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                        // which won't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                        // and b+m > tens, too, since that has overflowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                        // either!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                        low = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                        high = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    digits[ndigit++] = (char)('0' + q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                lowDigitDifference = (b<<1) - tens;
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   772
                exactDecimalConversion  = (b == 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                // still good! they're all longs!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                long b = (fractBits * long5pow[B5] ) << B2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                long s = long5pow[S5] << S2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                long m = long5pow[M5] << M2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                long tens = s * 10L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                 * Unroll the first iteration. If our decExp estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                 * was too high, our first quotient will be zero. In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                 * case, we discard it and decrement decExp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                ndigit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                q = (int) ( b / s );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                b = 10L * ( b % s );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                m *= 10L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                low  = (b <  m );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                high = (b+m > tens );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                assert q < 10 : q; // excessively large digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                if ( (q == 0) && ! high ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    // oops. Usually ignore leading zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    decExp--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    digits[ndigit++] = (char)('0' + q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                 * HACK! Java spec sez that we always have at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                 * one digit after the . in either F- or E-form output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                 * Thus we will need more than one digit if we're using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                 * E-form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                 */
4052
0464046ac1f9 4428022: System.out.println(0.001) outputs 0.0010
darcy
parents: 2170
diff changeset
   803
                if ( decExp < -3 || decExp >= 8 ){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    high = low = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                while( ! low && ! high ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    q = (int) ( b / s );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    b = 10 * ( b % s );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    m *= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    assert q < 10 : q;  // excessively large digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    if ( m > 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                        low  = (b <  m );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        high = (b+m > tens );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                        // hack -- m might overflow!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        // in this case, it is certainly > b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        // which won't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        // and b+m > tens, too, since that has overflowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        // either!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                        low = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                        high = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    digits[ndigit++] = (char)('0' + q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                lowDigitDifference = (b<<1) - tens;
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   826
                exactDecimalConversion  = (b == 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        } else {
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   829
            FDBigInt ZeroVal = new FDBigInt(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            FDBigInt tenSval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            int  shiftBias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
             * We really must do FDBigInt arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
             * Fist, construct our FDBigInt initial values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            Bval = multPow52( new FDBigInt( fractBits  ), B5, B2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            Sval = constructPow52( S5, S2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            Mval = constructPow52( M5, M2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            // normalize so that division works better
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            Bval.lshiftMe( shiftBias = Sval.normalizeMe() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            Mval.lshiftMe( shiftBias );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            tenSval = Sval.mult( 10 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
             * Unroll the first iteration. If our decExp estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
             * was too high, our first quotient will be zero. In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
             * case, we discard it and decrement decExp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            ndigit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            q = Bval.quoRemIteration( Sval );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            Mval = Mval.mult( 10 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            low  = (Bval.cmp( Mval ) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            high = (Bval.add( Mval ).cmp( tenSval ) > 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            assert q < 10 : q; // excessively large digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            if ( (q == 0) && ! high ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                // oops. Usually ignore leading zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                decExp--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                digits[ndigit++] = (char)('0' + q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
             * HACK! Java spec sez that we always have at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
             * one digit after the . in either F- or E-form output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
             * Thus we will need more than one digit if we're using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
             * E-form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
             */
4052
0464046ac1f9 4428022: System.out.println(0.001) outputs 0.0010
darcy
parents: 2170
diff changeset
   869
            if ( decExp < -3 || decExp >= 8 ){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                high = low = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            while( ! low && ! high ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                q = Bval.quoRemIteration( Sval );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                Mval = Mval.mult( 10 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                assert q < 10 : q;  // excessively large digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                low  = (Bval.cmp( Mval ) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                high = (Bval.add( Mval ).cmp( tenSval ) > 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                digits[ndigit++] = (char)('0' + q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            if ( high && low ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                Bval.lshiftMe(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                lowDigitDifference = Bval.cmp(tenSval);
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   883
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                lowDigitDifference = 0L; // this here only for flow analysis!
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   885
            }
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   886
            exactDecimalConversion  = (Bval.cmp( ZeroVal ) == 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        this.decExponent = decExp+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        this.digits = digits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        this.nDigits = ndigit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * Last digit gets rounded based on stopping condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if ( high ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if ( low ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                if ( lowDigitDifference == 0L ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    // it's a tie!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    // choose based on which digits we like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    if ( (digits[nDigits-1]&1) != 0 ) roundup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                } else if ( lowDigitDifference > 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    roundup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                roundup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   909
    public boolean decimalDigitsExact() {
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   910
        return exactDecimalConversion;
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   911
    }
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   912
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    toString(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        // most brain-dead version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        StringBuffer result = new StringBuffer( nDigits+8 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        if ( isNegative ){ result.append( '-' ); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        if ( isExceptional ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            result.append( digits, 0, nDigits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            result.append( "0.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            result.append( digits, 0, nDigits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            result.append('e');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            result.append( decExponent );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        return new String(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    public String toJavaFormatString() {
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
   930
        char result[] = perThreadBuffer.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        int i = getChars(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return new String(result, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    private int getChars(char[] result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        assert nDigits <= 19 : nDigits; // generous bound on size of nDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        if (isNegative) { result[0] = '-'; i = 1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        if (isExceptional) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            System.arraycopy(digits, 0, result, i, nDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            i += nDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (decExponent > 0 && decExponent < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                // print digits.digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                int charLength = Math.min(nDigits, decExponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                System.arraycopy(digits, 0, result, i, charLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                i += charLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                if (charLength < decExponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    charLength = decExponent-charLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                    System.arraycopy(zero, 0, result, i, charLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    i += charLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    result[i++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    result[i++] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    result[i++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    if (charLength < nDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                        int t = nDigits - charLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                        System.arraycopy(digits, charLength, result, i, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                        i += t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                        result[i++] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            } else if (decExponent <=0 && decExponent > -3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                result[i++] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                result[i++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                if (decExponent != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    System.arraycopy(zero, 0, result, i, -decExponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    i -= decExponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                System.arraycopy(digits, 0, result, i, nDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                i += nDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                result[i++] = digits[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                result[i++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                if (nDigits > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    System.arraycopy(digits, 1, result, i, nDigits-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                    i += nDigits-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    result[i++] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                result[i++] = 'E';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                int e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                if (decExponent <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                    result[i++] = '-';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    e = -decExponent+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                    e = decExponent-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                // decExponent has 1, 2, or 3, digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                if (e <= 9) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    result[i++] = (char)(e+'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                } else if (e <= 99) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    result[i++] = (char)(e/10 +'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    result[i++] = (char)(e%10 + '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    result[i++] = (char)(e/100+'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                    e %= 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    result[i++] = (char)(e/10+'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                    result[i++] = (char)(e%10 + '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    // Per-thread buffer for string/stringbuffer conversion
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
  1008
    private static ThreadLocal<char[]> perThreadBuffer = new ThreadLocal<char[]>() {
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
  1009
            protected synchronized char[] initialValue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                return new char[26];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    public void appendTo(Appendable buf) {
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
  1015
          char result[] = perThreadBuffer.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
          int i = getChars(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (buf instanceof StringBuilder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            ((StringBuilder) buf).append(result, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        else if (buf instanceof StringBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            ((StringBuffer) buf).append(result, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
  1025
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    public static FloatingDecimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    readJavaFormatString( String in ) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        boolean isNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        boolean signSeen   = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        int     decExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        char    c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    parseNumber:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            in = in.trim(); // don't fool around with white space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                            // throws NullPointerException if null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            int l = in.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            if ( l == 0 ) throw new NumberFormatException("empty String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            switch ( c = in.charAt( i ) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            case '-':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                isNegative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                //FALLTHROUGH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                signSeen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            // Check for NaN and Infinity strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            c = in.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            if(c == 'N' || c == 'I') { // possible NaN or infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                boolean potentialNaN = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                char targetChars[] = null;  // char array of "NaN" or "Infinity"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                if(c == 'N') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    targetChars = notANumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    potentialNaN = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    targetChars = infinity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                // compare Input string to "NaN" or "Infinity"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                while(i < l && j < targetChars.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                    if(in.charAt(i) == targetChars[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                        i++; j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                    else // something is amiss, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                        break parseNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                // For the candidate string to be a NaN or infinity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                // all characters in input string and target char[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                // must be matched ==> j must equal targetChars.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                // and i must equal l
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                if( (j == targetChars.length) && (i == l) ) { // return NaN or infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    return (potentialNaN ? new FloatingDecimal(Double.NaN) // NaN has no sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                            : new FloatingDecimal(isNegative?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                                                  Double.NEGATIVE_INFINITY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                                                  Double.POSITIVE_INFINITY)) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                else { // something went wrong, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                    break parseNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            } else if (c == '0')  { // check for hexadecimal floating-point number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                if (l > i+1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    char ch = in.charAt(i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    if (ch == 'x' || ch == 'X' ) // possible hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                        return parseHexString(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            }  // look for and process decimal floating-point string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            char[] digits = new char[ l ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            int    nDigits= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            boolean decSeen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            int decPt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            int nLeadZero = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            int nTrailZero= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        digitLoop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            while ( i < l ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                switch ( c = in.charAt( i ) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                case '0':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    if ( nDigits > 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                        nTrailZero += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                        nLeadZero += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    break; // out of switch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                case '1':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                case '2':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                case '3':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                case '4':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                case '5':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                case '6':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                case '7':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                case '8':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    while ( nTrailZero > 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                        digits[nDigits++] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                        nTrailZero -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                    digits[nDigits++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    break; // out of switch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                case '.':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    if ( decSeen ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                        // already saw one ., this is the 2nd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                        throw new NumberFormatException("multiple points");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                    decPt = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                    if ( signSeen ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                        decPt -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    decSeen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                    break; // out of switch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    break digitLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
             * At this point, we've scanned all the digits and decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
             * point we're going to see. Trim off leading and trailing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
             * zeros, which will just confuse us later, and adjust
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
             * our initial decimal exponent accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
             * To review:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
             * we have seen i total characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
             * nLeadZero of them were zeros before any other digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
             * nTrailZero of them were zeros after any other digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
             * if ( decSeen ), then a . was seen after decPt characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
             * ( including leading zeros which have been discarded )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
             * nDigits characters were neither lead nor trailing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
             * zeros, nor point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
             * special hack: if we saw no non-zero digits, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
             * answer is zero!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
             * Unfortunately, we feel honor-bound to keep parsing!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            if ( nDigits == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                digits = zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                nDigits = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                if ( nLeadZero == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    // we saw NO DIGITS AT ALL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                    // not even a crummy 0!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                    // this is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    break parseNumber; // go throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            /* Our initial exponent is decPt, adjusted by the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
             * discarded zeros. Or, if there was no decPt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
             * then its just nDigits adjusted by discarded trailing zeros.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            if ( decSeen ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                decExp = decPt - nLeadZero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                decExp = nDigits+nTrailZero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
             * Look for 'e' or 'E' and an optionally signed integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            if ( (i < l) &&  (((c = in.charAt(i) )=='e') || (c == 'E') ) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                int expSign = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                int expVal  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                int reallyBig = Integer.MAX_VALUE / 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                boolean expOverflow = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                switch( in.charAt(++i) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                case '-':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    expSign = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    //FALLTHROUGH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                int expAt = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            expLoop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                while ( i < l  ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    if ( expVal >= reallyBig ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                        // the next character will cause integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                        // overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                        expOverflow = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                    switch ( c = in.charAt(i++) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                    case '0':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    case '1':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                    case '2':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    case '3':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                    case '4':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    case '5':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    case '6':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                    case '7':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    case '8':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                        expVal = expVal*10 + ( (int)c - (int)'0' );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                        i--;           // back up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                        break expLoop; // stop parsing exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                int expLimit = bigDecimalExponent+nDigits+nTrailZero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                if ( expOverflow || ( expVal > expLimit ) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    // The intent here is to end up with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                    // infinity or zero, as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    // The reason for yielding such a small decExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    // rather than something intuitive such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    // expSign*Integer.MAX_VALUE, is that this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    // is subject to further manipulation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                    // doubleValue() and floatValue(), and I don't want
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    // it to be able to cause overflow there!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    // (The only way we can get into trouble here is for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                    // really outrageous nDigits+nTrailZero, such as 2 billion. )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                    decExp = expSign*expLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                    // this should not overflow, since we tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                    // for expVal > (MAX+N), where N >= abs(decExp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                    decExp = decExp + expSign*expVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                // if we saw something not a digit ( or end of string )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                // after the [Ee][+-], without seeing any digits at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                // this is certainly an error. If we saw some digits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                // but then some trailing garbage, that might be ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                // so we just fall through in that case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                // HUMBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                if ( i == expAt )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                    break parseNumber; // certainly bad
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
             * We parsed everything we could.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
             * If there are leftovers, then this is not good input!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            if ( i < l &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                ((i != l - 1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                (in.charAt(i) != 'f' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                 in.charAt(i) != 'F' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                 in.charAt(i) != 'd' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                 in.charAt(i) != 'D'))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                break parseNumber; // go throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            return new FloatingDecimal( isNegative, decExp, digits, nDigits,  false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        } catch ( StringIndexOutOfBoundsException e ){ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        throw new NumberFormatException("For input string: \"" + in + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * Take a FloatingDecimal, which we presumably just scanned in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * and find out what its value is, as a double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * AS A SIDE EFFECT, SET roundDir TO INDICATE PREFERRED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * ROUNDING DIRECTION in case the result is really destined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * for a single-precision float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
9521
e18f95eadb3c 7039369: Limit range of strictfp in FloatingDecimal
darcy
parents: 9276
diff changeset
  1280
    public strictfp double doubleValue(){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        int     kDigits = Math.min( nDigits, maxDecimalDigits+1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        long    lValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        double  dValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        double  rValue, tValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        // First, check for NaN and Infinity values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        if(digits == infinity || digits == notANumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            if(digits == notANumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                return Double.NaN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                return (isNegative?Double.NEGATIVE_INFINITY:Double.POSITIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            if (mustSetRoundDir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                roundDir = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
             * convert the lead kDigits to a long integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            // (special performance hack: start to do it using int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            int iValue = (int)digits[0]-(int)'0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            int iDigits = Math.min( kDigits, intDecimalDigits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            for ( int i=1; i < iDigits; i++ ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                iValue = iValue*10 + (int)digits[i]-(int)'0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            lValue = (long)iValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            for ( int i=iDigits; i < kDigits; i++ ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                lValue = lValue*10L + (long)((int)digits[i]-(int)'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            dValue = (double)lValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            int exp = decExponent-kDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
             * lValue now contains a long integer with the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
             * the first kDigits digits of the number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
             * dValue contains the (double) of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            if ( nDigits <= maxDecimalDigits ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                 * possibly an easy case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                 * We know that the digits can be represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                 * exactly. And if the exponent isn't too outrageous,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                 * the whole thing can be done with one operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                 * thus one rounding error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                 * Note that all our constructors trim all leading and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                 * trailing zeros, so simple values (including zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                 * will always end up here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                if (exp == 0 || dValue == 0.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    return (isNegative)? -dValue : dValue; // small floating integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                else if ( exp >= 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                    if ( exp <= maxSmallTen ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                         * Can get the answer with one operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                         * thus one roundoff.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                        rValue = dValue * small10pow[exp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                        if ( mustSetRoundDir ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                            tValue = rValue / small10pow[exp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                            roundDir = ( tValue ==  dValue ) ? 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                                :( tValue < dValue ) ? 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                                : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                        return (isNegative)? -rValue : rValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    int slop = maxDecimalDigits - kDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                    if ( exp <= maxSmallTen+slop ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                         * We can multiply dValue by 10^(slop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                         * and it is still "small" and exact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                         * Then we can multiply by 10^(exp-slop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                         * with one rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                        dValue *= small10pow[slop];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                        rValue = dValue * small10pow[exp-slop];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                        if ( mustSetRoundDir ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                            tValue = rValue / small10pow[exp-slop];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                            roundDir = ( tValue ==  dValue ) ? 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                                :( tValue < dValue ) ? 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                                : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                        return (isNegative)? -rValue : rValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                     * Else we have a hard case with a positive exp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                    if ( exp >= -maxSmallTen ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                         * Can get the answer in one division.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                        rValue = dValue / small10pow[-exp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                        tValue = rValue * small10pow[-exp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                        if ( mustSetRoundDir ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                            roundDir = ( tValue ==  dValue ) ? 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                                :( tValue < dValue ) ? 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                                : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                        return (isNegative)? -rValue : rValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                     * Else we have a hard case with a negative exp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
             * Harder cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
             * The sum of digits plus exponent is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
             * what we think we can do with one error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
             * Start by approximating the right answer by,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
             * naively, scaling by powers of 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            if ( exp > 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                if ( decExponent > maxDecimalExponent+1 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                     * Lets face it. This is going to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                     * Infinity. Cut to the chase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                    return (isNegative)? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                if ( (exp&15) != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    dValue *= small10pow[exp&15];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                if ( (exp>>=4) != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                    for( j = 0; exp > 1; j++, exp>>=1 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                        if ( (exp&1)!=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                            dValue *= big10pow[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                     * The reason for the weird exp > 1 condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                     * in the above loop was so that the last multiply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                     * would get unrolled. We handle it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                     * It could overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                    double t = dValue * big10pow[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                    if ( Double.isInfinite( t ) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                         * It did overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                         * Look more closely at the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                         * If the exponent is just one too large,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                         * then use the maximum finite as our estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                         * value. Else call the result infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                         * and punt it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                         * ( I presume this could happen because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                         * rounding forces the result here to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                         * an ULP or two larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                         * Double.MAX_VALUE ).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                        t = dValue / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                        t *= big10pow[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                        if ( Double.isInfinite( t ) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                            return (isNegative)? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                        t = Double.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                    dValue = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            } else if ( exp < 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                exp = -exp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                if ( decExponent < minDecimalExponent-1 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                     * Lets face it. This is going to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                     * zero. Cut to the chase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                    return (isNegative)? -0.0 : 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                if ( (exp&15) != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                    dValue /= small10pow[exp&15];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                if ( (exp>>=4) != 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                    for( j = 0; exp > 1; j++, exp>>=1 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                        if ( (exp&1)!=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                            dValue *= tiny10pow[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                     * The reason for the weird exp > 1 condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                     * in the above loop was so that the last multiply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                     * would get unrolled. We handle it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                     * It could underflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                    double t = dValue * tiny10pow[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    if ( t == 0.0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                         * It did underflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                         * Look more closely at the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                         * If the exponent is just one too small,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                         * then use the minimum finite as our estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                         * value. Else call the result 0.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                         * and punt it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                         * ( I presume this could happen because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                         * rounding forces the result here to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                         * an ULP or two less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                         * Double.MIN_VALUE ).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                        t = dValue * 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                        t *= tiny10pow[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                        if ( t == 0.0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                            return (isNegative)? -0.0 : 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                        t = Double.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                    dValue = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
             * dValue is now approximately the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
             * The hard part is adjusting it, by comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
             * with FDBigInt arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
             * Formulate the EXACT big-number result as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
             * bigD0 * 10^exp
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            FDBigInt bigD0 = new FDBigInt( lValue, digits, kDigits, nDigits );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            exp   = decExponent - nDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            correctionLoop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            while(true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                /* AS A SIDE EFFECT, THIS METHOD WILL SET THE INSTANCE VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                 * bigIntExp and bigIntNBits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                FDBigInt bigB = doubleToBigInt( dValue );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                 * Scale bigD, bigB appropriately for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                 * big-integer operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                 * Naively, we multiply by powers of ten
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                 * and powers of two. What we actually do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                 * is keep track of the powers of 5 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                 * powers of 2 we would use, then factor out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                 * common divisors before doing the work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                int B2, B5; // powers of 2, 5 in bigB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                int     D2, D5; // powers of 2, 5 in bigD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                int Ulp2;   // powers of 2 in halfUlp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                if ( exp >= 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                    B2 = B5 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                    D2 = D5 = exp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                    B2 = B5 = -exp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                    D2 = D5 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                if ( bigIntExp >= 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                    B2 += bigIntExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                    D2 -= bigIntExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                Ulp2 = B2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                // shift bigB and bigD left by a number s. t.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                // halfUlp is still an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                int hulpbias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                if ( bigIntExp+bigIntNBits <= -expBias+1 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    // This is going to be a denormalized number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    // (if not actually zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                    // half an ULP is at 2^-(expBias+expShift+1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    hulpbias = bigIntExp+ expBias + expShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                    hulpbias = expShift + 2 - bigIntNBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                B2 += hulpbias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                D2 += hulpbias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                // if there are common factors of 2, we might just as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                // factor them out, as they add nothing useful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                int common2 = Math.min( B2, Math.min( D2, Ulp2 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                B2 -= common2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                D2 -= common2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                Ulp2 -= common2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                // do multiplications by powers of 5 and 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                bigB = multPow52( bigB, B5, B2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                FDBigInt bigD = multPow52( new FDBigInt( bigD0 ), D5, D2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                // to recap:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                // bigB is the scaled-big-int version of our floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                // candidate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                // bigD is the scaled-big-int version of the exact value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                // as we understand it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                // halfUlp is 1/2 an ulp of bigB, except for special cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                // of exact powers of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                // the plan is to compare bigB with bigD, and if the difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                // is less than halfUlp, then we're satisfied. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                // use the ratio of difference to halfUlp to calculate a fudge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                // factor to add to the floating value, then go 'round again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                FDBigInt diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                int cmpResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                boolean overvalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                if ( (cmpResult = bigB.cmp( bigD ) ) > 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                    overvalue = true; // our candidate is too big.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                    diff = bigB.sub( bigD );
8189
5fd29d2cbc4b 4421494: infinite loop while parsing double literal
alanb
parents: 5506
diff changeset
  1575
                    if ( (bigIntNBits == 1) && (bigIntExp > -expBias+1) ){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                        // candidate is a normalized exact power of 2 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                        // is too big. We will be subtracting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                        // For our purposes, ulp is the ulp of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                        // next smaller range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                        Ulp2 -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                        if ( Ulp2 < 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                            // rats. Cannot de-scale ulp this far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                            // must scale diff in other direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                            Ulp2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                            diff.lshiftMe( 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                } else if ( cmpResult < 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                    overvalue = false; // our candidate is too small.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                    diff = bigD.sub( bigB );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                    // the candidate is exactly right!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                    // this happens with surprising frequency
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                    break correctionLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                FDBigInt halfUlp = constructPow52( B5, Ulp2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                if ( (cmpResult = diff.cmp( halfUlp ) ) < 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                    // difference is small.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                    // this is close enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                    if (mustSetRoundDir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                        roundDir = overvalue ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                    break correctionLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                } else if ( cmpResult == 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                    // difference is exactly half an ULP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                    // round to some other value maybe, then finish
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                    dValue += 0.5*ulp( dValue, overvalue );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                    // should check for bigIntNBits == 1 here??
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                    if (mustSetRoundDir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                        roundDir = overvalue ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                    break correctionLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                    // difference is non-trivial.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                    // could scale addend by ratio of difference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                    // halfUlp here, if we bothered to compute that difference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                    // Most of the time ( I hope ) it is about 1 anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                    dValue += ulp( dValue, overvalue );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                    if ( dValue == 0.0 || dValue == Double.POSITIVE_INFINITY )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                        break correctionLoop; // oops. Fell off end of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                    continue; // try again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            return (isNegative)? -dValue : dValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * Take a FloatingDecimal, which we presumably just scanned in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * and find out what its value is, as a float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * This is distinct from doubleValue() to avoid the extremely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * unlikely case of a double rounding error, wherein the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * to double has one rounding error, and the conversion of that double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * to a float has another rounding error, IN THE WRONG DIRECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * ( because of the preference to a zero low-order bit ).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
9521
e18f95eadb3c 7039369: Limit range of strictfp in FloatingDecimal
darcy
parents: 9276
diff changeset
  1639
    public strictfp float floatValue(){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        int     kDigits = Math.min( nDigits, singleMaxDecimalDigits+1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        int     iValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        float   fValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        // First, check for NaN and Infinity values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        if(digits == infinity || digits == notANumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            if(digits == notANumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                return Float.NaN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                return (isNegative?Float.NEGATIVE_INFINITY:Float.POSITIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
             * convert the lead kDigits to an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            iValue = (int)digits[0]-(int)'0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            for ( int i=1; i < kDigits; i++ ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                iValue = iValue*10 + (int)digits[i]-(int)'0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            fValue = (float)iValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            int exp = decExponent-kDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
             * iValue now contains an integer with the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
             * the first kDigits digits of the number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
             * fValue contains the (float) of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            if ( nDigits <= singleMaxDecimalDigits ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                 * possibly an easy case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                 * We know that the digits can be represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                 * exactly. And if the exponent isn't too outrageous,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                 * the whole thing can be done with one operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                 * thus one rounding error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                 * Note that all our constructors trim all leading and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                 * trailing zeros, so simple values (including zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                 * will always end up here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                if (exp == 0 || fValue == 0.0f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                    return (isNegative)? -fValue : fValue; // small floating integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                else if ( exp >= 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                    if ( exp <= singleMaxSmallTen ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                         * Can get the answer with one operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                         * thus one roundoff.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                        fValue *= singleSmall10pow[exp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                        return (isNegative)? -fValue : fValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                    int slop = singleMaxDecimalDigits - kDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                    if ( exp <= singleMaxSmallTen+slop ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                         * We can multiply dValue by 10^(slop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                         * and it is still "small" and exact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                         * Then we can multiply by 10^(exp-slop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                         * with one rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                        fValue *= singleSmall10pow[slop];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                        fValue *= singleSmall10pow[exp-slop];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                        return (isNegative)? -fValue : fValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                     * Else we have a hard case with a positive exp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                    if ( exp >= -singleMaxSmallTen ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                         * Can get the answer in one division.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                        fValue /= singleSmall10pow[-exp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                        return (isNegative)? -fValue : fValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                     * Else we have a hard case with a negative exp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            } else if ( (decExponent >= nDigits) && (nDigits+decExponent <= maxDecimalDigits) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                 * In double-precision, this is an exact floating integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                 * So we can compute to double, then shorten to float
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                 * with one round, and get the right answer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                 * First, finish accumulating digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                 * Then convert that integer to a double, multiply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                 * by the appropriate power of ten, and convert to float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                long lValue = (long)iValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                for ( int i=kDigits; i < nDigits; i++ ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                    lValue = lValue*10L + (long)((int)digits[i]-(int)'0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                double dValue = (double)lValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                exp = decExponent-nDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                dValue *= small10pow[exp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                fValue = (float)dValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                return (isNegative)? -fValue : fValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
             * Harder cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
             * The sum of digits plus exponent is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
             * what we think we can do with one error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
             * Start by weeding out obviously out-of-range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
             * results, then convert to double and go to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
             * common hard-case code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            if ( decExponent > singleMaxDecimalExponent+1 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                 * Lets face it. This is going to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                 * Infinity. Cut to the chase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                return (isNegative)? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            } else if ( decExponent < singleMinDecimalExponent-1 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                 * Lets face it. This is going to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                 * zero. Cut to the chase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                return (isNegative)? -0.0f : 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
             * Here, we do 'way too much work, but throwing away
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
             * our partial results, and going and doing the whole
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
             * thing as double, then throwing away half the bits that computes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
             * when we convert back to float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
             * The alternative is to reproduce the whole multiple-precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
             * algorithm for float precision, or to try to parameterize it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
             * for common usage. The former will take about 400 lines of code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
             * and the latter I tried without success. Thus the semi-hack
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
             * answer here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            mustSetRoundDir = !fromHex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            double dValue = doubleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            return stickyRound( dValue );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * All the positive powers of 10 that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * represented exactly in double/float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
    private static final double small10pow[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        1.0e0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        1.0e6, 1.0e7, 1.0e8, 1.0e9, 1.0e10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        1.0e11, 1.0e12, 1.0e13, 1.0e14, 1.0e15,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        1.0e16, 1.0e17, 1.0e18, 1.0e19, 1.0e20,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        1.0e21, 1.0e22
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    private static final float singleSmall10pow[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        1.0e0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        1.0e1f, 1.0e2f, 1.0e3f, 1.0e4f, 1.0e5f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        1.0e6f, 1.0e7f, 1.0e8f, 1.0e9f, 1.0e10f
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
    private static final double big10pow[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        1e16, 1e32, 1e64, 1e128, 1e256 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    private static final double tiny10pow[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
    private static final int maxSmallTen = small10pow.length-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    private static final int singleMaxSmallTen = singleSmall10pow.length-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    private static final int small5pow[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        5*5*5*5*5*5*5*5*5*5*5*5*5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
    private static final long long5pow[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        1L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        5L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        5L*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        5L*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        5L*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        5L*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        5L*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        5L*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        5L*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        5L*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        5L*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        5L*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        5L*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        5L*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    // approximately ceil( log2( long5pow[i] ) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    private static final int n5bits[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        12,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        14,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        17,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        19,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        21,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        24,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        26,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        28,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        31,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        33,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        35,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        38,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        40,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        42,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        45,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        47,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        49,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        52,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        54,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        56,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        59,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        61,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    private static final char infinity[] = { 'I', 'n', 'f', 'i', 'n', 'i', 't', 'y' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    private static final char notANumber[] = { 'N', 'a', 'N' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    private static final char zero[] = { '0', '0', '0', '0', '0', '0', '0', '0' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * Grammar is compatible with hexadecimal floating-point constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * described in section 6.4.4.2 of the C99 specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     */
2170
f4454a8d22de 6799689: Make sun.misc.FloatingDecimal.hexFloatPattern static field initialized lazily
mchung
parents: 2
diff changeset
  1894
    private static Pattern hexFloatPattern = null;
f4454a8d22de 6799689: Make sun.misc.FloatingDecimal.hexFloatPattern static field initialized lazily
mchung
parents: 2
diff changeset
  1895
    private static synchronized Pattern getHexFloatPattern() {
f4454a8d22de 6799689: Make sun.misc.FloatingDecimal.hexFloatPattern static field initialized lazily
mchung
parents: 2
diff changeset
  1896
        if (hexFloatPattern == null) {
f4454a8d22de 6799689: Make sun.misc.FloatingDecimal.hexFloatPattern static field initialized lazily
mchung
parents: 2
diff changeset
  1897
           hexFloatPattern = Pattern.compile(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                   //1           234                   56                7                   8      9
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                    "([-+])?0[xX](((\\p{XDigit}+)\\.?)|((\\p{XDigit}*)\\.(\\p{XDigit}+)))[pP]([-+])?(\\p{Digit}+)[fFdD]?"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                    );
2170
f4454a8d22de 6799689: Make sun.misc.FloatingDecimal.hexFloatPattern static field initialized lazily
mchung
parents: 2
diff changeset
  1901
        }
f4454a8d22de 6799689: Make sun.misc.FloatingDecimal.hexFloatPattern static field initialized lazily
mchung
parents: 2
diff changeset
  1902
        return hexFloatPattern;
f4454a8d22de 6799689: Make sun.misc.FloatingDecimal.hexFloatPattern static field initialized lazily
mchung
parents: 2
diff changeset
  1903
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * Convert string s to a suitable floating decimal; uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * double constructor and set the roundDir variable appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * in case the value is later converted to a float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
   static FloatingDecimal parseHexString(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        // Verify string is a member of the hexadecimal floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        // string language.
2170
f4454a8d22de 6799689: Make sun.misc.FloatingDecimal.hexFloatPattern static field initialized lazily
mchung
parents: 2
diff changeset
  1913
        Matcher m = getHexFloatPattern().matcher(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        boolean validInput = m.matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        if (!validInput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            // Input does not match pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            throw new NumberFormatException("For input string: \"" + s + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        } else { // validInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
             * We must isolate the sign, significand, and exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
             * fields.  The sign value is straightforward.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
             * floating-point numbers are stored with a normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
             * representation, the significand and exponent are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
             * interrelated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
             * After extracting the sign, we normalized the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
             * significand as a hexadecimal value, calculating an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
             * exponent adjust for any shifts made during
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
             * normalization.  If the significand is zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
             * exponent doesn't need to be examined since the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
             * will be zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
             * Next the exponent in the input string is extracted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
             * Afterwards, the significand is normalized as a *binary*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
             * value and the input value's normalized exponent can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
             * computed.  The significand bits are copied into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
             * double significand; if the string has more logical bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
             * than can fit in a double, the extra bits affect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
             * round and sticky bits which are used to round the final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
             * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
            //  Extract significand sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            String group1 = m.group(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            double sign = (( group1 == null ) || group1.equals("+"))? 1.0 : -1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            //  Extract Significand magnitude
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
             * Based on the form of the significand, calculate how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
             * binary exponent needs to be adjusted to create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
             * normalized *hexadecimal* floating-point number; that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
             * is, a number where there is one nonzero hex digit to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
             * the left of the (hexa)decimal point.  Since we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
             * adjusting a binary, not hexadecimal exponent, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
             * exponent is adjusted by a multiple of 4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
             * There are a number of significand scenarios to consider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
             * letters are used in indicate nonzero digits:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
             * 1. 000xxxx       =>      x.xxx   normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
             *    increase exponent by (number of x's - 1)*4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
             * 2. 000xxx.yyyy =>        x.xxyyyy        normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
             *    increase exponent by (number of x's - 1)*4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
             * 3. .000yyy  =>   y.yy    normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
             *    decrease exponent by (number of zeros + 1)*4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
             * 4. 000.00000yyy => y.yy normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
             *    decrease exponent by (number of zeros to right of point + 1)*4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
             * If the significand is exactly zero, return a properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
             * signed zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            String significandString =null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            int signifLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            int exponentAdjust = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                int leftDigits  = 0; // number of meaningful digits to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                                     // left of "decimal" point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                                     // (leading zeros stripped)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                int rightDigits = 0; // number of digits to right of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                                     // "decimal" point; leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                                     // must always be accounted for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                 * The significand is made up of either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                 * 1. group 4 entirely (integer portion only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                 * OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                 * 2. the fractional portion from group 7 plus any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                 * (optional) integer portions from group 6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                String group4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                if( (group4 = m.group(4)) != null) {  // Integer-only significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                    // Leading zeros never matter on the integer portion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                    significandString = stripLeadingZeros(group4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                    leftDigits = significandString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                    // Group 6 is the optional integer; leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                    // never matter on the integer portion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                    String group6 = stripLeadingZeros(m.group(6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                    leftDigits = group6.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                    // fraction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                    String group7 = m.group(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                    rightDigits = group7.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                    // Turn "integer.fraction" into "integer"+"fraction"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                    significandString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                        ((group6 == null)?"":group6) + // is the null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                        // check necessary?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                        group7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                significandString = stripLeadingZeros(significandString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                signifLength  = significandString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                 * Adjust exponent as described above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                if (leftDigits >= 1) {  // Cases 1 and 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                    exponentAdjust = 4*(leftDigits - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                } else {                // Cases 3 and 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                    exponentAdjust = -4*( rightDigits - signifLength + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                // If the significand is zero, the exponent doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                // matter; return a properly signed zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                if (signifLength == 0) { // Only zeros in input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                    return new FloatingDecimal(sign * 0.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            //  Extract Exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
             * Use an int to read in the exponent value; this should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
             * provide more than sufficient range for non-contrived
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
             * inputs.  If reading the exponent in as an int does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
             * overflow, examine the sign of the exponent and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
             * significand to determine what to do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            String group8 = m.group(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            boolean positiveExponent = ( group8 == null ) || group8.equals("+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
            long unsignedRawExponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                unsignedRawExponent = Integer.parseInt(m.group(9));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
            catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                // At this point, we know the exponent is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                // syntactically well-formed as a sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                // digits.  Therefore, if an NumberFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                // is thrown, it must be due to overflowing int's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                // range.  Also, at this point, we have already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                // checked for a zero significand.  Thus the signs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                // of the exponent and significand determine the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                // final result:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                //                      significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                //                      +               -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                // exponent     +       +infinity       -infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                //              -       +0.0            -0.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                return new FloatingDecimal(sign * (positiveExponent ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                                                   Double.POSITIVE_INFINITY : 0.0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            long rawExponent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                (positiveExponent ? 1L : -1L) * // exponent sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                unsignedRawExponent;            // exponent magnitude
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            // Calculate partially adjusted exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            long exponent = rawExponent + exponentAdjust ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            // Starting copying non-zero bits into proper position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            // a long; copy explicit bit too; this will be masked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            // later for normal values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            boolean round = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            boolean sticky = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            int bitsCopied=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            int nextShift=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            long significand=0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            // First iteration is different, since we only copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            // from the leading significand bit; one more exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            // adjust will be needed...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            // IMPORTANT: make leadingDigit a long to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            // surprising shift semantics!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
            long leadingDigit = getHexDigit(significandString, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
             * Left shift the leading digit (53 - (bit position of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
             * leading 1 in digit)); this sets the top bit of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
             * significand to 1.  The nextShift value is adjusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
             * to take into account the number of bit positions of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
             * the leadingDigit actually used.  Finally, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
             * exponent is adjusted to normalize the significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
             * as a binary value, not just a hex value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            if (leadingDigit == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                significand |= leadingDigit << 52;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                nextShift = 52 - 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                /* exponent += 0 */     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            else if (leadingDigit <= 3) { // [2, 3]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                significand |= leadingDigit << 51;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                nextShift = 52 - 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                exponent += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            else if (leadingDigit <= 7) { // [4, 7]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                significand |= leadingDigit << 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                nextShift = 52 - 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                exponent += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            else if (leadingDigit <= 15) { // [8, f]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                significand |= leadingDigit << 49;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                nextShift = 52 - 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                exponent += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                throw new AssertionError("Result from digit conversion too large!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
            // The preceding if-else could be replaced by a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            // code block based on the high-order bit set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            // leadingDigit.  Given leadingOnePosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            // significand |= leadingDigit << (SIGNIFICAND_WIDTH - leadingOnePosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            // nextShift = 52 - (3 + leadingOnePosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            // exponent += (leadingOnePosition-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
             * Now the exponent variable is equal to the normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
             * binary exponent.  Code below will make representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
             * adjustments if the exponent is incremented after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
             * rounding (includes overflows to infinity) or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
             * result is subnormal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            // Copy digit into significand until the significand can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
            // hold another full hex digit or there are no more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            // hex digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
            for(i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                i < signifLength && nextShift >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                long currentDigit = getHexDigit(significandString, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                significand |= (currentDigit << nextShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                nextShift-=4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            // After the above loop, the bulk of the string is copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            // Now, we must copy any partial hex digits into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            // significand AND compute the round bit and start computing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            // sticky bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            if ( i < signifLength ) { // at least one hex input digit exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                long currentDigit = getHexDigit(significandString, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                // from nextShift, figure out how many bits need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                // to be copied, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                switch(nextShift) { // must be negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                    // three bits need to be copied in; can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                    // set round bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                    significand |= ((currentDigit & 0xEL) >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                    round = (currentDigit & 0x1L)  != 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                case -2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                    // two bits need to be copied in; can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                    // set round and start sticky
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                    significand |= ((currentDigit & 0xCL) >> 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                    round = (currentDigit &0x2L)  != 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                    sticky = (currentDigit & 0x1L) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                case -3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                    // one bit needs to be copied in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                    significand |= ((currentDigit & 0x8L)>>3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                    // Now set round and start sticky, if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                    round = (currentDigit &0x4L)  != 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                    sticky = (currentDigit & 0x3L) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                case -4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    // all bits copied into significand; set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                    // round and start sticky
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                    round = ((currentDigit & 0x8L) != 0);  // is top bit set?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                    // nonzeros in three low order bits?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                    sticky = (currentDigit & 0x7L) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                    throw new AssertionError("Unexpected shift distance remainder.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                    // break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                // Round is set; sticky might be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                // For the sticky bit, it suffices to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                // current digit and test for any nonzero digits in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                // the remaining unprocessed input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                while(i < signifLength && !sticky) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                    currentDigit =  getHexDigit(significandString,i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                    sticky = sticky || (currentDigit != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            // else all of string was seen, round and sticky are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            // correct as false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            // Check for overflow and update exponent accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            if (exponent > DoubleConsts.MAX_EXPONENT) {         // Infinite result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                // overflow to properly signed infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                return new FloatingDecimal(sign * Double.POSITIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
            } else {  // Finite return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                if (exponent <= DoubleConsts.MAX_EXPONENT && // (Usually) normal result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                    exponent >= DoubleConsts.MIN_EXPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                    // The result returned in this block cannot be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                    // zero or subnormal; however after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                    // significand is adjusted from rounding, we could
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                    // still overflow in infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                    // AND exponent bits into significand; if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                    // significand is incremented and overflows from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                    // rounding, this combination will update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                    // exponent correctly, even in the case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                    // Double.MAX_VALUE overflowing to infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
  2240
                    significand = (( (exponent +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                                     (long)DoubleConsts.EXP_BIAS) <<
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                                     (DoubleConsts.SIGNIFICAND_WIDTH-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                                   & DoubleConsts.EXP_BIT_MASK) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                        (DoubleConsts.SIGNIF_BIT_MASK & significand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                }  else  {  // Subnormal or zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                    // (exponent < DoubleConsts.MIN_EXPONENT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                    if (exponent < (DoubleConsts.MIN_SUB_EXPONENT -1 )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                        // No way to round back to nonzero value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                        // regardless of significand if the exponent is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                        // less than -1075.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                        return new FloatingDecimal(sign * 0.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                    } else { //  -1075 <= exponent <= MIN_EXPONENT -1 = -1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                         * Find bit position to round to; recompute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                         * round and sticky bits, and shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                         * significand right appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                        sticky = sticky || round;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                        round = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                        // Number of bits of significand to preserve is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                        // exponent - abs_min_exp +1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                        // check:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                        // -1075 +1074 + 1 = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                        // -1023 +1074 + 1 = 52
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                        int bitsDiscarded = 53 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                            ((int)exponent - DoubleConsts.MIN_SUB_EXPONENT + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                        assert bitsDiscarded >= 1 && bitsDiscarded <= 53;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                        // What to do here:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                        // First, isolate the new round bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                        round = (significand & (1L << (bitsDiscarded -1))) != 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                        if (bitsDiscarded > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                            // create mask to update sticky bits; low
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                            // order bitsDiscarded bits should be 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                            long mask = ~((~0L) << (bitsDiscarded -1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                            sticky = sticky || ((significand & mask) != 0L ) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                        // Now, discard the bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                        significand = significand >> bitsDiscarded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                        significand = (( ((long)(DoubleConsts.MIN_EXPONENT -1) + // subnorm exp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                                          (long)DoubleConsts.EXP_BIAS) <<
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                                         (DoubleConsts.SIGNIFICAND_WIDTH-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                                       & DoubleConsts.EXP_BIT_MASK) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                            (DoubleConsts.SIGNIF_BIT_MASK & significand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                // The significand variable now contains the currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                // appropriate exponent bits too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                 * Determine if significand should be incremented;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                 * making this determination depends on the least
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                 * significant bit and the round and sticky bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                 * Round to nearest even rounding table, adapted from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                 * table 4.7 in "Computer Arithmetic" by IsraelKoren.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                 * The digit to the left of the "decimal" point is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                 * least significant bit, the digits to the right of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                 * the point are the round and sticky bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                 * Number       Round(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                 * x0.00        x0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                 * x0.01        x0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                 * x0.10        x0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                 * x0.11        x1. = x0. +1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                 * x1.00        x1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                 * x1.01        x1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                 * x1.10        x1. + 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                 * x1.11        x1. + 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                boolean incremented = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                boolean leastZero  = ((significand & 1L) == 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                if( (  leastZero  && round && sticky ) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                    ((!leastZero) && round )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                    incremented = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                    significand++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9521
diff changeset
  2327
                FloatingDecimal fd = new FloatingDecimal(Math.copySign(
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9521
diff changeset
  2328
                                                              Double.longBitsToDouble(significand),
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 9521
diff changeset
  2329
                                                              sign));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                 * Set roundingDir variable field of fd properly so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                 * that the input string can be properly rounded to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                 * float value.  There are two cases to consider:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                 * 1. rounding to double discards sticky bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                 * information that would change the result of a float
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                 * rounding (near halfway case between two floats)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                 * 2. rounding to double rounds up when rounding up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                 * would not occur when rounding to float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                 * For former case only needs to be considered when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                 * the bits rounded away when casting to float are all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                 * zero; otherwise, float round bit is properly set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                 * and sticky will already be true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                 * The lower exponent bound for the code below is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                 * minimum (normalized) subnormal exponent - 1 since a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                 * value with that exponent can round up to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                 * minimum subnormal value and the sticky bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                 * information must be preserved (i.e. case 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                if ((exponent >= FloatConsts.MIN_SUB_EXPONENT-1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                    (exponent <= FloatConsts.MAX_EXPONENT ) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                    // Outside above exponent range, the float value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                    // will be zero or infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                     * If the low-order 28 bits of a rounded double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                     * significand are 0, the double could be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                     * half-way case for a rounding to float.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                     * double value is a half-way case, the double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                     * significand may have to be modified to round
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                     * the the right float value (see the stickyRound
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                     * method).  If the rounding to double has lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                     * what would be float sticky bit information, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                     * double significand must be incremented.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                     * double value's significand was itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                     * incremented, the float value may end up too
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                     * large so the increment should be undone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                    if ((significand & 0xfffffffL) ==  0x0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                        // For negative values, the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                        // roundDir is the same as for positive values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
                        // since adding 1 increasing the significand's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                        // magnitude and subtracting 1 decreases the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                        // significand's magnitude.  If neither round
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                        // nor sticky is true, the double value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                        // exact and no adjustment is required for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                        // proper float rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                        if( round || sticky) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                            if (leastZero) { // prerounding lsb is 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                                // If round and sticky were both true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                                // and the least significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                                // significand bit were 0, the rounded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                                // significand would not have its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                                // low-order bits be zero.  Therefore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                                // we only need to adjust the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                                // significand if round XOR sticky is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                                // true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                                if (round ^ sticky) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                                    fd.roundDir =  1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                            else { // prerounding lsb is 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                                // If the prerounding lsb is 1 and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                                // resulting significand has its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                                // low-order bits zero, the significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
                                // was incremented.  Here, we undo the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                                // increment, which will ensure the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
                                // right guard and sticky bits for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
                                // float rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
                                if (round)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                                    fd.roundDir =  -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                fd.fromHex = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     * Return <code>s</code> with any leading zeros removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
    static String stripLeadingZeros(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        return  s.replaceFirst("^0+", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * Extract a hexadecimal digit from position <code>position</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * of string <code>s</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
    static int getHexDigit(String s, int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        int value = Character.digit(s.charAt(position), 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        if (value <= -1 || value >= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            throw new AssertionError("Unexpected failure of digit conversion of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                                     s.charAt(position));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
}