Fixes bugs and formatting

This commit is contained in:
konsoletyper 2014-07-03 14:06:14 +04:00
parent 08fc4c5f23
commit 81ac1568bf
2 changed files with 85 additions and 76 deletions

View File

@ -30,9 +30,8 @@ class TConversion {
* Holds the maximal exponent for each radix, so that radix<sup>digitFitInInt[radix]</sup>
* fit in an {@code int} (32 bits).
*/
static final int[] digitFitInInt = { -1, -1, 31, 19, 15, 13, 11,
11, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 5 };
static final int[] digitFitInInt = { -1, -1, 31, 19, 15, 13, 11, 11, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7,
7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 };
/**
* bigRadices values are precomputed maximal powers of radices (integer
@ -40,13 +39,11 @@ class TConversion {
* 2 ^ 31, bigRadices[8] = 10 ^ 9, etc.
*/
static final int bigRadices[] = { -2147483648, 1162261467,
1073741824, 1220703125, 362797056, 1977326743, 1073741824,
387420489, 1000000000, 214358881, 429981696, 815730721, 1475789056,
170859375, 268435456, 410338673, 612220032, 893871739, 1280000000,
1801088541, 113379904, 148035889, 191102976, 244140625, 308915776,
387420489, 481890304, 594823321, 729000000, 887503681, 1073741824,
1291467969, 1544804416, 1838265625, 60466176 };
static final int bigRadices[] = { -2147483648, 1162261467, 1073741824, 1220703125, 362797056, 1977326743,
1073741824, 387420489, 1000000000, 214358881, 429981696, 815730721, 1475789056, 170859375, 268435456,
410338673, 612220032, 893871739, 1280000000, 1801088541, 113379904, 148035889, 191102976, 244140625,
308915776, 387420489, 481890304, 594823321, 729000000, 887503681, 1073741824, 1291467969, 1544804416,
1838265625, 60466176 };
/** @see TBigInteger#toString(int) */
@ -56,7 +53,7 @@ class TConversion {
int digits[] = val.digits;
if (sign == 0) {
return "0"; //$NON-NLS-1$
return "0";
}
if (numberLength == 1) {
int highDigit = digits[numberLength - 1];
@ -66,14 +63,12 @@ class TConversion {
}
return Long.toString(v, radix);
}
if ((radix == 10) || (radix < Character.MIN_RADIX)
|| (radix > Character.MAX_RADIX)) {
if (radix == 10 || radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
return val.toString();
}
double bitsForRadixDigit;
bitsForRadixDigit = Math.log(radix) / Math.log(2);
int resLengthInChars = (int) (val.abs().bitLength() / bitsForRadixDigit + ((sign < 0) ? 1
: 0)) + 1;
int resLengthInChars = (int) (val.abs().bitLength() / bitsForRadixDigit + ((sign < 0) ? 1 : 0)) + 1;
char result[] = new char[resLengthInChars];
int currentChar = resLengthInChars;
@ -89,12 +84,10 @@ class TConversion {
while (true) {
// divide the array of digits by bigRadix and convert remainders
// to characters collecting them in the char array
resDigit = TDivision.divideArrayByInt(temp, temp, tempLen,
bigRadix);
resDigit = TDivision.divideArrayByInt(temp, temp, tempLen, bigRadix);
int previous = currentChar;
do {
result[--currentChar] = Character.forDigit(
resDigit % radix, radix);
result[--currentChar] = Character.forDigit(resDigit % radix, radix);
} while (((resDigit /= radix) != 0) && (currentChar != 0));
int delta = charsPerInt - previous + currentChar;
for (i = 0; i < delta && currentChar > 0; i++) {
@ -144,25 +137,25 @@ class TConversion {
if (sign == 0) {
switch (scale) {
case 0:
return "0"; //$NON-NLS-1$
return "0";
case 1:
return "0.0"; //$NON-NLS-1$
return "0.0";
case 2:
return "0.00"; //$NON-NLS-1$
return "0.00";
case 3:
return "0.000"; //$NON-NLS-1$
return "0.000";
case 4:
return "0.0000"; //$NON-NLS-1$
return "0.0000";
case 5:
return "0.00000"; //$NON-NLS-1$
return "0.00000";
case 6:
return "0.000000"; //$NON-NLS-1$
return "0.000000";
default:
StringBuilder result1 = new StringBuilder();
if (scale < 0) {
result1.append("0E+"); //$NON-NLS-1$
result1.append("0E+");
} else {
result1.append("0E"); //$NON-NLS-1$
result1.append("0E");
}
result1.append(-scale);
return result1.toString();
@ -206,8 +199,7 @@ class TConversion {
// to characters collecting them in the char array
long result11 = 0;
for (int i1 = tempLen - 1; i1 >= 0; i1--) {
long temp1 = (result11 << 32)
+ (temp[i1] & 0xFFFFFFFFL);
long temp1 = (result11 << 32) + (temp[i1] & 0xFFFFFFFFL);
long res = divideLongByBillion(temp1);
temp[i1] = (int) res;
result11 = (int) (res >> 32);
@ -216,7 +208,7 @@ class TConversion {
int previous = currentChar;
do {
result[--currentChar] = (char) (0x0030 + (resDigit % 10));
} while (((resDigit /= 10) != 0) && (currentChar != 0));
} while ((resDigit /= 10) != 0 && currentChar != 0);
int delta = 9 - previous + currentChar;
for (int i = 0; (i < delta) && (currentChar > 0); i++) {
result[--currentChar] = '0';
@ -239,8 +231,7 @@ class TConversion {
if (negNumber) {
result[--currentChar] = '-';
}
return new String(result, currentChar, resLengthInChars
- currentChar);
return new String(result, currentChar, resLengthInChars - currentChar);
}
if ((scale > 0) && (exponent >= -6)) {
if (exponent >= 0) {
@ -253,8 +244,7 @@ class TConversion {
if (negNumber) {
result[--currentChar] = '-';
}
return new String(result, currentChar, resLengthInChars
- currentChar + 1);
return new String(result, currentChar, resLengthInChars - currentChar + 1);
}
// special case 2
for (int j = 2; j < -exponent + 1; j++) {
@ -265,8 +255,7 @@ class TConversion {
if (negNumber) {
result[--currentChar] = '-';
}
return new String(result, currentChar, resLengthInChars
- currentChar);
return new String(result, currentChar, resLengthInChars - currentChar);
}
int startPoint = currentChar + 1;
int endPoint = resLengthInChars;
@ -277,11 +266,9 @@ class TConversion {
if (endPoint - startPoint >= 1) {
result1.append(result[currentChar]);
result1.append('.');
result1.append(result, currentChar + 1, resLengthInChars
- currentChar - 1);
result1.append(result, currentChar + 1, resLengthInChars - currentChar - 1);
} else {
result1.append(result, currentChar, resLengthInChars
- currentChar);
result1.append(result, currentChar, resLengthInChars - currentChar);
}
result1.append('E');
if (exponent > 0) {
@ -302,21 +289,21 @@ class TConversion {
}
if (value == 0) {
switch (scale) {
case 0: return "0"; //$NON-NLS-1$
case 1: return "0.0"; //$NON-NLS-1$
case 2: return "0.00"; //$NON-NLS-1$
case 3: return "0.000"; //$NON-NLS-1$
case 4: return "0.0000"; //$NON-NLS-1$
case 5: return "0.00000"; //$NON-NLS-1$
case 6: return "0.000000"; //$NON-NLS-1$
case 0: return "0";
case 1: return "0.0";
case 2: return "0.00";
case 3: return "0.000";
case 4: return "0.0000";
case 5: return "0.00000";
case 6: return "0.000000";
default:
StringBuilder result1 = new StringBuilder();
if (scale < 0) {
result1.append("0E+"); //$NON-NLS-1$
result1.append("0E+");
} else {
result1.append("0E"); //$NON-NLS-1$
result1.append("0E");
}
result1.append( (scale == Integer.MIN_VALUE) ? "2147483648" : Integer.toString(-scale)); //$NON-NLS-1$
result1.append((scale == Integer.MIN_VALUE) ? "2147483648" : Integer.toString(-scale));
return result1.toString();
}
}
@ -347,11 +334,11 @@ class TConversion {
if (scale > 0 && exponent >= -6) {
if (exponent >= 0) {
// special case 1
int insertPoint = currentChar + (int) exponent ;
for(int j=resLengthInChars-1; j>=insertPoint; j--) {
result[j+1] = result[j];
int insertPoint = currentChar + (int)exponent;
for(int j = resLengthInChars - 1; j >= insertPoint; j--) {
result[j + 1] = result[j];
}
result[++insertPoint]='.';
result[++insertPoint] = '.';
if (negNumber) {
result[--currentChar] = '-';
}
@ -370,16 +357,16 @@ class TConversion {
}
int startPoint = currentChar + 1;
int endPoint = resLengthInChars;
StringBuilder result1 = new StringBuilder(16+endPoint-startPoint);
StringBuilder result1 = new StringBuilder(16 + endPoint - startPoint);
if (negNumber) {
result1.append('-');
}
if (endPoint - startPoint >= 1) {
result1.append(result[currentChar]);
result1.append('.');
result1.append(result,currentChar+1,resLengthInChars - currentChar-1);
result1.append(result, currentChar+1, resLengthInChars - currentChar-1);
} else {
result1.append(result,currentChar,resLengthInChars - currentChar);
result1.append(result, currentChar, resLengthInChars - currentChar);
}
result1.append('E');
if (exponent > 0) {
@ -409,20 +396,18 @@ class TConversion {
// double the remainder and add 1 if 'a' is odd
rem = (rem << 1) + (a & 1);
}
return ((rem << 32) | (quot & 0xFFFFFFFFL));
return (rem << 32) | (quot & 0xFFFFFFFFL);
}
/** @see TBigInteger#doubleValue() */
static double bigInteger2Double(TBigInteger val) {
// val.bitLength() < 64
if ((val.numberLength < 2)
|| ((val.numberLength == 2) && (val.digits[1] > 0))) {
if ((val.numberLength < 2) || ((val.numberLength == 2) && (val.digits[1] > 0))) {
return val.longValue();
}
// val.bitLength() >= 33 * 32 > 1024
if (val.numberLength > 32) {
return ((val.sign > 0) ? Double.POSITIVE_INFINITY
: Double.NEGATIVE_INFINITY);
return val.sign > 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
int bitLen = val.abs().bitLength();
long exponent = bitLen - 1;
@ -436,17 +421,14 @@ class TConversion {
long mantissa = lVal & 0x1FFFFFFFFFFFFFL;
if (exponent == 1023) {
if (mantissa == 0X1FFFFFFFFFFFFFL) {
return ((val.sign > 0) ? Double.POSITIVE_INFINITY
: Double.NEGATIVE_INFINITY);
return val.sign > 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
if (mantissa == 0x1FFFFFFFFFFFFEL) {
return ((val.sign > 0) ? Double.MAX_VALUE : -Double.MAX_VALUE);
return val.sign > 0 ? Double.MAX_VALUE : -Double.MAX_VALUE;
}
}
// Round the mantissa
if (((mantissa & 1) == 1)
&& (((mantissa & 2) == 2) || TBitLevel.nonZeroDroppedBits(delta,
val.digits))) {
if ((mantissa & 1) == 1 && (mantissa & 2) == 2 || TBitLevel.nonZeroDroppedBits(delta, val.digits)) {
mantissa += 2;
}
mantissa >>= 1; // drop the rounding bit

View File

@ -486,10 +486,24 @@ Long_mul = function(a, b) {
var b_hilo = b.hi & 0xFFFF;
var b_hihi = b.hi >>> 16;
var lolo = (a_lolo * b_lolo) | 0;
var lohi = (a_lohi * b_lolo + a_lolo * b_lohi + (lolo >>> 16)) | 0;
var hilo = (a_hilo * b_lolo + a_lohi * b_lohi + a_lolo * b_hilo + (lohi >>> 16)) | 0;
var hihi = (a_hihi * b_lolo + a_hilo * b_lohi + a_lohi * b_hilo + a_lolo * b_hihi + (hilo >>> 16)) | 0;
var lolo = 0;
var lohi = 0;
var hilo = 0;
var hihi = 0;
lolo = (a_lolo * b_lolo) | 0;
lohi = lolo >>> 16;
lohi = ((lohi & 0xFFFF) + a_lohi * b_lolo) | 0;
hilo = (hilo + (lohi >>> 16)) | 0;
lohi = ((lohi & 0xFFFF) + a_lolo * b_lohi) | 0;
hilo = (hilo + (lohi >>> 16)) | 0;
hihi = hilo >>> 16;
hilo = ((hilo & 0xFFFF) + a_hilo * b_lolo) | 0;
hihi = (hihi + (hilo >>> 16)) | 0;
hilo = ((hilo & 0xFFFF) + a_lohi * b_lohi) | 0;
hihi = (hihi + (hilo >>> 16)) | 0;
hilo = ((hilo & 0xFFFF) + a_lolo * b_hilo) | 0;
hihi = (hihi + (hilo >>> 16)) | 0;
hihi = (hihi + a_hihi * b_lolo + a_hilo * b_lohi + a_lohi * b_hilo + a_lolo * b_hihi) | 0;
var result = new Long((lolo & 0xFFFF) | (lohi << 16), (hilo & 0xFFFF) | (hihi << 16));
return positive ? result : Long_neg(result);
}
@ -531,23 +545,36 @@ Long_xor = function(a, b) {
}
Long_shl = function(a, b) {
b &= 63;
if (b < 32) {
if (b == 0) {
return a;
} else if (b < 32) {
return new Long(a.lo << b, (a.lo >>> (32 - b)) | (a.hi << b));
} else if (b == 32) {
return new Long(0, a.lo);
} else {
return new Long(0, a.lo << (b - 32));
}
}
Long_shr = function(a, b) {
b &= 63;
if (b < 32) {
if (b == 0) {
return a;
} else if (b < 32) {
return new Long((a.lo >>> b) | (a.hi << (32 - b)), a.hi >> b);
} else if (b == 32) {
return new Long(a.hi, a.hi >> 31);
} else {
return new Long((a.hi >> (b - 32)), a.hi >> 31);
}
}
Long_shru = function(a, b) {
if (b < 32) {
b &= 63;
if (b == 0) {
return a;
} else if (b < 32) {
return new Long((a.lo >>> b) | (a.hi << (32 - b)), a.hi >>> b);
} else if (b == 32) {
return new Long(a.hi, 0);
} else {
return new Long((a.hi >>> (b - 32)), 0);
}