diff options
Diffstat (limited to 'libjava/classpath/java/math')
-rw-r--r-- | libjava/classpath/java/math/BigDecimal.java | 490 | ||||
-rw-r--r-- | libjava/classpath/java/math/BigInteger.java | 1328 | ||||
-rw-r--r-- | libjava/classpath/java/math/MathContext.java | 56 | ||||
-rw-r--r-- | libjava/classpath/java/math/RoundingMode.java | 10 |
4 files changed, 942 insertions, 942 deletions
diff --git a/libjava/classpath/java/math/BigDecimal.java b/libjava/classpath/java/math/BigDecimal.java index eceaf8cddc2..7f4546cdac9 100644 --- a/libjava/classpath/java/math/BigDecimal.java +++ b/libjava/classpath/java/math/BigDecimal.java @@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -50,21 +50,21 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> * The constant zero as a BigDecimal with scale zero. * @since 1.5 */ - public static final BigDecimal ZERO = + public static final BigDecimal ZERO = new BigDecimal (BigInteger.ZERO, 0); /** * The constant one as a BigDecimal with scale zero. * @since 1.5 */ - public static final BigDecimal ONE = + public static final BigDecimal ONE = new BigDecimal (BigInteger.ONE, 0); /** * The constant ten as a BigDecimal with scale zero. * @since 1.5 */ - public static final BigDecimal TEN = + public static final BigDecimal TEN = new BigDecimal (BigInteger.TEN, 0); public static final int ROUND_UP = 0; @@ -87,7 +87,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.intVal = BigInteger.valueOf(val); this.scale = 0; } - + /** * Constructs a BigDecimal using the BigDecimal(int) constructor and then * rounds according to the MathContext. @@ -106,9 +106,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.intVal = result.intVal; this.scale = result.scale; this.precision = result.precision; - } + } } - + /** * Constructs a new BigDecimal whose unscaled value is val and whose * scale is zero. @@ -119,7 +119,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.intVal = BigInteger.valueOf(val); this.scale = 0; } - + /** * Constructs a BigDecimal from the long in the same way as BigDecimal(long) * and then rounds according to the MathContext. @@ -138,12 +138,12 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.intVal = result.intVal; this.scale = result.scale; this.precision = result.precision; - } + } } - + /** - * Constructs a BigDecimal whose value is given by num rounded according to - * mc. Since num is already a BigInteger, the rounding refers only to the + * Constructs a BigDecimal whose value is given by num rounded according to + * mc. Since num is already a BigInteger, the rounding refers only to the * precision setting in mc, if mc.getPrecision() returns an int lower than * the number of digits in num, then rounding is necessary. * @param num the unscaledValue, before rounding @@ -163,15 +163,15 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.precision = result.precision; } } - + /** * Constructs a BigDecimal from the String val according to the same - * rules as the BigDecimal(String) constructor and then rounds + * rules as the BigDecimal(String) constructor and then rounds * according to the MathContext mc. * @param val the String from which we construct the initial BigDecimal * @param mc the MathContext that specifies the rounding * @throws ArithmeticException if the result is inexact but the rounding type - * is RoundingMode.UNNECESSARY + * is RoundingMode.UNNECESSARY * @since 1.5 */ public BigDecimal (String val, MathContext mc) @@ -185,13 +185,13 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.precision = result.precision; } } - + /** * Constructs a BigDecimal whose unscaled value is num and whose * scale is zero. * @param num the value of the new BigDecimal */ - public BigDecimal (BigInteger num) + public BigDecimal (BigInteger num) { this (num, 0); } @@ -207,9 +207,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.intVal = num; this.scale = scale; } - + /** - * Constructs a BigDecimal using the BigDecimal(BigInteger, int) + * Constructs a BigDecimal using the BigDecimal(BigInteger, int) * constructor and then rounds according to the MathContext. * @param num the unscaled value of the unrounded BigDecimal * @param scale the scale of the unrounded BigDecimal @@ -236,7 +236,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> * @param num the double from which the initial BigDecimal is created * @param mc the MathContext that specifies the rounding behaviour * @throws ArithmeticException if the result is inexact but the rounding type - * is RoundingMode.UNNECESSARY + * is RoundingMode.UNNECESSARY * @since 1.5 */ public BigDecimal (double num, MathContext mc) @@ -250,8 +250,8 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.precision = result.precision; } } - - public BigDecimal (double num) throws NumberFormatException + + public BigDecimal (double num) throws NumberFormatException { if (Double.isInfinite (num) || Double.isNaN (num)) throw new NumberFormatException ("invalid argument: " + num); @@ -282,37 +282,37 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> // Shave off factors of 10. while (exponent < 0 && (mantissa & 1) == 0) { - ++exponent; - mantissa >>= 1; + ++exponent; + mantissa >>= 1; } intVal = BigInteger.valueOf (bits < 0 ? - mantissa : mantissa); if (exponent < 0) { - // We have MANTISSA * 2 ^ (EXPONENT). - // Since (1/2)^N == 5^N * 10^-N we can easily convert this - // into a power of 10. - scale = (int) (- exponent); - BigInteger mult = BigInteger.valueOf (5).pow (scale); - intVal = intVal.multiply (mult); + // We have MANTISSA * 2 ^ (EXPONENT). + // Since (1/2)^N == 5^N * 10^-N we can easily convert this + // into a power of 10. + scale = (int) (- exponent); + BigInteger mult = BigInteger.valueOf (5).pow (scale); + intVal = intVal.multiply (mult); } else { - intVal = intVal.shiftLeft ((int) exponent); - scale = 0; + intVal = intVal.shiftLeft ((int) exponent); + scale = 0; } } /** - * Constructs a BigDecimal from the char subarray and rounding + * Constructs a BigDecimal from the char subarray and rounding * according to the MathContext. * @param in the char array * @param offset the start of the subarray * @param len the length of the subarray * @param mc the MathContext for rounding - * @throws NumberFormatException if the char subarray is not a valid + * @throws NumberFormatException if the char subarray is not a valid * BigDecimal representation - * @throws ArithmeticException if the result is inexact but the rounding + * @throws ArithmeticException if the result is inexact but the rounding * mode is RoundingMode.UNNECESSARY * @since 1.5 */ @@ -328,10 +328,10 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.precision = temp.precision; } } - + /** * Constructs a BigDecimal from the char array and rounding according - * to the MathContext. + * to the MathContext. * @param in the char array * @param mc the MathContext * @throws NumberFormatException if <code>in</code> is not a valid BigDecimal @@ -350,9 +350,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.intVal = temp.intVal; this.scale = temp.scale; this.precision = temp.precision; - } + } } - + /** * Constructs a BigDecimal from the given char array, accepting the same * sequence of characters as the BigDecimal(String) constructor. @@ -365,10 +365,10 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { this(in, 0, in.length); } - + /** * Constructs a BigDecimal from a char subarray, accepting the same sequence - * of characters as the BigDecimal(String) constructor. + * of characters as the BigDecimal(String) constructor. * @param in the char array * @param offset the start of the subarray * @param len the length of the subarray @@ -385,12 +385,12 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> // point is the index into the char array where the exponent starts // (or, if there is no exponent, this is equal to end) int point = offset; - // dot is the index into the char array where the decimal point is + // dot is the index into the char array where the decimal point is // found, or -1 if there is no decimal point int dot = -1; - + // The following examples show what these variables mean. Note that - // point and dot don't yet have the correct values, they will be + // point and dot don't yet have the correct values, they will be // properly assigned in a loop later on in this method. // // Example 1 @@ -409,11 +409,11 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> // // Example 3 // - // - 1 2 3 4 5 e 7 + // - 1 2 3 4 5 e 7 // __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ // - // offset = 2, len = 8, start = 3, dot = -1, point = 8, end = 10 - + // offset = 2, len = 8, start = 3, dot = -1, point = 8, end = 10 + // Determine the sign of the number. boolean negative = false; if (in[offset] == '+') @@ -428,7 +428,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> negative = true; } - // Check each character looking for the decimal point and the + // Check each character looking for the decimal point and the // start of the exponent. while (point < end) { @@ -443,7 +443,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> // Break when we reach the start of the exponent. else if (c == 'e' || c == 'E') break; - // Throw an exception if the character was not a decimal or an + // Throw an exception if the character was not a decimal or an // exponent and is not a digit. else if (!Character.isDigit(c)) throw new NumberFormatException("unrecognized character at " + point @@ -456,7 +456,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> CPStringBuilder val = new CPStringBuilder(point - start - 1); if (dot != -1) { - // If there was a decimal we must combine the two parts that + // If there was a decimal we must combine the two parts that // contain only digits and we must set the scale properly. val.append(in, start, dot - start); val.append(in, dot + 1, point - dot - 1); @@ -494,7 +494,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> try { - // Adjust the scale according to the exponent. + // Adjust the scale according to the exponent. // Remember that the value of a BigDecimal is // unscaledValue x Math.pow(10, -scale) scale -= Integer.parseInt(new String(in, point, end - point)); @@ -505,8 +505,8 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } } } - - public BigDecimal (String num) throws NumberFormatException + + public BigDecimal (String num) throws NumberFormatException { int len = num.length(); int start = 0, point = 0; @@ -514,42 +514,42 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> boolean negative = false; if (num.charAt(0) == '+') { - ++start; - ++point; + ++start; + ++point; } else if (num.charAt(0) == '-') { - ++start; - ++point; - negative = true; + ++start; + ++point; + negative = true; } while (point < len) { - char c = num.charAt (point); - if (c == '.') - { - if (dot >= 0) - throw new NumberFormatException ("multiple `.'s in number"); - dot = point; - } - else if (c == 'e' || c == 'E') - break; - else if (Character.digit (c, 10) < 0) - throw new NumberFormatException ("unrecognized character: " + c); - ++point; + char c = num.charAt (point); + if (c == '.') + { + if (dot >= 0) + throw new NumberFormatException ("multiple `.'s in number"); + dot = point; + } + else if (c == 'e' || c == 'E') + break; + else if (Character.digit (c, 10) < 0) + throw new NumberFormatException ("unrecognized character: " + c); + ++point; } String val; if (dot >= 0) { - val = num.substring (start, dot) + num.substring (dot + 1, point); - scale = point - 1 - dot; + val = num.substring (start, dot) + num.substring (dot + 1, point); + scale = point - 1 - dot; } else { - val = num.substring (start, point); - scale = 0; + val = num.substring (start, point); + scale = 0; } if (val.length () == 0) throw new NumberFormatException ("no digits seen"); @@ -567,39 +567,39 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> if (point >= len ) throw new NumberFormatException ("no exponent following e or E"); - - try - { + + try + { scale -= Integer.parseInt (num.substring (point)); - } - catch (NumberFormatException ex) - { - throw new NumberFormatException ("malformed exponent"); - } + } + catch (NumberFormatException ex) + { + throw new NumberFormatException ("malformed exponent"); + } } } - public static BigDecimal valueOf (long val) + public static BigDecimal valueOf (long val) { return valueOf (val, 0); } - public static BigDecimal valueOf (long val, int scale) - throws NumberFormatException + public static BigDecimal valueOf (long val, int scale) + throws NumberFormatException { if ((scale == 0) && ((int)val == val)) switch ((int) val) - { - case 0: - return ZERO; - case 1: - return ONE; - } + { + case 0: + return ZERO; + case 1: + return ONE; + } return new BigDecimal (BigInteger.valueOf (val), scale); } - public BigDecimal add (BigDecimal val) + public BigDecimal add (BigDecimal val) { // For addition, need to line up decimals. Note that the movePointRight // method cannot be used for this as it might return a BigDecimal with @@ -613,9 +613,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> return new BigDecimal (op1.add (op2), Math.max (scale, val.scale)); } - + /** - * Returns a BigDecimal whose value is found first by calling the + * Returns a BigDecimal whose value is found first by calling the * method add(val) and then by rounding according to the MathContext mc. * @param val the augend * @param mc the MathContext for rounding @@ -629,13 +629,13 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> return add(val).round(mc); } - public BigDecimal subtract (BigDecimal val) + public BigDecimal subtract (BigDecimal val) { return this.add(val.negate()); } /** - * Returns a BigDecimal whose value is found first by calling the + * Returns a BigDecimal whose value is found first by calling the * method subtract(val) and then by rounding according to the MathContext mc. * @param val the subtrahend * @param mc the MathContext for rounding @@ -649,14 +649,14 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> return subtract(val).round(mc); } - public BigDecimal multiply (BigDecimal val) + public BigDecimal multiply (BigDecimal val) { return new BigDecimal (intVal.multiply (val.intVal), scale + val.scale); } - + /** * Returns a BigDecimal whose value is (this x val) before it is rounded - * according to the MathContext mc. + * according to the MathContext mc. * @param val the multiplicand * @param mc the MathContext for rounding * @return a new BigDecimal with value approximately (this x val) @@ -669,15 +669,15 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> return multiply(val).round(mc); } - public BigDecimal divide (BigDecimal val, int roundingMode) - throws ArithmeticException, IllegalArgumentException + public BigDecimal divide (BigDecimal val, int roundingMode) + throws ArithmeticException, IllegalArgumentException { return divide (val, scale, roundingMode); } - + /** * Returns a BigDecimal whose value is (this / val), with the specified scale - * and rounding according to the RoundingMode + * and rounding according to the RoundingMode * @param val the divisor * @param scale the scale of the BigDecimal returned * @param roundingMode the rounding mode to use @@ -686,7 +686,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> * UNNECESSARY but the specified scale cannot represent the value exactly * @since 1.5 */ - public BigDecimal divide(BigDecimal val, + public BigDecimal divide(BigDecimal val, int scale, RoundingMode roundingMode) { return divide (val, scale, roundingMode.ordinal()); @@ -705,30 +705,30 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { return divide (val, scale, roundingMode.ordinal()); } - + public BigDecimal divide(BigDecimal val, int newScale, int roundingMode) - throws ArithmeticException, IllegalArgumentException + throws ArithmeticException, IllegalArgumentException { if (roundingMode < 0 || roundingMode > 7) - throw - new IllegalArgumentException("illegal rounding mode: " + roundingMode); + throw + new IllegalArgumentException("illegal rounding mode: " + roundingMode); - if (intVal.signum () == 0) // handle special case of 0.0/0.0 + if (intVal.signum () == 0) // handle special case of 0.0/0.0 return newScale == 0 ? ZERO : new BigDecimal (ZERO.intVal, newScale); - + // Ensure that pow gets a non-negative value. BigInteger valIntVal = val.intVal; int power = newScale - (scale - val.scale); if (power < 0) { - // Effectively increase the scale of val to avoid an - // ArithmeticException for a negative power. + // Effectively increase the scale of val to avoid an + // ArithmeticException for a negative power. valIntVal = valIntVal.multiply (BigInteger.TEN.pow (-power)); - power = 0; + power = 0; } BigInteger dividend = intVal.multiply (BigInteger.TEN.pow (power)); - + BigInteger parts[] = dividend.divideAndRemainder (valIntVal); BigInteger unrounded = parts[0]; @@ -746,33 +746,33 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> roundingMode = (sign < 0) ? ROUND_UP : ROUND_DOWN; else { - // half is -1 if remainder*2 < positive intValue (*power), 0 if equal, - // 1 if >. This implies that the remainder to round is less than, - // equal to, or greater than half way to the next digit. - BigInteger posRemainder - = parts[1].signum () < 0 ? parts[1].negate() : parts[1]; - valIntVal = valIntVal.signum () < 0 ? valIntVal.negate () : valIntVal; - int half = posRemainder.shiftLeft(1).compareTo(valIntVal); - - switch(roundingMode) - { - case ROUND_HALF_UP: - roundingMode = (half < 0) ? ROUND_DOWN : ROUND_UP; - break; - case ROUND_HALF_DOWN: - roundingMode = (half > 0) ? ROUND_UP : ROUND_DOWN; - break; - case ROUND_HALF_EVEN: - if (half < 0) - roundingMode = ROUND_DOWN; - else if (half > 0) - roundingMode = ROUND_UP; - else if (unrounded.testBit(0)) // odd, then ROUND_HALF_UP - roundingMode = ROUND_UP; - else // even, ROUND_HALF_DOWN - roundingMode = ROUND_DOWN; - break; - } + // half is -1 if remainder*2 < positive intValue (*power), 0 if equal, + // 1 if >. This implies that the remainder to round is less than, + // equal to, or greater than half way to the next digit. + BigInteger posRemainder + = parts[1].signum () < 0 ? parts[1].negate() : parts[1]; + valIntVal = valIntVal.signum () < 0 ? valIntVal.negate () : valIntVal; + int half = posRemainder.shiftLeft(1).compareTo(valIntVal); + + switch(roundingMode) + { + case ROUND_HALF_UP: + roundingMode = (half < 0) ? ROUND_DOWN : ROUND_UP; + break; + case ROUND_HALF_DOWN: + roundingMode = (half > 0) ? ROUND_UP : ROUND_DOWN; + break; + case ROUND_HALF_EVEN: + if (half < 0) + roundingMode = ROUND_DOWN; + else if (half > 0) + roundingMode = ROUND_UP; + else if (unrounded.testBit(0)) // odd, then ROUND_HALF_UP + roundingMode = ROUND_UP; + else // even, ROUND_HALF_DOWN + roundingMode = ROUND_DOWN; + break; + } } if (roundingMode == ROUND_UP) @@ -781,24 +781,24 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> // roundingMode == ROUND_DOWN return new BigDecimal (unrounded, newScale); } - + /** * Performs division, if the resulting quotient requires rounding - * (has a nonterminating decimal expansion), - * an ArithmeticException is thrown. + * (has a nonterminating decimal expansion), + * an ArithmeticException is thrown. * #see divide(BigDecimal, int, int) * @since 1.5 */ public BigDecimal divide(BigDecimal divisor) - throws ArithmeticException, IllegalArgumentException + throws ArithmeticException, IllegalArgumentException { return divide(divisor, scale, ROUND_UNNECESSARY); } /** * Returns a BigDecimal whose value is the remainder in the quotient - * this / val. This is obtained by - * subtract(divideToIntegralValue(val).multiply(val)). + * this / val. This is obtained by + * subtract(divideToIntegralValue(val).multiply(val)). * @param val the divisor * @return a BigDecimal whose value is the remainder * @throws ArithmeticException if val == 0 @@ -811,7 +811,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> /** * Returns a BigDecimal array, the first element of which is the integer part - * of this / val, and the second element of which is the remainder of + * of this / val, and the second element of which is the remainder of * that quotient. * @param val the divisor * @return the above described BigDecimal array @@ -825,9 +825,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> result[1] = subtract(result[0].multiply(val)); return result; } - + /** - * Returns a BigDecimal whose value is the integer part of the quotient + * Returns a BigDecimal whose value is the integer part of the quotient * this / val. The preferred scale is this.scale - val.scale. * @param val the divisor * @return a BigDecimal whose value is the integer part of this / val. @@ -838,12 +838,12 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { return divide(val, ROUND_DOWN).floor().setScale(scale - val.scale, ROUND_DOWN); } - + /** - * Mutates this BigDecimal into one with no fractional part, whose value is + * Mutates this BigDecimal into one with no fractional part, whose value is * equal to the largest integer that is <= to this BigDecimal. Note that * since this method is private it is okay to mutate this BigDecimal. - * @return the BigDecimal obtained through the floor operation on this + * @return the BigDecimal obtained through the floor operation on this * BigDecimal. */ private BigDecimal floor() @@ -855,17 +855,17 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> intVal = new BigInteger(intValStr).multiply(BigInteger.TEN.pow(scale)); return this; } - - public int compareTo (BigDecimal val) + + public int compareTo (BigDecimal val) { if (scale == val.scale) return intVal.compareTo (val.intVal); - BigInteger thisParts[] = + BigInteger thisParts[] = intVal.divideAndRemainder (BigInteger.TEN.pow (scale)); BigInteger valParts[] = val.intVal.divideAndRemainder (BigInteger.TEN.pow (val.scale)); - + int compare; if ((compare = thisParts[0].compareTo (valParts[0])) != 0) return compare; @@ -875,46 +875,46 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> // Add some trailing zeros to the remainder with the smallest scale if (scale < val.scale) thisParts[1] = thisParts[1].multiply - (BigInteger.valueOf (10).pow (val.scale - scale)); + (BigInteger.valueOf (10).pow (val.scale - scale)); else if (scale > val.scale) valParts[1] = valParts[1].multiply - (BigInteger.valueOf (10).pow (scale - val.scale)); + (BigInteger.valueOf (10).pow (scale - val.scale)); // and compare them return thisParts[1].compareTo (valParts[1]); } - public boolean equals (Object o) + public boolean equals (Object o) { - return (o instanceof BigDecimal - && scale == ((BigDecimal) o).scale - && compareTo ((BigDecimal) o) == 0); + return (o instanceof BigDecimal + && scale == ((BigDecimal) o).scale + && compareTo ((BigDecimal) o) == 0); } - public int hashCode() + public int hashCode() { return intValue() ^ scale; } public BigDecimal max (BigDecimal val) { - switch (compareTo (val)) + switch (compareTo (val)) { case 1: - return this; + return this; default: - return val; + return val; } } - public BigDecimal min (BigDecimal val) + public BigDecimal min (BigDecimal val) { - switch (compareTo (val)) + switch (compareTo (val)) { case -1: - return this; + return this; default: - return val; + return val; } } @@ -931,35 +931,35 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> if (scale >= n) return new BigDecimal (intVal, scale - n); - return new BigDecimal (intVal.multiply - (BigInteger.TEN.pow (n - scale)), 0); + return new BigDecimal (intVal.multiply + (BigInteger.TEN.pow (n - scale)), 0); } - public int signum () + public int signum () { return intVal.signum (); } - public int scale () + public int scale () { return scale; } - + public BigInteger unscaledValue() { return intVal; } - public BigDecimal abs () + public BigDecimal abs () { return new BigDecimal (intVal.abs (), scale); } - public BigDecimal negate () + public BigDecimal negate () { return new BigDecimal (intVal.negate (), scale); } - + /** * Returns a BigDecimal whose value is found first by negating this via * the negate() method, then by rounding according to the MathContext mc. @@ -976,9 +976,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> result = result.round(mc); return result; } - + /** - * Returns this BigDecimal. This is included for symmetry with the + * Returns this BigDecimal. This is included for symmetry with the * method negate(). * @return this * @since 1.5 @@ -987,9 +987,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { return this; } - + /** - * Returns a BigDecimal whose value is found by rounding <code>this</code> + * Returns a BigDecimal whose value is found by rounding <code>this</code> * according to the MathContext. This is the same as round(MathContext). * @param mc the MathContext for rounding * @return a BigDecimal whose value is <code>this</code> before being rounded @@ -1001,7 +1001,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { return round(mc); } - + /** * Returns a BigDecimal which is this BigDecimal rounded according to the * MathContext rounding settings. @@ -1012,12 +1012,12 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { int mcPrecision = mc.getPrecision(); int numToChop = precision() - mcPrecision; - // If mc specifies not to chop any digits or if we've already chopped + // If mc specifies not to chop any digits or if we've already chopped // enough digits (say by using a MathContext in the constructor for this // BigDecimal) then just return this. if (mcPrecision == 0 || numToChop <= 0) return this; - + // Make a new BigDecimal which is the correct power of 10 to chop off // the required number of digits and then call divide. BigDecimal div = new BigDecimal(BigInteger.TEN.pow(numToChop)); @@ -1026,38 +1026,38 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> rounded.precision = mcPrecision; return rounded; } - + /** * Returns the precision of this BigDecimal (the number of digits in the * unscaled value). The precision of a zero value is 1. - * @return the number of digits in the unscaled value, or 1 if the value + * @return the number of digits in the unscaled value, or 1 if the value * is zero. */ public int precision() { if (precision == 0) { - String s = intVal.toString(); - precision = s.length() - (( s.charAt(0) == '-' ) ? 1 : 0); + String s = intVal.toString(); + precision = s.length() - (( s.charAt(0) == '-' ) ? 1 : 0); } return precision; } - + /** * Returns the String representation of this BigDecimal, using scientific * notation if necessary. The following steps are taken to generate * the result: - * + * * 1. the BigInteger unscaledValue's toString method is called and if * <code>scale == 0<code> is returned. * 2. an <code>int adjExp</code> is created which is equal to the negation - * of <code>scale</code> plus the number of digits in the unscaled value, + * of <code>scale</code> plus the number of digits in the unscaled value, * minus one. - * 3. if <code>scale >= 0 && adjExp >= -6</code> then we represent this - * BigDecimal without scientific notation. A decimal is added if the + * 3. if <code>scale >= 0 && adjExp >= -6</code> then we represent this + * BigDecimal without scientific notation. A decimal is added if the * scale is positive and zeros are prepended as necessary. * 4. if scale is negative or adjExp is less than -6 we use scientific - * notation. If the unscaled value has more than one digit, a decimal + * notation. If the unscaled value has more than one digit, a decimal * as inserted after the first digit, the character 'E' is appended * and adjExp is appended. */ @@ -1076,7 +1076,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> if (scale >= 0 && (point - 1) >= -6) { - // Convert to character form without scientific notation. + // Convert to character form without scientific notation. if (point <= 0) { // Zeros need to be prepended to the StringBuilder. @@ -1094,7 +1094,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } else { - // No zeros need to be prepended so the String is simply the + // No zeros need to be prepended so the String is simply the // unscaled value with the decimal point inserted. val.append(bigStr); val.insert(point + (negative ? 1 : 0), '.'); @@ -1104,7 +1104,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { // We must use scientific notation to represent this BigDecimal. val.append(bigStr); - // If there is more than one digit in the unscaled value we put a + // If there is more than one digit in the unscaled value we put a // decimal after the first digit. if (bigStr.length() > 1) val.insert( ( negative ? 2 : 1 ), '.'); @@ -1119,10 +1119,10 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> /** * Returns the String representation of this BigDecimal, using engineering - * notation if necessary. This is similar to toString() but when exponents + * notation if necessary. This is similar to toString() but when exponents * are used the exponent is made to be a multiple of 3 such that the integer * part is between 1 and 999. - * + * * @return a String representation of this BigDecimal in engineering notation * @since 1.5 */ @@ -1161,7 +1161,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } else { - // No zeros need to be prepended so the String is simply the + // No zeros need to be prepended so the String is simply the // unscaled value with the decimal point inserted. val.append(bigStr); val.insert(point + (negative ? 1 : 0), '.'); @@ -1172,7 +1172,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> // We must use scientific notation to represent this BigDecimal. // The exponent must be a multiple of 3 and the integer part // must be between 1 and 999. - val.append(bigStr); + val.append(bigStr); int zeros = adjExp % 3; int dot = 1; if (adjExp > 0) @@ -1185,9 +1185,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> else { // If the exponent is negative then we move the dot to the right - // and decrease the exponent (increase its magnitude) until + // and decrease the exponent (increase its magnitude) until // it is a multiple of 3. Note that this is not adjExp -= zeros - // because the mod operator doesn't give us the distance to the + // because the mod operator doesn't give us the distance to the // correct multiple of 3. (-5 mod 3) is -2 but the distance from // -5 to the correct multiple of 3 (-6) is 1, not 2. if (zeros == -2) @@ -1211,7 +1211,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } else if (bigStr.length() > dot) val.insert(dot + (negative ? 1 : 0), '.'); - + // And then append 'E' and the exponent (adjExp). val.append('E'); if (adjExp >= 0) @@ -1220,20 +1220,20 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } return val.toString(); } - + /** - * Returns a String representation of this BigDecimal without using + * Returns a String representation of this BigDecimal without using * scientific notation. This is how toString() worked for releases 1.4 * and previous. Zeros may be added to the end of the String. For - * example, an unscaled value of 1234 and a scale of -3 would result in - * the String 1234000, but the toString() method would return + * example, an unscaled value of 1234 and a scale of -3 would result in + * the String 1234000, but the toString() method would return * 1.234E+6. * @return a String representation of this BigDecimal * @since 1.5 */ public String toPlainString() { - // If the scale is zero we simply return the String representation of the + // If the scale is zero we simply return the String representation of the // unscaled value. String bigStr = intVal.toString(); if (scale == 0) @@ -1245,7 +1245,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> int point = bigStr.length() - scale - (negative ? 1 : 0); CPStringBuilder sb = new CPStringBuilder(bigStr.length() + 2 - + (point <= 0 ? (-point + 1) : 0)); + + (point <= 0 ? (-point + 1) : 0)); if (point <= 0) { // We have to prepend zeros and a decimal point. @@ -1275,13 +1275,13 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } return sb.toString(); } - + /** * Converts this BigDecimal to a BigInteger. Any fractional part will * be discarded. * @return a BigDecimal whose value is equal to floor[this] */ - public BigInteger toBigInteger () + public BigInteger toBigInteger () { // If scale > 0 then we must divide, if scale > 0 then we must multiply, // and if scale is zero then we just return intVal; @@ -1291,9 +1291,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> return intVal.multiply(BigInteger.TEN.pow(-scale)); return intVal; } - + /** - * Converts this BigDecimal into a BigInteger, throwing an + * Converts this BigDecimal into a BigInteger, throwing an * ArithmeticException if the conversion is not exact. * @return a BigInteger whose value is equal to the value of this BigDecimal * @since 1.5 @@ -1303,7 +1303,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> if (scale > 0) { // If we have to divide, we must check if the result is exact. - BigInteger[] result = + BigInteger[] result = intVal.divideAndRemainder(BigInteger.TEN.pow(scale)); if (result[1].equals(BigInteger.ZERO)) return result[0]; @@ -1316,20 +1316,20 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> return intVal; } - public int intValue () + public int intValue () { return toBigInteger ().intValue (); } - + /** - * Returns a BigDecimal which is numerically equal to this BigDecimal but - * with no trailing zeros in the representation. For example, if this + * Returns a BigDecimal which is numerically equal to this BigDecimal but + * with no trailing zeros in the representation. For example, if this * BigDecimal has [unscaledValue, scale] = [6313000, 4] this method returns - * a BigDecimal with [unscaledValue, scale] = [6313, 1]. As another + * a BigDecimal with [unscaledValue, scale] = [6313, 1]. As another * example, [12400, -2] would become [124, -4]. * @return a numerically equal BigDecimal with no trailing zeros */ - public BigDecimal stripTrailingZeros() + public BigDecimal stripTrailingZeros() { String intValStr = intVal.toString(); int newScale = scale; @@ -1344,7 +1344,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } // Create a new BigDecimal with the appropriate substring and then // set its scale. - BigDecimal result = new BigDecimal(intValStr.substring(0, pointer + 1)); + BigDecimal result = new BigDecimal(intValStr.substring(0, pointer + 1)); result.scale = newScale; return result; } @@ -1354,12 +1354,12 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> return toBigInteger().longValue(); } - public float floatValue() + public float floatValue() { return Float.valueOf(toString()).floatValue(); } - public double doubleValue() + public double doubleValue() { return Double.valueOf(toString()).doubleValue(); } @@ -1378,26 +1378,26 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> if( scale < 0 ) throw new ArithmeticException("Scale parameter < 0."); return divide (ONE, scale, roundingMode); } - + /** * Returns a BigDecimal whose value is the same as this BigDecimal but whose * representation has a scale of <code>newScale</code>. If the scale is * reduced then rounding may occur, according to the RoundingMode. * @param newScale * @param roundingMode - * @return a BigDecimal whose scale is as given, whose value is + * @return a BigDecimal whose scale is as given, whose value is * <code>this</code> with possible rounding - * @throws ArithmeticException if the rounding mode is UNNECESSARY but - * rounding is required + * @throws ArithmeticException if the rounding mode is UNNECESSARY but + * rounding is required * @since 1.5 */ public BigDecimal setScale(int newScale, RoundingMode roundingMode) { return setScale(newScale, roundingMode.ordinal()); } - + /** - * Returns a new BigDecimal constructed from the BigDecimal(String) + * Returns a new BigDecimal constructed from the BigDecimal(String) * constructor using the Double.toString(double) method to obtain * the String. * @param val the double value used in Double.toString(double) @@ -1411,10 +1411,10 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> throw new NumberFormatException("argument cannot be NaN or infinite."); return new BigDecimal(Double.toString(val)); } - + /** * Returns a BigDecimal whose numerical value is the numerical value - * of this BigDecimal multiplied by 10 to the power of <code>n</code>. + * of this BigDecimal multiplied by 10 to the power of <code>n</code>. * @param n the power of ten * @return the new BigDecimal * @since 1.5 @@ -1425,10 +1425,10 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> result.precision = precision; return result; } - + /** - * Returns a BigDecimal whose value is <code>this</code> to the power of - * <code>n</code>. + * Returns a BigDecimal whose value is <code>this</code> to the power of + * <code>n</code>. * @param n the power * @return the new BigDecimal * @since 1.5 @@ -1440,7 +1440,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> BigDecimal result = new BigDecimal(intVal.pow(n), scale * n); return result; } - + /** * Returns a BigDecimal whose value is determined by first calling pow(n) * and then by rounding according to the MathContext mc. @@ -1457,7 +1457,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> // currently do not. return pow(n).round(mc); } - + /** * Returns a BigDecimal whose value is the absolute value of this BigDecimal * with rounding according to the given MathContext. @@ -1470,7 +1470,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> result = result.round(mc); return result; } - + /** * Returns the size of a unit in the last place of this BigDecimal. This * returns a BigDecimal with [unscaledValue, scale] = [1, this.scale()]. @@ -1481,7 +1481,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { return new BigDecimal(BigInteger.ONE, scale); } - + /** * Converts this BigDecimal to a long value. * @return the long value @@ -1499,10 +1499,10 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> || (result < 0 && signum() == 1) || (result > 0 && signum() == -1)) throw new ArithmeticException("this BigDecimal is too " + "large to fit into the return type"); - + return intVal.longValue(); } - + /** * Converts this BigDecimal into an int by first calling longValueExact * and then checking that the <code>long</code> returned from that @@ -1520,7 +1520,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> throw new ArithmeticException ("this BigDecimal cannot fit into an int"); return result; } - + /** * Converts this BigDecimal into a byte by first calling longValueExact * and then checking that the <code>long</code> returned from that @@ -1538,7 +1538,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> throw new ArithmeticException ("this BigDecimal cannot fit into a byte"); return result; } - + /** * Converts this BigDecimal into a short by first calling longValueExact * and then checking that the <code>long</code> returned from that diff --git a/libjava/classpath/java/math/BigInteger.java b/libjava/classpath/java/math/BigInteger.java index 9d7abc755ac..953e557a811 100644 --- a/libjava/classpath/java/math/BigInteger.java +++ b/libjava/classpath/java/math/BigInteger.java @@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -54,7 +54,7 @@ import java.util.logging.Logger; * Written using on-line Java Platform 1.2 API Specification, as well * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998) and * "Applied Cryptography, Second Edition" by Bruce Schneier (Wiley, 1996). - * + * * Based primarily on IntNum.java BitOps.java by Per Bothner (per@bothner.com) * (found in Kawa 1.6.62). * @@ -84,13 +84,13 @@ public class BigInteger extends Number implements Comparable<BigInteger> private static final long serialVersionUID = -8287574255936472291L; - /** We pre-allocate integers in the range minFixNum..maxFixNum. + /** We pre-allocate integers in the range minFixNum..maxFixNum. * Note that we must at least preallocate 0, 1, and 10. */ private static final int minFixNum = -100; private static final int maxFixNum = 1024; private static final int numFixNum = maxFixNum-minFixNum+1; private static final BigInteger[] smallFixNums; - + /** The alter-ego GMP instance for this. */ private transient GMP mpz; @@ -109,8 +109,8 @@ public class BigInteger extends Number implements Comparable<BigInteger> else { smallFixNums = new BigInteger[numFixNum]; - for (int i = numFixNum; --i >= 0; ) - smallFixNums[i] = new BigInteger(i + minFixNum); + for (int i = numFixNum; --i >= 0; ) + smallFixNums[i] = new BigInteger(i + minFixNum); ZERO = smallFixNums[-minFixNum]; ONE = smallFixNums[1 - minFixNum]; @@ -260,11 +260,11 @@ public class BigInteger extends Number implements Comparable<BigInteger> if (signum == 0) { - int i; - for (i = magnitude.length - 1; i >= 0 && magnitude[i] == 0; --i) - ; - if (i >= 0) - throw new NumberFormatException(); + int i; + for (i = magnitude.length - 1; i >= 0 && magnitude[i] == 0; --i) + ; + if (i >= 0) + throw new NumberFormatException(); return; } @@ -330,20 +330,20 @@ public class BigInteger extends Number implements Comparable<BigInteger> while (highbits == 0 && nwords > 0) { - highbits = rnd.nextInt(); - --nwords; + highbits = rnd.nextInt(); + --nwords; } if (nwords == 0 && highbits >= 0) { - ival = highbits; + ival = highbits; } else { - ival = highbits < 0 ? nwords + 2 : nwords + 1; - words = new int[ival]; - words[nwords] = highbits; - while (--nwords >= 0) - words[nwords] = rnd.nextInt(); + ival = highbits < 0 ? nwords + 2 : nwords + 1; + words = new int[ival]; + words[nwords] = highbits; + while (--nwords >= 0) + words[nwords] = rnd.nextInt(); } } @@ -369,7 +369,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> } } - /** + /** * Return a BigInteger that is bitLength bits long with a * probability < 2^-100 of being composite. * @@ -465,9 +465,9 @@ public class BigInteger extends Number implements Comparable<BigInteger> // Elements remaining in byte[] are a multiple of 4. while (nwords > 0) words[--nwords] = bytes[bptr++] << 24 | - (bytes[bptr++] & 0xff) << 16 | - (bytes[bptr++] & 0xff) << 8 | - (bytes[bptr++] & 0xff); + (bytes[bptr++] & 0xff) << 16 | + (bytes[bptr++] & 0xff) << 8 | + (bytes[bptr++] & 0xff); return words; } @@ -489,30 +489,30 @@ public class BigInteger extends Number implements Comparable<BigInteger> { if (nwords == 0) { - if (words != null) - { - if (ival > 0) - ival = words[0]; - words = null; - } + if (words != null) + { + if (ival > 0) + ival = words[0]; + words = null; + } } else if (words == null - || words.length < nwords - || words.length > nwords + 2) + || words.length < nwords + || words.length > nwords + 2) { - int[] new_words = new int [nwords]; - if (words == null) - { - new_words[0] = ival; - ival = 1; - } - else - { - if (nwords < ival) - ival = nwords; - System.arraycopy(words, 0, new_words, 0, ival); - } - words = new_words; + int[] new_words = new int [nwords]; + if (words == null) + { + new_words[0] = ival; + ival = 1; + } + else + { + if (nwords < ival) + ival = nwords; + System.arraycopy(words, 0, new_words, 0, ival); + } + words = new_words; } } @@ -588,19 +588,19 @@ public class BigInteger extends Number implements Comparable<BigInteger> int i = len; if (i > 0) { - int word = words[--i]; - if (word == -1) - { - while (i > 0 && (word = words[i - 1]) < 0) - { - i--; - if (word != -1) break; - } - } - else - { - while (word == 0 && i > 0 && (word = words[i - 1]) >= 0) i--; - } + int word = words[--i]; + if (word == -1) + { + while (i > 0 && (word = words[i - 1]) < 0) + { + i--; + if (word != -1) break; + } + } + else + { + while (word == 0 && i > 0 && (word = words[i - 1]) >= 0) i--; + } } return i + 1; } @@ -608,11 +608,11 @@ public class BigInteger extends Number implements Comparable<BigInteger> private BigInteger canonicalize() { if (words != null - && (ival = BigInteger.wordsNeeded(words, ival)) <= 1) + && (ival = BigInteger.wordsNeeded(words, ival)) <= 1) { - if (ival == 1) - ival = words[0]; - words = null; + if (ival == 1) + ival = words[0]; + words = null; } if (words == null && ival >= minFixNum && ival <= maxFixNum) return smallFixNums[ival - minFixNum]; @@ -641,17 +641,17 @@ public class BigInteger extends Number implements Comparable<BigInteger> { if (x.words == null) { - set((long) x.ival + (long) y); - return; + set((long) x.ival + (long) y); + return; } int len = x.ival; realloc(len + 1); long carry = y; for (int i = 0; i < len; i++) { - carry += ((long) x.words[i] & 0xffffffffL); - words[i] = (int) carry; - carry >>= 32; + carry += ((long) x.words[i] & 0xffffffffL); + words[i] = (int) carry; + carry >>= 32; } if (x.words[len - 1] < 0) carry--; @@ -671,15 +671,15 @@ public class BigInteger extends Number implements Comparable<BigInteger> int i = (int) y; if ((long) i == y) { - ival = i; - words = null; + ival = i; + words = null; } else { - realloc(2); - words[0] = i; - words[1] = (int) (y >> 32); - ival = 2; + realloc(2); + words[0] = i; + words[1] = (int) (y >> 32); + ival = 2; } } @@ -698,9 +698,9 @@ public class BigInteger extends Number implements Comparable<BigInteger> set(y.ival); else if (this != y) { - realloc(y.ival); - System.arraycopy(y.words, 0, words, 0, y.ival); - ival = y.ival; + realloc(y.ival); + System.arraycopy(y.words, 0, words, 0, y.ival); + ival = y.ival; } } @@ -711,10 +711,10 @@ public class BigInteger extends Number implements Comparable<BigInteger> return valueOf((long) k * (long) y.ival + (long) x.ival); if (k != 1) { - if (k == -1) - y = BigInteger.neg(y); - else - y = BigInteger.times(y, valueOf(k)); + if (k == -1) + y = BigInteger.neg(y); + else + y = BigInteger.times(y, valueOf(k)); } if (x.words == null) return BigInteger.add(y, x.ival); @@ -723,7 +723,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> // Both are big if (y.ival > x.ival) { // Swap so x is longer then y. - BigInteger tmp = x; x = y; y = tmp; + BigInteger tmp = x; x = y; y = tmp; } BigInteger result = alloc(x.ival + 1); int i = y.ival; @@ -731,9 +731,9 @@ public class BigInteger extends Number implements Comparable<BigInteger> long y_ext = y.words[i - 1] < 0 ? 0xffffffffL : 0; for (; i < x.ival; i++) { - carry += ((long) x.words[i] & 0xffffffffL) + y_ext; - result.words[i] = (int) carry; - carry >>>= 32; + carry += ((long) x.words[i] & 0xffffffffL) + y_ext; + result.words[i] = (int) carry; + carry >>>= 32; } if (x.words[i - 1] < 0) y_ext--; @@ -782,16 +782,16 @@ public class BigInteger extends Number implements Comparable<BigInteger> BigInteger result = BigInteger.alloc(xlen + 1); if (xwords[xlen - 1] < 0) { - negative = true; - negate(result.words, xwords, xlen); - xwords = result.words; + negative = true; + negate(result.words, xwords, xlen); + xwords = result.words; } else negative = false; if (y < 0) { - negative = !negative; - y = -y; + negative = !negative; + y = -y; } result.words[xlen] = MPN.mul_1(result.words, xwords, xlen, y); result.ival = xlen + 1; @@ -813,28 +813,28 @@ public class BigInteger extends Number implements Comparable<BigInteger> int ylen = y.ival; if (x.isNegative()) { - negative = true; - xwords = new int[xlen]; - negate(xwords, x.words, xlen); + negative = true; + xwords = new int[xlen]; + negate(xwords, x.words, xlen); } else { - negative = false; - xwords = x.words; + negative = false; + xwords = x.words; } if (y.isNegative()) { - negative = !negative; - ywords = new int[ylen]; - negate(ywords, y.words, ylen); + negative = !negative; + ywords = new int[ylen]; + negate(ywords, y.words, ylen); } else ywords = y.words; // Swap if x is shorter then y. if (xlen < ylen) { - int[] twords = xwords; xwords = ywords; ywords = twords; - int tlen = xlen; xlen = ylen; ylen = tlen; + int[] twords = xwords; xwords = ywords; ywords = twords; + int tlen = xlen; xlen = ylen; ylen = tlen; } BigInteger result = BigInteger.alloc(xlen+ylen); MPN.mul(result.words, xwords, xlen, ywords, ylen); @@ -858,42 +858,42 @@ public class BigInteger extends Number implements Comparable<BigInteger> } private static void divide(long x, long y, - BigInteger quotient, BigInteger remainder, - int rounding_mode) + BigInteger quotient, BigInteger remainder, + int rounding_mode) { boolean xNegative, yNegative; if (x < 0) { - xNegative = true; - if (x == Long.MIN_VALUE) - { - divide(valueOf(x), valueOf(y), - quotient, remainder, rounding_mode); - return; - } - x = -x; + xNegative = true; + if (x == Long.MIN_VALUE) + { + divide(valueOf(x), valueOf(y), + quotient, remainder, rounding_mode); + return; + } + x = -x; } else xNegative = false; if (y < 0) { - yNegative = true; - if (y == Long.MIN_VALUE) - { - if (rounding_mode == TRUNCATE) - { // x != Long.Min_VALUE implies abs(x) < abs(y) - if (quotient != null) - quotient.set(0); - if (remainder != null) - remainder.set(x); - } - else - divide(valueOf(x), valueOf(y), - quotient, remainder, rounding_mode); - return; - } - y = -y; + yNegative = true; + if (y == Long.MIN_VALUE) + { + if (rounding_mode == TRUNCATE) + { // x != Long.Min_VALUE implies abs(x) < abs(y) + if (quotient != null) + quotient.set(0); + if (remainder != null) + remainder.set(x); + } + else + divide(valueOf(x), valueOf(y), + quotient, remainder, rounding_mode); + return; + } + y = -y; } else yNegative = false; @@ -905,47 +905,47 @@ public class BigInteger extends Number implements Comparable<BigInteger> boolean add_one = false; if (r != 0) { - switch (rounding_mode) - { - case TRUNCATE: - break; - case CEILING: - case FLOOR: - if (qNegative == (rounding_mode == FLOOR)) - add_one = true; - break; - case ROUND: - add_one = r > ((y - (q & 1)) >> 1); - break; - } + switch (rounding_mode) + { + case TRUNCATE: + break; + case CEILING: + case FLOOR: + if (qNegative == (rounding_mode == FLOOR)) + add_one = true; + break; + case ROUND: + add_one = r > ((y - (q & 1)) >> 1); + break; + } } if (quotient != null) { - if (add_one) - q++; - if (qNegative) - q = -q; - quotient.set(q); + if (add_one) + q++; + if (qNegative) + q = -q; + quotient.set(q); } if (remainder != null) { - // The remainder is by definition: X-Q*Y - if (add_one) - { - // Subtract the remainder from Y. - r = y - r; - // In this case, abs(Q*Y) > abs(X). - // So sign(remainder) = -sign(X). - xNegative = ! xNegative; - } - else - { - // If !add_one, then: abs(Q*Y) <= abs(X). - // So sign(remainder) = sign(X). - } - if (xNegative) - r = -r; - remainder.set(r); + // The remainder is by definition: X-Q*Y + if (add_one) + { + // Subtract the remainder from Y. + r = y - r; + // In this case, abs(Q*Y) > abs(X). + // So sign(remainder) = -sign(X). + xNegative = ! xNegative; + } + else + { + // If !add_one, then: abs(Q*Y) <= abs(X). + // So sign(remainder) = sign(X). + } + if (xNegative) + r = -r; + remainder.set(r); } } @@ -958,19 +958,19 @@ public class BigInteger extends Number implements Comparable<BigInteger> * @param rounding_mode one of FLOOR, CEILING, TRUNCATE, or ROUND. */ private static void divide(BigInteger x, BigInteger y, - BigInteger quotient, BigInteger remainder, - int rounding_mode) + BigInteger quotient, BigInteger remainder, + int rounding_mode) { if ((x.words == null || x.ival <= 2) - && (y.words == null || y.ival <= 2)) + && (y.words == null || y.ival <= 2)) { - long x_l = x.longValue(); - long y_l = y.longValue(); - if (x_l != Long.MIN_VALUE && y_l != Long.MIN_VALUE) - { - divide(x_l, y_l, quotient, remainder, rounding_mode); - return; - } + long x_l = x.longValue(); + long y_l = y.longValue(); + if (x_l != Long.MIN_VALUE && y_l != Long.MIN_VALUE) + { + divide(x_l, y_l, quotient, remainder, rounding_mode); + return; + } } boolean xNegative = x.isNegative(); @@ -992,57 +992,57 @@ public class BigInteger extends Number implements Comparable<BigInteger> int cmpval = MPN.cmp(xwords, xlen, ywords, ylen); if (cmpval < 0) // abs(x) < abs(y) { // quotient = 0; remainder = num. - int[] rwords = xwords; xwords = ywords; ywords = rwords; - rlen = xlen; qlen = 1; xwords[0] = 0; + int[] rwords = xwords; xwords = ywords; ywords = rwords; + rlen = xlen; qlen = 1; xwords[0] = 0; } else if (cmpval == 0) // abs(x) == abs(y) { - xwords[0] = 1; qlen = 1; // quotient = 1 - ywords[0] = 0; rlen = 1; // remainder = 0; + xwords[0] = 1; qlen = 1; // quotient = 1 + ywords[0] = 0; rlen = 1; // remainder = 0; } else if (ylen == 1) { - qlen = xlen; - // Need to leave room for a word of leading zeros if dividing by 1 - // and the dividend has the high bit set. It might be safe to - // increment qlen in all cases, but it certainly is only necessary - // in the following case. - if (ywords[0] == 1 && xwords[xlen-1] < 0) - qlen++; - rlen = 1; - ywords[0] = MPN.divmod_1(xwords, xwords, xlen, ywords[0]); + qlen = xlen; + // Need to leave room for a word of leading zeros if dividing by 1 + // and the dividend has the high bit set. It might be safe to + // increment qlen in all cases, but it certainly is only necessary + // in the following case. + if (ywords[0] == 1 && xwords[xlen-1] < 0) + qlen++; + rlen = 1; + ywords[0] = MPN.divmod_1(xwords, xwords, xlen, ywords[0]); } else // abs(x) > abs(y) { - // Normalize the denominator, i.e. make its most significant bit set by - // shifting it normalization_steps bits to the left. Also shift the - // numerator the same number of steps (to keep the quotient the same!). - - int nshift = MPN.count_leading_zeros(ywords[ylen - 1]); - if (nshift != 0) - { - // Shift up the denominator setting the most significant bit of - // the most significant word. - MPN.lshift(ywords, 0, ywords, ylen, nshift); - - // Shift up the numerator, possibly introducing a new most - // significant word. - int x_high = MPN.lshift(xwords, 0, xwords, xlen, nshift); - xwords[xlen++] = x_high; - } - - if (xlen == ylen) - xwords[xlen++] = 0; - MPN.divide(xwords, xlen, ywords, ylen); - rlen = ylen; - MPN.rshift0 (ywords, xwords, 0, rlen, nshift); - - qlen = xlen + 1 - ylen; - if (quotient != null) - { - for (int i = 0; i < qlen; i++) - xwords[i] = xwords[i+ylen]; - } + // Normalize the denominator, i.e. make its most significant bit set by + // shifting it normalization_steps bits to the left. Also shift the + // numerator the same number of steps (to keep the quotient the same!). + + int nshift = MPN.count_leading_zeros(ywords[ylen - 1]); + if (nshift != 0) + { + // Shift up the denominator setting the most significant bit of + // the most significant word. + MPN.lshift(ywords, 0, ywords, ylen, nshift); + + // Shift up the numerator, possibly introducing a new most + // significant word. + int x_high = MPN.lshift(xwords, 0, xwords, xlen, nshift); + xwords[xlen++] = x_high; + } + + if (xlen == ylen) + xwords[xlen++] = 0; + MPN.divide(xwords, xlen, ywords, ylen); + rlen = ylen; + MPN.rshift0 (ywords, xwords, 0, rlen, nshift); + + qlen = xlen + 1 - ylen; + if (quotient != null) + { + for (int i = 0; i < qlen; i++) + xwords[i] = xwords[i+ylen]; + } } if (ywords[rlen-1] < 0) @@ -1056,74 +1056,74 @@ public class BigInteger extends Number implements Comparable<BigInteger> boolean add_one = false; if (rlen > 1 || ywords[0] != 0) { // Non-zero remainder i.e. in-exact quotient. - switch (rounding_mode) - { - case TRUNCATE: - break; - case CEILING: - case FLOOR: - if (qNegative == (rounding_mode == FLOOR)) - add_one = true; - break; - case ROUND: - // int cmp = compareTo(remainder<<1, abs(y)); - BigInteger tmp = remainder == null ? new BigInteger() : remainder; - tmp.set(ywords, rlen); - tmp = shift(tmp, 1); - if (yNegative) - tmp.setNegative(); - int cmp = compareTo(tmp, y); - // Now cmp == compareTo(sign(y)*(remainder<<1), y) - if (yNegative) - cmp = -cmp; - add_one = (cmp == 1) || (cmp == 0 && (xwords[0]&1) != 0); - } + switch (rounding_mode) + { + case TRUNCATE: + break; + case CEILING: + case FLOOR: + if (qNegative == (rounding_mode == FLOOR)) + add_one = true; + break; + case ROUND: + // int cmp = compareTo(remainder<<1, abs(y)); + BigInteger tmp = remainder == null ? new BigInteger() : remainder; + tmp.set(ywords, rlen); + tmp = shift(tmp, 1); + if (yNegative) + tmp.setNegative(); + int cmp = compareTo(tmp, y); + // Now cmp == compareTo(sign(y)*(remainder<<1), y) + if (yNegative) + cmp = -cmp; + add_one = (cmp == 1) || (cmp == 0 && (xwords[0]&1) != 0); + } } if (quotient != null) { - quotient.set(xwords, qlen); - if (qNegative) - { - if (add_one) // -(quotient + 1) == ~(quotient) - quotient.setInvert(); - else - quotient.setNegative(); - } - else if (add_one) - quotient.setAdd(1); + quotient.set(xwords, qlen); + if (qNegative) + { + if (add_one) // -(quotient + 1) == ~(quotient) + quotient.setInvert(); + else + quotient.setNegative(); + } + else if (add_one) + quotient.setAdd(1); } if (remainder != null) { - // The remainder is by definition: X-Q*Y - remainder.set(ywords, rlen); - if (add_one) - { - // Subtract the remainder from Y: - // abs(R) = abs(Y) - abs(orig_rem) = -(abs(orig_rem) - abs(Y)). - BigInteger tmp; - if (y.words == null) - { - tmp = remainder; - tmp.set(yNegative ? ywords[0] + y.ival : ywords[0] - y.ival); - } - else - tmp = BigInteger.add(remainder, y, yNegative ? 1 : -1); - // Now tmp <= 0. - // In this case, abs(Q) = 1 + floor(abs(X)/abs(Y)). - // Hence, abs(Q*Y) > abs(X). - // So sign(remainder) = -sign(X). - if (xNegative) - remainder.setNegative(tmp); - else - remainder.set(tmp); - } - else - { - // If !add_one, then: abs(Q*Y) <= abs(X). - // So sign(remainder) = sign(X). - if (xNegative) - remainder.setNegative(); - } + // The remainder is by definition: X-Q*Y + remainder.set(ywords, rlen); + if (add_one) + { + // Subtract the remainder from Y: + // abs(R) = abs(Y) - abs(orig_rem) = -(abs(orig_rem) - abs(Y)). + BigInteger tmp; + if (y.words == null) + { + tmp = remainder; + tmp.set(yNegative ? ywords[0] + y.ival : ywords[0] - y.ival); + } + else + tmp = BigInteger.add(remainder, y, yNegative ? 1 : -1); + // Now tmp <= 0. + // In this case, abs(Q) = 1 + floor(abs(X)/abs(Y)). + // Hence, abs(Q*Y) > abs(X). + // So sign(remainder) = -sign(X). + if (xNegative) + remainder.setNegative(tmp); + else + remainder.set(tmp); + } + else + { + // If !add_one, then: abs(Q*Y) <= abs(X). + // So sign(remainder) = sign(X). + if (xNegative) + remainder.setNegative(); + } } } @@ -1220,9 +1220,9 @@ public class BigInteger extends Number implements Comparable<BigInteger> { if (exponent <= 0) { - if (exponent == 0) - return ONE; - throw new ArithmeticException("negative exponent"); + if (exponent == 0) + return ONE; + throw new ArithmeticException("negative exponent"); } if (USING_NATIVE) @@ -1240,28 +1240,28 @@ public class BigInteger extends Number implements Comparable<BigInteger> int[] pow2 = new int [blen]; int[] rwords = new int [blen]; int[] work = new int [blen]; - getAbsolute(pow2); // pow2 = abs(this); + getAbsolute(pow2); // pow2 = abs(this); int rlen = 1; rwords[0] = 1; // rwords = 1; for (;;) // for (i = 0; ; i++) { - // pow2 == this**(2**i) - // prod = this**(sum(j=0..i-1, (exponent>>j)&1)) - if ((exponent & 1) != 0) - { // r *= pow2 - MPN.mul(work, pow2, plen, rwords, rlen); - int[] temp = work; work = rwords; rwords = temp; - rlen += plen; - while (rwords[rlen - 1] == 0) rlen--; - } - exponent >>= 1; - if (exponent == 0) - break; - // pow2 *= pow2; - MPN.mul(work, pow2, plen, pow2, plen); - int[] temp = work; work = pow2; pow2 = temp; // swap to avoid a copy - plen *= 2; - while (pow2[plen - 1] == 0) plen--; + // pow2 == this**(2**i) + // prod = this**(sum(j=0..i-1, (exponent>>j)&1)) + if ((exponent & 1) != 0) + { // r *= pow2 + MPN.mul(work, pow2, plen, rwords, rlen); + int[] temp = work; work = rwords; rwords = temp; + rlen += plen; + while (rwords[rlen - 1] == 0) rlen--; + } + exponent >>= 1; + if (exponent == 0) + break; + // pow2 *= pow2; + MPN.mul(work, pow2, plen, pow2, plen); + int[] temp = work; work = pow2; pow2 = temp; // swap to avoid a copy + plen *= 2; + while (pow2[plen - 1] == 0) plen--; } if (rwords[rlen - 1] < 0) rlen++; @@ -1276,11 +1276,11 @@ public class BigInteger extends Number implements Comparable<BigInteger> throw new ArithmeticException("not invertible"); if (b == 1) - // Success: values are indeed invertible! - // Bottom of the recursion reached; start unwinding. - return new int[] { -prevDiv, 1 }; + // Success: values are indeed invertible! + // Bottom of the recursion reached; start unwinding. + return new int[] { -prevDiv, 1 }; - int[] xy = euclidInv(b, a % b, a / b); // Recursion happens here. + int[] xy = euclidInv(b, a % b, a / b); // Recursion happens here. a = xy[0]; // use our local copy of 'a' as a work var xy[0] = a * -prevDiv + xy[1]; xy[1] = a; @@ -1295,11 +1295,11 @@ public class BigInteger extends Number implements Comparable<BigInteger> if (b.isOne()) { - // Success: values are indeed invertible! - // Bottom of the recursion reached; start unwinding. - xy[0] = neg(prevDiv); + // Success: values are indeed invertible! + // Bottom of the recursion reached; start unwinding. + xy[0] = neg(prevDiv); xy[1] = ONE; - return; + return; } // Recursion happens in the following conditional! @@ -1308,18 +1308,18 @@ public class BigInteger extends Number implements Comparable<BigInteger> if (a.words == null) { int[] xyInt = euclidInv(b.ival, a.ival % b.ival, a.ival / b.ival); - xy[0] = new BigInteger(xyInt[0]); + xy[0] = new BigInteger(xyInt[0]); xy[1] = new BigInteger(xyInt[1]); } else { - BigInteger rem = new BigInteger(); - BigInteger quot = new BigInteger(); - divide(a, b, quot, rem, FLOOR); + BigInteger rem = new BigInteger(); + BigInteger quot = new BigInteger(); + divide(a, b, quot, rem, FLOOR); // quot and rem may not be in canonical form. ensure rem.canonicalize(); quot.canonicalize(); - euclidInv(b, rem, quot, xy); + euclidInv(b, rem, quot, xy); } BigInteger t = xy[0]; @@ -1358,61 +1358,61 @@ public class BigInteger extends Number implements Comparable<BigInteger> if (y.words == null) { - // The result is guaranteed to be less than the modulus, y (which is - // an int), so simplify this by working with the int result of this - // modulo y. Also, if this is negative, make it positive via modulo - // math. Note that BigInteger.mod() must be used even if this is - // already an int as the % operator would provide a negative result if - // this is negative, BigInteger.mod() never returns negative values. + // The result is guaranteed to be less than the modulus, y (which is + // an int), so simplify this by working with the int result of this + // modulo y. Also, if this is negative, make it positive via modulo + // math. Note that BigInteger.mod() must be used even if this is + // already an int as the % operator would provide a negative result if + // this is negative, BigInteger.mod() never returns negative values. int xval = (words != null || isNegative()) ? mod(y).ival : ival; int yval = y.ival; - // Swap values so x > y. - if (yval > xval) - { - int tmp = xval; xval = yval; yval = tmp; - swapped = true; - } - // Normally, the result is in the 2nd element of the array, but - // if originally x < y, then x and y were swapped and the result - // is in the 1st element of the array. - result.ival = - euclidInv(yval, xval % yval, xval / yval)[swapped ? 0 : 1]; - - // Result can't be negative, so make it positive by adding the - // original modulus, y.ival (not the possibly "swapped" yval). - if (result.ival < 0) - result.ival += y.ival; + // Swap values so x > y. + if (yval > xval) + { + int tmp = xval; xval = yval; yval = tmp; + swapped = true; + } + // Normally, the result is in the 2nd element of the array, but + // if originally x < y, then x and y were swapped and the result + // is in the 1st element of the array. + result.ival = + euclidInv(yval, xval % yval, xval / yval)[swapped ? 0 : 1]; + + // Result can't be negative, so make it positive by adding the + // original modulus, y.ival (not the possibly "swapped" yval). + if (result.ival < 0) + result.ival += y.ival; } else { - // As above, force this to be a positive value via modulo math. - BigInteger x = isNegative() ? this.mod(y) : this; - - // Swap values so x > y. - if (x.compareTo(y) < 0) - { - result = x; x = y; y = result; // use 'result' as a work var - swapped = true; - } - // As above (for ints), result will be in the 2nd element unless - // the original x and y were swapped. - BigInteger rem = new BigInteger(); - BigInteger quot = new BigInteger(); - divide(x, y, quot, rem, FLOOR); + // As above, force this to be a positive value via modulo math. + BigInteger x = isNegative() ? this.mod(y) : this; + + // Swap values so x > y. + if (x.compareTo(y) < 0) + { + result = x; x = y; y = result; // use 'result' as a work var + swapped = true; + } + // As above (for ints), result will be in the 2nd element unless + // the original x and y were swapped. + BigInteger rem = new BigInteger(); + BigInteger quot = new BigInteger(); + divide(x, y, quot, rem, FLOOR); // quot and rem may not be in canonical form. ensure rem.canonicalize(); quot.canonicalize(); - BigInteger[] xy = new BigInteger[2]; - euclidInv(y, rem, quot, xy); - result = swapped ? xy[0] : xy[1]; + BigInteger[] xy = new BigInteger[2]; + euclidInv(y, rem, quot, xy); + result = swapped ? xy[0] : xy[1]; - // Result can't be negative, so make it positive by adding the - // original modulus, y (which is now x if they were swapped). - if (result.isNegative()) - result = add(result, swapped ? x : y, 1); + // Result can't be negative, so make it positive by adding the + // original modulus, y (which is now x if they were swapped). + if (result.isNegative()) + result = add(result, swapped ? x : y, 1); } - + return result; } @@ -1450,10 +1450,10 @@ public class BigInteger extends Number implements Comparable<BigInteger> while (!u.isZero()) { - if (u.and(ONE).isOne()) - s = times(s, t).mod(m); - u = u.shiftRight(1); - t = times(t, t).mod(m); + if (u.and(ONE).isOne()) + s = times(s, t).mod(m); + u = u.shiftRight(1); + t = times(t, t).mod(m); } return s; @@ -1466,18 +1466,18 @@ public class BigInteger extends Number implements Comparable<BigInteger> int tmp; if (b > a) { - tmp = a; a = b; b = tmp; + tmp = a; a = b; b = tmp; } for(;;) { - if (b == 0) - return a; + if (b == 0) + return a; if (b == 1) - return b; + return b; tmp = b; - b = a % b; - a = tmp; - } + b = a % b; + a = tmp; + } } public BigInteger gcd(BigInteger y) @@ -1494,24 +1494,24 @@ public class BigInteger extends Number implements Comparable<BigInteger> int yval = y.ival; if (words == null) { - if (xval == 0) - return abs(y); - if (y.words == null - && xval != Integer.MIN_VALUE && yval != Integer.MIN_VALUE) - { - if (xval < 0) - xval = -xval; - if (yval < 0) - yval = -yval; - return valueOf(gcd(xval, yval)); - } - xval = 1; + if (xval == 0) + return abs(y); + if (y.words == null + && xval != Integer.MIN_VALUE && yval != Integer.MIN_VALUE) + { + if (xval < 0) + xval = -xval; + if (yval < 0) + yval = -yval; + return valueOf(gcd(xval, yval)); + } + xval = 1; } if (y.words == null) { - if (yval == 0) - return abs(this); - yval = 1; + if (yval == 0) + return abs(this); + yval = 1; } int len = (xval > yval ? xval : yval) + 1; int[] xwords = new int[len]; @@ -1562,12 +1562,12 @@ public class BigInteger extends Number implements Comparable<BigInteger> int i; for (i = 0; i < primes.length; i++) { - if (words == null && ival == primes[i]) - return true; + if (words == null && ival == primes[i]) + return true; divide(this, smallFixNums[primes[i] - minFixNum], null, rem, TRUNCATE); if (rem.canonicalize().isZero()) - return false; + return false; } // Now perform the Rabin-Miller test. @@ -1599,23 +1599,23 @@ public class BigInteger extends Number implements Comparable<BigInteger> // Remark 4.28 states: "...A strategy that is sometimes employed // is to fix the bases a to be the first few primes instead of // choosing them at random. - z = smallFixNums[primes[t] - minFixNum].modPow(m, this); - if (z.isOne() || z.equals(pMinus1)) - continue; // Passes the test; may be prime. + z = smallFixNums[primes[t] - minFixNum].modPow(m, this); + if (z.isOne() || z.equals(pMinus1)) + continue; // Passes the test; may be prime. - for (i = 0; i < b; ) - { - if (z.isOne()) - return false; - i++; - if (z.equals(pMinus1)) - break; // Passes the test; may be prime. + for (i = 0; i < b; ) + { + if (z.isOne()) + return false; + i++; + if (z.equals(pMinus1)) + break; // Passes the test; may be prime. - z = z.modPow(valueOf(2), this); - } + z = z.modPow(valueOf(2), this); + } - if (i == b && !z.equals(pMinus1)) - return false; + if (i == b && !z.equals(pMinus1)) + return false; } return true; } @@ -1626,8 +1626,8 @@ public class BigInteger extends Number implements Comparable<BigInteger> ival = ~ival; else { - for (int i = ival; --i >= 0; ) - words[i] = ~words[i]; + for (int i = ival; --i >= 0; ) + words[i] = ~words[i]; } } @@ -1637,36 +1637,36 @@ public class BigInteger extends Number implements Comparable<BigInteger> int xlen; if (x.words == null) { - if (count < 32) - { - set((long) x.ival << count); - return; - } - xwords = new int[1]; - xwords[0] = x.ival; - xlen = 1; + if (count < 32) + { + set((long) x.ival << count); + return; + } + xwords = new int[1]; + xwords[0] = x.ival; + xlen = 1; } else { - xwords = x.words; - xlen = x.ival; + xwords = x.words; + xlen = x.ival; } int word_count = count >> 5; count &= 31; int new_len = xlen + word_count; if (count == 0) { - realloc(new_len); - for (int i = xlen; --i >= 0; ) - words[i+word_count] = xwords[i]; + realloc(new_len); + for (int i = xlen; --i >= 0; ) + words[i+word_count] = xwords[i]; } else { - new_len++; - realloc(new_len); - int shift_out = MPN.lshift(words, word_count, xwords, xlen, count); - count = 32 - count; - words[new_len-1] = (shift_out << count) >> count; // sign-extend. + new_len++; + realloc(new_len); + int shift_out = MPN.lshift(words, word_count, xwords, xlen, count); + count = 32 - count; + words[new_len-1] = (shift_out << count) >> count; // sign-extend. } ival = new_len; for (int i = word_count; --i >= 0; ) @@ -1681,21 +1681,21 @@ public class BigInteger extends Number implements Comparable<BigInteger> set(x); else { - boolean neg = x.isNegative(); - int word_count = count >> 5; - count &= 31; - int d_len = x.ival - word_count; - if (d_len <= 0) - set(neg ? -1 : 0); - else - { - if (words == null || words.length < d_len) - realloc(d_len); - MPN.rshift0 (words, x.words, word_count, d_len, count); - ival = d_len; - if (neg) - words[d_len-1] |= -2 << (31 - count); - } + boolean neg = x.isNegative(); + int word_count = count >> 5; + count &= 31; + int d_len = x.ival - word_count; + if (d_len <= 0) + set(neg ? -1 : 0); + else + { + if (words == null || words.length < d_len) + realloc(d_len); + MPN.rshift0 (words, x.words, word_count, d_len, count); + ival = d_len; + if (neg) + words[d_len-1] |= -2 << (31 - count); + } } } @@ -1711,10 +1711,10 @@ public class BigInteger extends Number implements Comparable<BigInteger> { if (x.words == null) { - if (count <= 0) - return valueOf(count > -32 ? x.ival >> (-count) : x.ival < 0 ? -1 : 0); - if (count < 32) - return valueOf((long) x.ival << count); + if (count <= 0) + return valueOf(count > -32 ? x.ival >> (-count) : x.ival < 0 ? -1 : 0); + if (count < 32) + return valueOf((long) x.ival << count); } if (count == 0) return x; @@ -1767,57 +1767,57 @@ public class BigInteger extends Number implements Comparable<BigInteger> buffer.append(Long.toString(longValue(), radix)); else { - boolean neg = isNegative(); - int[] work; - if (neg || radix != 16) - { - work = new int[ival]; - getAbsolute(work); - } - else - work = words; - int len = ival; - - if (radix == 16) - { - if (neg) - buffer.append('-'); - int buf_start = buffer.length(); - for (int i = len; --i >= 0; ) - { - int word = work[i]; - for (int j = 8; --j >= 0; ) - { - int hex_digit = (word >> (4 * j)) & 0xF; - // Suppress leading zeros: - if (hex_digit > 0 || buffer.length() > buf_start) - buffer.append(Character.forDigit(hex_digit, 16)); - } - } - } - else - { - int i = buffer.length(); - for (;;) - { - int digit = MPN.divmod_1(work, work, len, radix); - buffer.append(Character.forDigit(digit, radix)); - while (len > 0 && work[len-1] == 0) len--; - if (len == 0) - break; - } - if (neg) - buffer.append('-'); - /* Reverse buffer. */ - int j = buffer.length() - 1; - while (i < j) - { - char tmp = buffer.charAt(i); - buffer.setCharAt(i, buffer.charAt(j)); - buffer.setCharAt(j, tmp); - i++; j--; - } - } + boolean neg = isNegative(); + int[] work; + if (neg || radix != 16) + { + work = new int[ival]; + getAbsolute(work); + } + else + work = words; + int len = ival; + + if (radix == 16) + { + if (neg) + buffer.append('-'); + int buf_start = buffer.length(); + for (int i = len; --i >= 0; ) + { + int word = work[i]; + for (int j = 8; --j >= 0; ) + { + int hex_digit = (word >> (4 * j)) & 0xF; + // Suppress leading zeros: + if (hex_digit > 0 || buffer.length() > buf_start) + buffer.append(Character.forDigit(hex_digit, 16)); + } + } + } + else + { + int i = buffer.length(); + for (;;) + { + int digit = MPN.divmod_1(work, work, len, radix); + buffer.append(Character.forDigit(digit, radix)); + while (len > 0 && work[len-1] == 0) len--; + if (len == 0) + break; + } + if (neg) + buffer.append('-'); + /* Reverse buffer. */ + int j = buffer.length() - 1; + while (i < j) + { + char tmp = buffer.charAt(i); + buffer.setCharAt(i, buffer.charAt(j)); + buffer.setCharAt(j, tmp); + i++; j--; + } + } } } @@ -1900,8 +1900,8 @@ public class BigInteger extends Number implements Comparable<BigInteger> return false; for (int i = x.ival; --i >= 0; ) { - if (x.words[i] != y.words[i]) - return false; + if (x.words[i] != y.words[i]) + return false; } return true; } @@ -1915,7 +1915,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> } private static BigInteger valueOf(byte[] digits, int byte_len, - boolean negative, int radix) + boolean negative, int radix) { int chars_per_word = MPN.chars_per_word(radix); int[] words = new int[byte_len / chars_per_word + 1]; @@ -1959,7 +1959,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> int i; for (i = 0; i < (n >> 5) ; i++) if (words[i] != 0) - return true; + return true; return (n & 31) != 0 && (words[i] & ((1 << (n & 31)) - 1)) != 0; } @@ -2000,7 +2000,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> int excess_bits = il - (ml + 1); if (excess_bits > 0) m = ((words == null) ? ival >> excess_bits - : MPN.rshift_long(words, ival, excess_bits)); + : MPN.rshift_long(words, ival, excess_bits)); else m = longValue() << (- excess_bits); @@ -2008,31 +2008,31 @@ public class BigInteger extends Number implements Comparable<BigInteger> // any amount, even if it's less than half a step, it overflows. if (exp == 1023 && ((m >> 1) == (1L << 53) - 1)) { - if (remainder || checkBits(il - ml)) - return neg ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; - else - return neg ? - Double.MAX_VALUE : Double.MAX_VALUE; + if (remainder || checkBits(il - ml)) + return neg ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; + else + return neg ? - Double.MAX_VALUE : Double.MAX_VALUE; } // Normal round-to-even rule: round up if the bit dropped is a one, and // the bit above it or any of the bits below it is a one. if ((m & 1) == 1 - && ((m & 2) == 2 || remainder || checkBits(excess_bits))) - { - m += 2; - // Check if we overflowed the mantissa - if ((m & (1L << 54)) != 0) - { - exp++; - // renormalize - m >>= 1; - } - // Check if a denormalized mantissa was just rounded up to a - // normalized one. - else if (ml == 52 && (m & (1L << 53)) != 0) - exp++; - } - + && ((m & 2) == 2 || remainder || checkBits(excess_bits))) + { + m += 2; + // Check if we overflowed the mantissa + if ((m & (1L << 54)) != 0) + { + exp++; + // renormalize + m >>= 1; + } + // Check if a denormalized mantissa was just rounded up to a + // normalized one. + else if (ml == 52 && (m & (1L << 53)) != 0) + exp++; + } + // Discard the rounding bit m >>= 1; @@ -2052,14 +2052,14 @@ public class BigInteger extends Number implements Comparable<BigInteger> int len; if (this.words == null) { - len = 1; - words[0] = this.ival; + len = 1; + words[0] = this.ival; } else { - len = this.ival; - for (int i = len; --i >= 0; ) - words[i] = this.words[i]; + len = this.ival; + for (int i = len; --i >= 0; ) + words[i] = this.words[i]; } if (words[len - 1] < 0) negate(words, words, len); @@ -2090,11 +2090,11 @@ public class BigInteger extends Number implements Comparable<BigInteger> int len = x.ival; if (x.words == null) { - if (len == Integer.MIN_VALUE) - set(- (long) len); - else - set(-len); - return; + if (len == Integer.MIN_VALUE) + set(- (long) len); + else + set(-len); + return; } realloc(len + 1); if (negate(words, x.words, len)) @@ -2139,7 +2139,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> if (USING_NATIVE) { BigInteger result = new BigInteger(); - mpz.negate(result.mpz); + mpz.negate(result.mpz); return result; } @@ -2193,8 +2193,8 @@ public class BigInteger extends Number implements Comparable<BigInteger> // If BigInteger is an int, then it is in ival and nbytes will be <= 4. while (nbytes > 4) { - word = words[wptr++]; - for (int i = 4; i > 0; --i, word >>= 8) + word = words[wptr++]; + for (int i = 4; i > 0; --i, word >>= 8) bytes[--nbytes] = (byte) word; } @@ -2234,35 +2234,35 @@ public class BigInteger extends Number implements Comparable<BigInteger> /** Do one the the 16 possible bit-wise operations of two BigIntegers. */ private static void setBitOp(BigInteger result, int op, - BigInteger x, BigInteger y) + BigInteger x, BigInteger y) { if ((y.words != null) && (x.words == null || x.ival < y.ival)) { - BigInteger temp = x; x = y; y = temp; - op = swappedOp(op); + BigInteger temp = x; x = y; y = temp; + op = swappedOp(op); } int xi; int yi; int xlen, ylen; if (y.words == null) { - yi = y.ival; - ylen = 1; + yi = y.ival; + ylen = 1; } else { - yi = y.words[0]; - ylen = y.ival; + yi = y.words[0]; + ylen = y.ival; } if (x.words == null) { - xi = x.ival; - xlen = 1; + xi = x.ival; + xlen = 1; } else { - xi = x.words[0]; - xlen = x.ival; + xi = x.words[0]; + xlen = x.ival; } if (xlen > 1) result.realloc(xlen); @@ -2277,126 +2277,126 @@ public class BigInteger extends Number implements Comparable<BigInteger> switch (op) { case 0: // clr - ni = 0; - break; + ni = 0; + break; case 1: // and - for (;;) - { - ni = xi & yi; - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - if (yi < 0) finish = 1; - break; + for (;;) + { + ni = xi & yi; + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + if (yi < 0) finish = 1; + break; case 2: // andc2 - for (;;) - { - ni = xi & ~yi; - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - if (yi >= 0) finish = 1; - break; + for (;;) + { + ni = xi & ~yi; + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + if (yi >= 0) finish = 1; + break; case 3: // copy x - ni = xi; - finish = 1; // Copy rest - break; + ni = xi; + finish = 1; // Copy rest + break; case 4: // andc1 - for (;;) - { - ni = ~xi & yi; - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - if (yi < 0) finish = 2; - break; + for (;;) + { + ni = ~xi & yi; + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + if (yi < 0) finish = 2; + break; case 5: // copy y - for (;;) - { - ni = yi; - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - break; + for (;;) + { + ni = yi; + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + break; case 6: // xor - for (;;) - { - ni = xi ^ yi; - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - finish = yi < 0 ? 2 : 1; - break; + for (;;) + { + ni = xi ^ yi; + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + finish = yi < 0 ? 2 : 1; + break; case 7: // ior - for (;;) - { - ni = xi | yi; - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - if (yi >= 0) finish = 1; - break; + for (;;) + { + ni = xi | yi; + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + if (yi >= 0) finish = 1; + break; case 8: // nor - for (;;) - { - ni = ~(xi | yi); - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - if (yi >= 0) finish = 2; - break; + for (;;) + { + ni = ~(xi | yi); + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + if (yi >= 0) finish = 2; + break; case 9: // eqv [exclusive nor] - for (;;) - { - ni = ~(xi ^ yi); - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - finish = yi >= 0 ? 2 : 1; - break; + for (;;) + { + ni = ~(xi ^ yi); + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + finish = yi >= 0 ? 2 : 1; + break; case 10: // c2 - for (;;) - { - ni = ~yi; - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - break; + for (;;) + { + ni = ~yi; + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + break; case 11: // orc2 - for (;;) - { - ni = xi | ~yi; - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - if (yi < 0) finish = 1; - break; + for (;;) + { + ni = xi | ~yi; + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + if (yi < 0) finish = 1; + break; case 12: // c1 - ni = ~xi; - finish = 2; - break; + ni = ~xi; + finish = 2; + break; case 13: // orc1 - for (;;) - { - ni = ~xi | yi; - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - if (yi >= 0) finish = 2; - break; + for (;;) + { + ni = ~xi | yi; + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + if (yi >= 0) finish = 2; + break; case 14: // nand - for (;;) - { - ni = ~(xi & yi); - if (i+1 >= ylen) break; - w[i++] = ni; xi = x.words[i]; yi = y.words[i]; - } - if (yi < 0) finish = 2; - break; + for (;;) + { + ni = ~(xi & yi); + if (i+1 >= ylen) break; + w[i++] = ni; xi = x.words[i]; yi = y.words[i]; + } + if (yi < 0) finish = 2; + break; default: case 15: // set - ni = -1; - break; + ni = -1; + break; } // Here i==ylen-1; w[0]..w[i-1] have the correct result; // and ni contains the correct result for w[i+1]. @@ -2405,13 +2405,13 @@ public class BigInteger extends Number implements Comparable<BigInteger> switch (finish) { case 0: - if (i == 0 && w == null) - { - result.ival = ni; - return; - } - w[i++] = ni; - break; + if (i == 0 && w == null) + { + result.ival = ni; + return; + } + w[i++] = ni; + break; case 1: w[i] = ni; while (++i < xlen) w[i] = x.words[i]; break; case 2: w[i] = ni; while (++i < xlen) w[i] = ~x.words[i]; break; } @@ -2590,15 +2590,15 @@ public class BigInteger extends Number implements Comparable<BigInteger> // bit4count[I] is number of '1' bits in I. private static final byte[] bit4_count = { 0, 1, 1, 2, 1, 2, 2, 3, - 1, 2, 2, 3, 2, 3, 3, 4}; + 1, 2, 2, 3, 2, 3, 3, 4}; private static int bitCount(int i) { int count = 0; while (i != 0) { - count += bit4_count[i & 15]; - i >>>= 4; + count += bit4_count[i & 15]; + i >>>= 4; } return count; } @@ -2622,13 +2622,13 @@ public class BigInteger extends Number implements Comparable<BigInteger> int[] x_words = words; if (x_words == null) { - x_len = 1; - i = bitCount(ival); + x_len = 1; + i = bitCount(ival); } else { - x_len = ival; - i = bitCount(x_words, x_len); + x_len = ival; + i = bitCount(x_words, x_len); } return isNegative() ? x_len * 32 - i : i; } @@ -2646,19 +2646,19 @@ public class BigInteger extends Number implements Comparable<BigInteger> } else { - s.defaultReadObject(); - if (magnitude.length == 0 || signum == 0) - { - this.ival = 0; - this.words = null; - } - else - { - words = byteArrayToIntArray(magnitude, signum < 0 ? -1 : 0); - BigInteger result = make(words, words.length); - this.ival = result.ival; - this.words = result.words; - } + s.defaultReadObject(); + if (magnitude.length == 0 || signum == 0) + { + this.ival = 0; + this.words = null; + } + else + { + words = byteArrayToIntArray(magnitude, signum < 0 ? -1 : 0); + BigInteger result = make(words, words.length); + this.ival = result.ival; + this.words = result.words; + } } } diff --git a/libjava/classpath/java/math/MathContext.java b/libjava/classpath/java/math/MathContext.java index 533ab13acf3..16114954031 100644 --- a/libjava/classpath/java/math/MathContext.java +++ b/libjava/classpath/java/math/MathContext.java @@ -1,4 +1,4 @@ -/* MathContext.java -- +/* MathContext.java -- Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -49,58 +49,58 @@ import java.io.Serializable; public final class MathContext implements Serializable { /** A MathContext for unlimited precision arithmetic * */ - public static final MathContext UNLIMITED = + public static final MathContext UNLIMITED = new MathContext(0, RoundingMode.HALF_UP); - + /** * A MathContext for the IEEE 754R Decimal32 format - 7 digit preicision and * HALF_EVEN rounding. */ - public static final MathContext DECIMAL32 = + public static final MathContext DECIMAL32 = new MathContext(7, RoundingMode.HALF_EVEN); - + /** * A MathContext for the IEEE 754R Decimal64 format - 16 digit preicision and * HALF_EVEN rounding. */ - public static final MathContext DECIMAL64 = + public static final MathContext DECIMAL64 = new MathContext(16, RoundingMode.HALF_EVEN); - + /** * A MathContext for the IEEE 754R Decimal128 format - 34 digit preicision and * HALF_EVEN rounding. */ - public static final MathContext DECIMAL128 = + public static final MathContext DECIMAL128 = new MathContext(34, RoundingMode.HALF_EVEN); - + /** * This is the serialVersionUID reported here: * java.sun.com/j2se/1.5.0/docs/api/serialized-form.html#java.math.MathContext */ private static final long serialVersionUID = 5579720004786848255L; - + private int precision; - + private RoundingMode roundMode; - + /** * Constructs a new MathContext with the specified precision and with HALF_UP * rounding. * @param setPrecision the precision for the new MathContext - * + * * @throws IllegalArgumentException if precision is < 0. */ public MathContext(int setPrecision) { this(setPrecision, RoundingMode.HALF_UP); } - + /** * Constructs a new MathContext with the specified precision and rounding * mode. * @param setPrecision the precision * @param setRoundingMode the rounding mode - * + * * @throws IllegalArgumentException if precision is < 0. */ public MathContext(int setPrecision, RoundingMode setRoundingMode) @@ -110,12 +110,12 @@ public final class MathContext implements Serializable precision = setPrecision; roundMode = setRoundingMode; } - + /** * Constructs a MathContext from a String that has the same form as one * produced by the toString() method. * @param val - * + * * @throws IllegalArgumentException if the String is not in the correct * format or if the precision specified is < 0. */ @@ -138,11 +138,11 @@ public final class MathContext implements Serializable if (precision < 0) throw new IllegalArgumentException("Precision cannot be less than 0."); } - + /** * Returns true if x is a MathContext and has the same precision setting * and rounding mode as this MathContext. - * + * * @return true if the above conditions hold */ public boolean equals(Object x) @@ -153,7 +153,7 @@ public final class MathContext implements Serializable return mc.precision == this.precision && mc.roundMode.equals(this.roundMode); } - + /** * Returns the precision setting. * @return the precision setting. @@ -162,11 +162,11 @@ public final class MathContext implements Serializable { return precision; } - + /** - * Returns the rounding mode setting. This will be one of - * RoundingMode.CEILING, RoundingMode.DOWN, RoundingMode.FLOOR, - * RoundingMode.HALF_DOWN, RoundingMode.HALF_EVEN, RoundingMode.HALF_UP, + * Returns the rounding mode setting. This will be one of + * RoundingMode.CEILING, RoundingMode.DOWN, RoundingMode.FLOOR, + * RoundingMode.HALF_DOWN, RoundingMode.HALF_EVEN, RoundingMode.HALF_UP, * RoundingMode.UNNECESSARY, or RoundingMode.UP. * @return the rounding mode setting. */ @@ -174,19 +174,19 @@ public final class MathContext implements Serializable { return roundMode; } - + /** - * Returns "precision=p roundingMode=MODE" where p is an int giving the + * Returns "precision=p roundingMode=MODE" where p is an int giving the * precision and MODE is UP, DOWN, HALF_UP, HALF_DOWN, HALF_EVEN, CEILING, * FLOOR, or UNNECESSARY corresponding to rounding modes. - * + * * @return a String describing this MathContext */ public String toString() { return "precision="+precision+" roundingMode="+roundMode; } - + /** * Returns the hashcode for this MathContext. * @return the hashcode for this MathContext. diff --git a/libjava/classpath/java/math/RoundingMode.java b/libjava/classpath/java/math/RoundingMode.java index c85bf4ff533..140f0a36e4c 100644 --- a/libjava/classpath/java/math/RoundingMode.java +++ b/libjava/classpath/java/math/RoundingMode.java @@ -1,4 +1,4 @@ -/* RoundingMode.java -- An Enum to replace BigDecimal rounding constants. +/* RoundingMode.java -- An Enum to replace BigDecimal rounding constants. Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -52,7 +52,7 @@ public enum RoundingMode * For compatability with Sun's JDK */ private static final long serialVersionUID = 432302042773881265L; - + /** * Returns the RoundingMode object corresponding to the legacy rounding modes * in BigDecimal. @@ -80,9 +80,9 @@ public enum RoundingMode case BigDecimal.ROUND_UNNECESSARY: return UNNECESSARY; default: - throw new - IllegalArgumentException("invalid argument: " + rm + - ". Argument should be one of the " + + throw new + IllegalArgumentException("invalid argument: " + rm + + ". Argument should be one of the " + "rounding modes defined in BigDecimal."); } } |