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