📄 floatmul.java
字号:
/* * @(#)BigDecimal.java 1.64 06/11/28 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * @(#)BigDecimal.java 1.x 01/xx/xx * * Copyright 1996-2001 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright IBM Corporation, 2001. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. * */package java.math;/** * Immutable, arbitrary-precision signed decimal numbers. A * <tt>BigDecimal</tt> consists of an arbitrary precision integer * <i>unscaled value</i> and a 32-bit integer <i>scale</i>. If zero * or positive, the scale is the number of digits to the right of the * decimal point. If negative, the unscaled value of the number is * multiplied by ten to the power of the negation of the scale. The * value of the number represented by the <tt>BigDecimal</tt> is * therefore <tt>(unscaledValue × 10<sup>-scale</sup>)</tt>. * * <p>The <tt>BigDecimal</tt> class provides operations for * arithmetic, scale manipulation, rounding, comparison, hashing, and * format conversion. The {@link #toString} method provides a * canonical representation of a <tt>BigDecimal</tt>. * * <p>The <tt>BigDecimal</tt> class gives its user complete control * over rounding behavior. If no rounding mode is specified and the * exact result cannot be represented, an exception is thrown; * otherwise, calculations can be carried out to a chosen precision * and rounding mode by supplying an appropriate {@link MathContext} * object to the operation. In either case, eight <em>rounding * modes</em> are provided for the control of rounding. Using the * integer fields in this class (such as {@link #ROUND_HALF_UP}) to * represent rounding mode is largely obsolete; the enumeration values * of the <tt>RoundingMode</tt> <tt>enum</tt>, (such as {@link * RoundingMode#HALF_UP}) should be used instead. * * <p>When a <tt>MathContext</tt> object is supplied with a precision * setting of 0 (for example, {@link MathContext#UNLIMITED}), * arithmetic operations are exact, as are the arithmetic methods * which take no <tt>MathContext</tt> object. (This is the only * behavior that was supported in releases prior to 5.) As a * corollary of computing the exact result, the rounding mode setting * of a <tt>MathContext</tt> object with a precision setting of 0 is * not used and thus irrelevant. In the case of divide, the exact * quotient could have an infinitely long decimal expansion; for * example, 1 divided by 3. If the quotient has a nonterminating * decimal expansion and the operation is specified to return an exact * result, an <tt>ArithmeticException</tt> is thrown. Otherwise, the * exact result of the division is returned, as done for other * operations. * * <p>When the precision setting is not 0, the rules of * <tt>BigDecimal</tt> arithmetic are broadly compatible with selected * modes of operation of the arithmetic defined in ANSI X3.274-1996 * and ANSI X3.274-1996/AM 1-2000 (section 7.4). Unlike those * standards, <tt>BigDecimal</tt> includes many rounding modes, which * were mandatory for division in <tt>BigDecimal</tt> releases prior * to 5. Any conflicts between these ANSI standards and the * <tt>BigDecimal</tt> specification are resolved in favor of * <tt>BigDecimal</tt>. * * <p>Since the same numerical value can have different * representations (with different scales), the rules of arithmetic * and rounding must specify both the numerical result and the scale * used in the result's representation. * * * <p>In general the rounding modes and precision setting determine * how operations return results with a limited number of digits when * the exact result has more digits (perhaps infinitely many in the * case of division) than the number of digits returned. * * First, the * total number of digits to return is specified by the * <tt>MathContext</tt>'s <tt>precision</tt> setting; this determines * the result's <i>precision</i>. The digit count starts from the * leftmost nonzero digit of the exact result. The rounding mode * determines how any discarded trailing digits affect the returned * result. * * <p>For all arithmetic operators , the operation is carried out as * though an exact intermediate result were first calculated and then * rounded to the number of digits specified by the precision setting * (if necessary), using the selected rounding mode. If the exact * result is not returned, some digit positions of the exact result * are discarded. When rounding increases the magnitude of the * returned result, it is possible for a new digit position to be * created by a carry propagating to a leading "9" digit. * For example, rounding the value 999.9 to three digits rounding up * would be numerically equal to one thousand, represented as * 100×10<sup>1</sup>. In such cases, the new "1" is * the leading digit position of the returned result. * * <p>Besides a logical exact result, each arithmetic operation has a * preferred scale for representing a result. The preferred * scale for each operation is listed in the table below. * * <table border> * <caption top><h3>Preferred Scales for Results of Arithmetic Operations * </h3></caption> * <tr><th>Operation</th><th>Preferred Scale of Result</th></tr> * <tr><td>Add</td><td>max(addend.scale(), augend.scale())</td> * <tr><td>Subtract</td><td>max(minuend.scale(), subtrahend.scale())</td> * <tr><td>Multiply</td><td>multiplier.scale() + multiplicand.scale()</td> * <tr><td>Divide</td><td>dividend.scale() - divisor.scale()</td> * </table> * * These scales are the ones used by the methods which return exact * arithmetic results; except that an exact divide may have to use a * larger scale since the exact result may have more digits. For * example, <tt>1/32</tt> is <tt>0.03125</tt>. * * <p>Before rounding, the scale of the logical exact intermediate * result is the preferred scale for that operation. If the exact * numerical result cannot be represented in <code>precision</code> * digits, rounding selects the set of digits to return and the scale * of the result is reduced from the scale of the intermediate result * to the least scale which can represent the <code>precision</code> * digits actually returned. If the exact result can be represented * with at most <code>precision</code> digits, the representation * of the result with the scale closest to the preferred scale is * returned. In particular, an exactly representable quotient may be * represented in fewer than <code>precision</code> digits by removing * trailing zeros and decreasing the scale. For example, rounding to * three digits using the {@linkplain RoundingMode#FLOOR floor} * rounding mode, <br> * * <code>19/100 = 0.19 // integer=19, scale=2</code> <br> * * but<br> * * <code>21/110 = 0.190 // integer=190, scale=3</code> <br> * * <p>Note that for add, subtract, and multiply, the reduction in * scale will equal the number of digit positions of the exact result * which are discarded. If the rounding causes a carry propagation to * create a new high-order digit position, an additional digit of the * result is discarded than when no new digit position is created. * * <p>Other methods may have slightly different rounding semantics. * For example, the result of the <tt>pow</tt> method using the * {@linkplain #pow(int, MathContext) specified algorithm} can * occasionally differ from the rounded mathematical result by more * than one unit in the last place, one <i>{@linkplain #ulp() ulp}</i>. * * <p>Two types of operations are provided for manipulating the scale * of a <tt>BigDecimal</tt>: scaling/rounding operations and decimal * point motion operations. Scaling/rounding operations ({@link * #setScale setScale} and {@link #round round}) return a * <tt>BigDecimal</tt> whose value is approximately (or exactly) equal * to that of the operand, but whose scale or precision is the * specified value; that is, they increase or decrease the precision * of the stored number with minimal effect on its value. Decimal * point motion operations ({@link #movePointLeft movePointLeft} and * {@link #movePointRight movePointRight}) return a * <tt>BigDecimal</tt> created from the operand by moving the decimal * point a specified distance in the specified direction. * * <p>For the sake of brevity and clarity, pseudo-code is used * throughout the descriptions of <tt>BigDecimal</tt> methods. The * pseudo-code expression <tt>(i + j)</tt> is shorthand for "a * <tt>BigDecimal</tt> whose value is that of the <tt>BigDecimal</tt> * <tt>i</tt> added to that of the <tt>BigDecimal</tt> * <tt>j</tt>." The pseudo-code expression <tt>(i == j)</tt> is * shorthand for "<tt>true</tt> if and only if the * <tt>BigDecimal</tt> <tt>i</tt> represents the same value as the * <tt>BigDecimal</tt> <tt>j</tt>." Other pseudo-code expressions * are interpreted similarly. Square brackets are used to represent * the particular <tt>BigInteger</tt> and scale pair defining a * <tt>BigDecimal</tt> value; for example [19, 2] is the * <tt>BigDecimal</tt> numerically equal to 0.19 having a scale of 2. * * <p>Note: care should be exercised if <tt>BigDecimal</tt> objects * are used as keys in a {@link java.util.SortedMap SortedMap} or * elements in a {@link java.util.SortedSet SortedSet} since * <tt>BigDecimal</tt>'s <i>natural ordering</i> is <i>inconsistent * with equals</i>. See {@link Comparable}, {@link * java.util.SortedMap} or {@link java.util.SortedSet} for more * information. * * <p>All methods and constructors for this class throw * <tt>NullPointerException</tt> when passed a <tt>null</tt> object * reference for any input parameter. * * @see BigInteger * @see MathContext * @see RoundingMode * @see java.util.SortedMap * @see java.util.SortedSet * @author Josh Bloch * @author Mike Cowlishaw * @author Joseph D. Darcy */public class BigDecimal extends Number implements Comparable<BigDecimal> { /** * The unscaled value of this BigDecimal, as returned by {@link * #unscaledValue}. * * @serial * @see #unscaledValue */ private volatile BigInteger intVal; /** * The scale of this BigDecimal, as returned by {@link #scale}. * * @serial * @see #scale */ private int scale = 0; // Note: this may have any value, so // calculations must be done in longs /** * The number of decimal digits in this BigDecimal, or 0 if the * number of digits are not known (lookaside information). If * nonzero, the value is guaranteed correct. Use the precision() * method to obtain and set the value if it might be 0. This * field is mutable until set nonzero. * * @since 1.5 */ private volatile transient int precision = 0; /** * Used to store the canonical string representation, if computed. */ private volatile transient String stringCache = null; /** * Sentinel value for {@link #intCompact} indicating the * significand information is only available from {@code intVal}. */ private static final long INFLATED = Long.MIN_VALUE; /** * If the absolute value of the significand of this BigDecimal is * less than or equal to {@code Long.MAX_VALUE}, the value can be * compactly stored in this field and used in computations. */ private transient long intCompact = INFLATED; // All 18-digit base ten strings fit into a long; not all 19-digit // strings will private static final int MAX_COMPACT_DIGITS = 18; private static final int MAX_BIGINT_BITS = 62; /* Appease the serialization gods */ private static final long serialVersionUID = 6108874887143696463L; // Cache of common small BigDecimal values. private static final BigDecimal zeroThroughTen[] = { new BigDecimal(BigInteger.ZERO, 0, 0), new BigDecimal(BigInteger.ONE, 1, 0), new BigDecimal(BigInteger.valueOf(2), 2, 0), new BigDecimal(BigInteger.valueOf(3), 3, 0), new BigDecimal(BigInteger.valueOf(4), 4, 0), new BigDecimal(BigInteger.valueOf(5), 5, 0), new BigDecimal(BigInteger.valueOf(6), 6, 0), new BigDecimal(BigInteger.valueOf(7), 7, 0), new BigDecimal(BigInteger.valueOf(8), 8, 0), new BigDecimal(BigInteger.valueOf(9), 9, 0), new BigDecimal(BigInteger.TEN, 10, 0), }; // Constants /** * The value 0, with a scale of 0. * * @since 1.5 */ public static final BigDecimal ZERO = zeroThroughTen[0]; /** * The value 1, with a scale of 0. * * @since 1.5 */ public static final BigDecimal ONE = zeroThroughTen[1]; /** * The value 10, with a scale of 0. * * @since 1.5 */ public static final BigDecimal TEN = zeroThroughTen[10]; // Constructors /** * Translates a character array representation of a * <tt>BigDecimal</tt> into a <tt>BigDecimal</tt>, accepting the * same sequence of characters as the {@link #BigDecimal(String)} * constructor, while allowing a sub-array to be specified. * * <p>Note that if the sequence of characters is already available * within a character array, using this constructor is faster than * converting the <tt>char</tt> array to string and using the * <tt>BigDecimal(String)</tt> constructor . * * @param in <tt>char</tt> array that is the source of characters. * @param offset first character in the array to inspect. * @param len number of characters to consider. * @throws NumberFormatException if <tt>in</tt> is not a valid * representation of a <tt>BigDecimal</tt> or the defined subarray * is not wholly within <tt>in</tt>. * @since 1.5 */ public BigDecimal(char[] in, int offset, int len) { // This is the primary string to BigDecimal constructor; all // incoming strings end up here; it uses explicit (inline) // parsing for speed and generates at most one intermediate // (temporary) object (a char[] array).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -