feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,212 @@
/*
* Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.math;
/**
* A simple bit sieve used for finding prime number candidates. Allows setting
* and clearing of bits in a storage array. The size of the sieve is assumed to
* be constant to reduce overhead. All the bits of a new bitSieve are zero, and
* bits are removed from it by setting them.
*
* To reduce storage space and increase efficiency, no even numbers are
* represented in the sieve (each bit in the sieve represents an odd number).
* The relationship between the index of a bit and the number it represents is
* given by
* N = offset + (2*index + 1);
* Where N is the integer represented by a bit in the sieve, offset is some
* even integer offset indicating where the sieve begins, and index is the
* index of a bit in the sieve array.
*
* @see BigInteger
* @author Michael McCloskey
* @since 1.3
*/
class BitSieve {
/**
* Stores the bits in this bitSieve.
*/
private long bits[];
/**
* Length is how many bits this sieve holds.
*/
private int length;
/**
* A small sieve used to filter out multiples of small primes in a search
* sieve.
*/
private static BitSieve smallSieve = new BitSieve();
/**
* Construct a "small sieve" with a base of 0. This constructor is
* used internally to generate the set of "small primes" whose multiples
* are excluded from sieves generated by the main (package private)
* constructor, BitSieve(BigInteger base, int searchLen). The length
* of the sieve generated by this constructor was chosen for performance;
* it controls a tradeoff between how much time is spent constructing
* other sieves, and how much time is wasted testing composite candidates
* for primality. The length was chosen experimentally to yield good
* performance.
*/
private BitSieve() {
length = 150 * 64;
bits = new long[(unitIndex(length - 1) + 1)];
// Mark 1 as composite
set(0);
int nextIndex = 1;
int nextPrime = 3;
// Find primes and remove their multiples from sieve
do {
sieveSingle(length, nextIndex + nextPrime, nextPrime);
nextIndex = sieveSearch(length, nextIndex + 1);
nextPrime = 2*nextIndex + 1;
} while((nextIndex > 0) && (nextPrime < length));
}
/**
* Construct a bit sieve of searchLen bits used for finding prime number
* candidates. The new sieve begins at the specified base, which must
* be even.
*/
BitSieve(BigInteger base, int searchLen) {
/*
* Candidates are indicated by clear bits in the sieve. As a candidates
* nonprimality is calculated, a bit is set in the sieve to eliminate
* it. To reduce storage space and increase efficiency, no even numbers
* are represented in the sieve (each bit in the sieve represents an
* odd number).
*/
bits = new long[(unitIndex(searchLen-1) + 1)];
length = searchLen;
int start = 0;
int step = smallSieve.sieveSearch(smallSieve.length, start);
int convertedStep = (step *2) + 1;
// Construct the large sieve at an even offset specified by base
MutableBigInteger b = new MutableBigInteger(base);
MutableBigInteger q = new MutableBigInteger();
do {
// Calculate base mod convertedStep
start = b.divideOneWord(convertedStep, q);
// Take each multiple of step out of sieve
start = convertedStep - start;
if (start%2 == 0)
start += convertedStep;
sieveSingle(searchLen, (start-1)/2, convertedStep);
// Find next prime from small sieve
step = smallSieve.sieveSearch(smallSieve.length, step+1);
convertedStep = (step *2) + 1;
} while (step > 0);
}
/**
* Given a bit index return unit index containing it.
*/
private static int unitIndex(int bitIndex) {
return bitIndex >>> 6;
}
/**
* Return a unit that masks the specified bit in its unit.
*/
private static long bit(int bitIndex) {
return 1L << (bitIndex & ((1<<6) - 1));
}
/**
* Get the value of the bit at the specified index.
*/
private boolean get(int bitIndex) {
int unitIndex = unitIndex(bitIndex);
return ((bits[unitIndex] & bit(bitIndex)) != 0);
}
/**
* Set the bit at the specified index.
*/
private void set(int bitIndex) {
int unitIndex = unitIndex(bitIndex);
bits[unitIndex] |= bit(bitIndex);
}
/**
* This method returns the index of the first clear bit in the search
* array that occurs at or after start. It will not search past the
* specified limit. It returns -1 if there is no such clear bit.
*/
private int sieveSearch(int limit, int start) {
if (start >= limit)
return -1;
int index = start;
do {
if (!get(index))
return index;
index++;
} while(index < limit-1);
return -1;
}
/**
* Sieve a single set of multiples out of the sieve. Begin to remove
* multiples of the specified step starting at the specified start index,
* up to the specified limit.
*/
private void sieveSingle(int limit, int start, int step) {
while(start < limit) {
set(start);
start += step;
}
}
/**
* Test probable primes in the sieve and return successful candidates.
*/
BigInteger retrieve(BigInteger initValue, int certainty, java.util.Random random) {
// Examine the sieve one long at a time to find possible primes
int offset = 1;
for (int i=0; i<bits.length; i++) {
long nextLong = ~bits[i];
for (int j=0; j<64; j++) {
if ((nextLong & 1) == 1) {
BigInteger candidate = initValue.add(
BigInteger.valueOf(offset));
if (candidate.primeToCertainty(certainty, random))
return candidate;
}
nextLong >>>= 1;
offset+=2;
}
}
return null;
}
}

View File

@@ -0,0 +1,326 @@
/*
* Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Portions Copyright IBM Corporation, 1997, 2001. All Rights Reserved.
*/
package java.math;
import java.io.*;
/**
* Immutable objects which encapsulate the context settings which
* describe certain rules for numerical operators, such as those
* implemented by the {@link BigDecimal} class.
*
* <p>The base-independent settings are:
* <ol>
* <li>{@code precision}:
* the number of digits to be used for an operation; results are
* rounded to this precision
*
* <li>{@code roundingMode}:
* a {@link RoundingMode} object which specifies the algorithm to be
* used for rounding.
* </ol>
*
* @see BigDecimal
* @see RoundingMode
* @author Mike Cowlishaw
* @author Joseph D. Darcy
* @since 1.5
*/
public final class MathContext implements Serializable {
/* ----- Constants ----- */
// defaults for constructors
private static final int DEFAULT_DIGITS = 9;
private static final RoundingMode DEFAULT_ROUNDINGMODE = RoundingMode.HALF_UP;
// Smallest values for digits (Maximum is Integer.MAX_VALUE)
private static final int MIN_DIGITS = 0;
// Serialization version
private static final long serialVersionUID = 5579720004786848255L;
/* ----- Public Properties ----- */
/**
* A {@code MathContext} object whose settings have the values
* required for unlimited precision arithmetic.
* The values of the settings are:
* <code>
* precision=0 roundingMode=HALF_UP
* </code>
*/
public static final MathContext UNLIMITED =
new MathContext(0, RoundingMode.HALF_UP);
/**
* A {@code MathContext} object with a precision setting
* matching the IEEE 754R Decimal32 format, 7 digits, and a
* rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
* IEEE 754R default.
*/
public static final MathContext DECIMAL32 =
new MathContext(7, RoundingMode.HALF_EVEN);
/**
* A {@code MathContext} object with a precision setting
* matching the IEEE 754R Decimal64 format, 16 digits, and a
* rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
* IEEE 754R default.
*/
public static final MathContext DECIMAL64 =
new MathContext(16, RoundingMode.HALF_EVEN);
/**
* A {@code MathContext} object with a precision setting
* matching the IEEE 754R Decimal128 format, 34 digits, and a
* rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
* IEEE 754R default.
*/
public static final MathContext DECIMAL128 =
new MathContext(34, RoundingMode.HALF_EVEN);
/* ----- Shared Properties ----- */
/**
* The number of digits to be used for an operation. A value of 0
* indicates that unlimited precision (as many digits as are
* required) will be used. Note that leading zeros (in the
* coefficient of a number) are never significant.
*
* <p>{@code precision} will always be non-negative.
*
* @serial
*/
final int precision;
/**
* The rounding algorithm to be used for an operation.
*
* @see RoundingMode
* @serial
*/
final RoundingMode roundingMode;
/* ----- Constructors ----- */
/**
* Constructs a new {@code MathContext} with the specified
* precision and the {@link RoundingMode#HALF_UP HALF_UP} rounding
* mode.
*
* @param setPrecision The non-negative {@code int} precision setting.
* @throws IllegalArgumentException if the {@code setPrecision} parameter is less
* than zero.
*/
public MathContext(int setPrecision) {
this(setPrecision, DEFAULT_ROUNDINGMODE);
return;
}
/**
* Constructs a new {@code MathContext} with a specified
* precision and rounding mode.
*
* @param setPrecision The non-negative {@code int} precision setting.
* @param setRoundingMode The rounding mode to use.
* @throws IllegalArgumentException if the {@code setPrecision} parameter is less
* than zero.
* @throws NullPointerException if the rounding mode argument is {@code null}
*/
public MathContext(int setPrecision,
RoundingMode setRoundingMode) {
if (setPrecision < MIN_DIGITS)
throw new IllegalArgumentException("Digits < 0");
if (setRoundingMode == null)
throw new NullPointerException("null RoundingMode");
precision = setPrecision;
roundingMode = setRoundingMode;
return;
}
/**
* Constructs a new {@code MathContext} from a string.
*
* The string must be in the same format as that produced by the
* {@link #toString} method.
*
* <p>An {@code IllegalArgumentException} is thrown if the precision
* section of the string is out of range ({@code < 0}) or the string is
* not in the format created by the {@link #toString} method.
*
* @param val The string to be parsed
* @throws IllegalArgumentException if the precision section is out of range
* or of incorrect format
* @throws NullPointerException if the argument is {@code null}
*/
public MathContext(String val) {
boolean bad = false;
int setPrecision;
if (val == null)
throw new NullPointerException("null String");
try { // any error here is a string format problem
if (!val.startsWith("precision=")) throw new RuntimeException();
int fence = val.indexOf(' '); // could be -1
int off = 10; // where value starts
setPrecision = Integer.parseInt(val.substring(10, fence));
if (!val.startsWith("roundingMode=", fence+1))
throw new RuntimeException();
off = fence + 1 + 13;
String str = val.substring(off, val.length());
roundingMode = RoundingMode.valueOf(str);
} catch (RuntimeException re) {
throw new IllegalArgumentException("bad string format");
}
if (setPrecision < MIN_DIGITS)
throw new IllegalArgumentException("Digits < 0");
// the other parameters cannot be invalid if we got here
precision = setPrecision;
}
/**
* Returns the {@code precision} setting.
* This value is always non-negative.
*
* @return an {@code int} which is the value of the {@code precision}
* setting
*/
public int getPrecision() {
return precision;
}
/**
* Returns the roundingMode setting.
* This will be one of
* {@link RoundingMode#CEILING},
* {@link RoundingMode#DOWN},
* {@link RoundingMode#FLOOR},
* {@link RoundingMode#HALF_DOWN},
* {@link RoundingMode#HALF_EVEN},
* {@link RoundingMode#HALF_UP},
* {@link RoundingMode#UNNECESSARY}, or
* {@link RoundingMode#UP}.
*
* @return a {@code RoundingMode} object which is the value of the
* {@code roundingMode} setting
*/
public RoundingMode getRoundingMode() {
return roundingMode;
}
/**
* Compares this {@code MathContext} with the specified
* {@code Object} for equality.
*
* @param x {@code Object} to which this {@code MathContext} is to
* be compared.
* @return {@code true} if and only if the specified {@code Object} is
* a {@code MathContext} object which has exactly the same
* settings as this object
*/
public boolean equals(Object x){
MathContext mc;
if (!(x instanceof MathContext))
return false;
mc = (MathContext) x;
return mc.precision == this.precision
&& mc.roundingMode == this.roundingMode; // no need for .equals()
}
/**
* Returns the hash code for this {@code MathContext}.
*
* @return hash code for this {@code MathContext}
*/
public int hashCode() {
return this.precision + roundingMode.hashCode() * 59;
}
/**
* Returns the string representation of this {@code MathContext}.
* The {@code String} returned represents the settings of the
* {@code MathContext} object as two space-delimited words
* (separated by a single space character, <tt>'&#92;u0020'</tt>,
* and with no leading or trailing white space), as follows:
* <ol>
* <li>
* The string {@code "precision="}, immediately followed
* by the value of the precision setting as a numeric string as if
* generated by the {@link Integer#toString(int) Integer.toString}
* method.
*
* <li>
* The string {@code "roundingMode="}, immediately
* followed by the value of the {@code roundingMode} setting as a
* word. This word will be the same as the name of the
* corresponding public constant in the {@link RoundingMode}
* enum.
* </ol>
* <p>
* For example:
* <pre>
* precision=9 roundingMode=HALF_UP
* </pre>
*
* Additional words may be appended to the result of
* {@code toString} in the future if more properties are added to
* this class.
*
* @return a {@code String} representing the context settings
*/
public java.lang.String toString() {
return "precision=" + precision + " " +
"roundingMode=" + roundingMode.toString();
}
// Private methods
/**
* Reconstitute the {@code MathContext} instance from a stream (that is,
* deserialize it).
*
* @param s the stream being read.
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject(); // read in all fields
// validate possibly bad fields
if (precision < MIN_DIGITS) {
String message = "MathContext: invalid digits in stream";
throw new java.io.StreamCorruptedException(message);
}
if (roundingMode == null) {
String message = "MathContext: null roundingMode in stream";
throw new java.io.StreamCorruptedException(message);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,356 @@
/*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Portions Copyright IBM Corporation, 2001. All Rights Reserved.
*/
package java.math;
/**
* Specifies a <i>rounding behavior</i> for numerical operations
* capable of discarding precision. Each rounding mode indicates how
* the least significant returned digit of a rounded result is to be
* calculated. If fewer digits are returned than the digits needed to
* represent the exact numerical result, the discarded digits will be
* referred to as the <i>discarded fraction</i> regardless the digits'
* contribution to the value of the number. In other words,
* considered as a numerical value, the discarded fraction could have
* an absolute value greater than one.
*
* <p>Each rounding mode description includes a table listing how
* different two-digit decimal values would round to a one digit
* decimal value under the rounding mode in question. The result
* column in the tables could be gotten by creating a
* {@code BigDecimal} number with the specified value, forming a
* {@link MathContext} object with the proper settings
* ({@code precision} set to {@code 1}, and the
* {@code roundingMode} set to the rounding mode in question), and
* calling {@link BigDecimal#round round} on this number with the
* proper {@code MathContext}. A summary table showing the results
* of these rounding operations for all rounding modes appears below.
*
*<table border>
* <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></caption>
* <tr><th></th><th colspan=8>Result of rounding input to one digit with the given
* rounding mode</th>
* <tr valign=top>
* <th>Input Number</th> <th>{@code UP}</th>
* <th>{@code DOWN}</th>
* <th>{@code CEILING}</th>
* <th>{@code FLOOR}</th>
* <th>{@code HALF_UP}</th>
* <th>{@code HALF_DOWN}</th>
* <th>{@code HALF_EVEN}</th>
* <th>{@code UNNECESSARY}</th>
*
* <tr align=right><td>5.5</td> <td>6</td> <td>5</td> <td>6</td> <td>5</td> <td>6</td> <td>5</td> <td>6</td> <td>throw {@code ArithmeticException}</td>
* <tr align=right><td>2.5</td> <td>3</td> <td>2</td> <td>3</td> <td>2</td> <td>3</td> <td>2</td> <td>2</td> <td>throw {@code ArithmeticException}</td>
* <tr align=right><td>1.6</td> <td>2</td> <td>1</td> <td>2</td> <td>1</td> <td>2</td> <td>2</td> <td>2</td> <td>throw {@code ArithmeticException}</td>
* <tr align=right><td>1.1</td> <td>2</td> <td>1</td> <td>2</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>throw {@code ArithmeticException}</td>
* <tr align=right><td>1.0</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td>
* <tr align=right><td>-1.0</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td>
* <tr align=right><td>-1.1</td> <td>-2</td> <td>-1</td> <td>-1</td> <td>-2</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>throw {@code ArithmeticException}</td>
* <tr align=right><td>-1.6</td> <td>-2</td> <td>-1</td> <td>-1</td> <td>-2</td> <td>-2</td> <td>-2</td> <td>-2</td> <td>throw {@code ArithmeticException}</td>
* <tr align=right><td>-2.5</td> <td>-3</td> <td>-2</td> <td>-2</td> <td>-3</td> <td>-3</td> <td>-2</td> <td>-2</td> <td>throw {@code ArithmeticException}</td>
* <tr align=right><td>-5.5</td> <td>-6</td> <td>-5</td> <td>-5</td> <td>-6</td> <td>-6</td> <td>-5</td> <td>-6</td> <td>throw {@code ArithmeticException}</td>
*</table>
*
*
* <p>This {@code enum} is intended to replace the integer-based
* enumeration of rounding mode constants in {@link BigDecimal}
* ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN},
* etc. ).
*
* @see BigDecimal
* @see MathContext
* @author Josh Bloch
* @author Mike Cowlishaw
* @author Joseph D. Darcy
* @since 1.5
*/
public enum RoundingMode {
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a non-zero discarded fraction. Note that this
* rounding mode never decreases the magnitude of the calculated
* value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode UP Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code UP} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>3</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>2</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-2</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-3</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
UP(BigDecimal.ROUND_UP),
/**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode DOWN Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code DOWN} rounding
*<tr align=right><td>5.5</td> <td>5</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>1</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-1</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-5</td>
*</table>
*/
DOWN(BigDecimal.ROUND_DOWN),
/**
* Rounding mode to round towards positive infinity. If the
* result is positive, behaves as for {@code RoundingMode.UP};
* if negative, behaves as for {@code RoundingMode.DOWN}. Note
* that this rounding mode never decreases the calculated value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode CEILING Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code CEILING} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>3</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>2</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-1</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-5</td>
*</table>
*/
CEILING(BigDecimal.ROUND_CEILING),
/**
* Rounding mode to round towards negative infinity. If the
* result is positive, behave as for {@code RoundingMode.DOWN};
* if negative, behave as for {@code RoundingMode.UP}. Note that
* this rounding mode never increases the calculated value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode FLOOR Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code FLOOR} rounding
*<tr align=right><td>5.5</td> <td>5</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>1</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-2</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-3</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
FLOOR(BigDecimal.ROUND_FLOOR),
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code RoundingMode.UP} if the discarded
* fraction is &ge; 0.5; otherwise, behaves as for
* {@code RoundingMode.DOWN}. Note that this is the rounding
* mode commonly taught at school.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode HALF_UP Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code HALF_UP} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>3</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-3</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
HALF_UP(BigDecimal.ROUND_HALF_UP),
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code RoundingMode.UP} if the discarded
* fraction is &gt; 0.5; otherwise, behaves as for
* {@code RoundingMode.DOWN}.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode HALF_DOWN Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding
*<tr align=right><td>5.5</td> <td>5</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-5</td>
*</table>
*/
HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),
/**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* {@code RoundingMode.HALF_UP} if the digit to the left of the
* discarded fraction is odd; behaves as for
* {@code RoundingMode.HALF_DOWN} if it's even. Note that this
* is the rounding mode that statistically minimizes cumulative
* error when applied repeatedly over a sequence of calculations.
* It is sometimes known as {@literal "Banker's rounding,"} and is
* chiefly used in the USA. This rounding mode is analogous to
* the rounding policy used for {@code float} and {@code double}
* arithmetic in Java.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode HALF_EVEN Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),
/**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary. If this rounding mode is
* specified on an operation that yields an inexact result, an
* {@code ArithmeticException} is thrown.
*<p>Example:
*<table border>
* <caption><b>Rounding mode UNNECESSARY Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding
*<tr align=right><td>5.5</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>2.5</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>1.6</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>1.1</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>-1.6</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>-2.5</td> <td>throw {@code ArithmeticException}</td>
*<tr align=right><td>-5.5</td> <td>throw {@code ArithmeticException}</td>
*</table>
*/
UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);
// Corresponding BigDecimal rounding constant
final int oldMode;
/**
* Constructor
*
* @param oldMode The {@code BigDecimal} constant corresponding to
* this mode
*/
private RoundingMode(int oldMode) {
this.oldMode = oldMode;
}
/**
* Returns the {@code RoundingMode} object corresponding to a
* legacy integer rounding mode constant in {@link BigDecimal}.
*
* @param rm legacy integer rounding mode to convert
* @return {@code RoundingMode} corresponding to the given integer.
* @throws IllegalArgumentException integer is out of range
*/
public static RoundingMode valueOf(int rm) {
switch(rm) {
case BigDecimal.ROUND_UP:
return UP;
case BigDecimal.ROUND_DOWN:
return DOWN;
case BigDecimal.ROUND_CEILING:
return CEILING;
case BigDecimal.ROUND_FLOOR:
return FLOOR;
case BigDecimal.ROUND_HALF_UP:
return HALF_UP;
case BigDecimal.ROUND_HALF_DOWN:
return HALF_DOWN;
case BigDecimal.ROUND_HALF_EVEN:
return HALF_EVEN;
case BigDecimal.ROUND_UNNECESSARY:
return UNNECESSARY;
default:
throw new IllegalArgumentException("argument out of range");
}
}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.math;
/**
* A class used to represent multiprecision integers that makes efficient
* use of allocated space by allowing a number to occupy only part of
* an array so that the arrays do not have to be reallocated as often.
* When performing an operation with many iterations the array used to
* hold a number is only increased when necessary and does not have to
* be the same size as the number it represents. A mutable number allows
* calculations to occur on the same number without having to create
* a new number for every step of the calculation as occurs with
* BigIntegers.
*
* Note that SignedMutableBigIntegers only support signed addition and
* subtraction. All other operations occur as with MutableBigIntegers.
*
* @see BigInteger
* @author Michael McCloskey
* @since 1.3
*/
class SignedMutableBigInteger extends MutableBigInteger {
/**
* The sign of this MutableBigInteger.
*/
int sign = 1;
// Constructors
/**
* The default constructor. An empty MutableBigInteger is created with
* a one word capacity.
*/
SignedMutableBigInteger() {
super();
}
/**
* Construct a new MutableBigInteger with a magnitude specified by
* the int val.
*/
SignedMutableBigInteger(int val) {
super(val);
}
/**
* Construct a new MutableBigInteger with a magnitude equal to the
* specified MutableBigInteger.
*/
SignedMutableBigInteger(MutableBigInteger val) {
super(val);
}
// Arithmetic Operations
/**
* Signed addition built upon unsigned add and subtract.
*/
void signedAdd(SignedMutableBigInteger addend) {
if (sign == addend.sign)
add(addend);
else
sign = sign * subtract(addend);
}
/**
* Signed addition built upon unsigned add and subtract.
*/
void signedAdd(MutableBigInteger addend) {
if (sign == 1)
add(addend);
else
sign = sign * subtract(addend);
}
/**
* Signed subtraction built upon unsigned add and subtract.
*/
void signedSubtract(SignedMutableBigInteger addend) {
if (sign == addend.sign)
sign = sign * subtract(addend);
else
add(addend);
}
/**
* Signed subtraction built upon unsigned add and subtract.
*/
void signedSubtract(MutableBigInteger addend) {
if (sign == 1)
sign = sign * subtract(addend);
else
add(addend);
if (intLen == 0)
sign = 1;
}
/**
* Print out the first intLen ints of this MutableBigInteger's value
* array starting at offset.
*/
public String toString() {
return this.toBigInteger(sign).toString();
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Provides classes for performing arbitrary-precision integer
* arithmetic ({@code BigInteger}) and arbitrary-precision decimal
* arithmetic ({@code BigDecimal}). {@code BigInteger} is analogous
* to the primitive integer types except that it provides arbitrary
* precision, hence operations on {@code BigInteger}s do not overflow
* or lose precision. In addition to standard arithmetic operations,
* {@code BigInteger} provides modular arithmetic, GCD calculation,
* primality testing, prime generation, bit manipulation, and a few
* other miscellaneous operations.
*
* {@code BigDecimal} provides arbitrary-precision signed decimal
* numbers suitable for currency calculations and the like. {@code
* BigDecimal} gives the user complete control over rounding behavior,
* allowing the user to choose from a comprehensive set of eight
* rounding modes.
*
* @since JDK1.1
*/
package java.math;