📄 floatmul.java
字号:
// use array bounds checking to handle too-long, len == 0, // bad offset, etc. try { // handle the sign boolean isneg = false; // assume positive if (in[offset] == '-') { isneg = true; // leading minus means negative offset++; len--; } else if (in[offset] == '+') { // leading + allowed offset++; len--; } // should now be at numeric part of the significand int dotoff = -1; // '.' offset, -1 if none int cfirst = offset; // record start of integer long exp = 0; // exponent if (len > in.length) // protect against huge length throw new NumberFormatException(); char coeff[] = new char[len]; // integer significand array char c; // work for (; len > 0; offset++, len--) { c = in[offset]; if ((c >= '0' && c <= '9') || Character.isDigit(c)) { // have digit coeff[precision] = c; precision++; // count of digits continue; } if (c == '.') { // have dot if (dotoff >= 0) // two dots throw new NumberFormatException(); dotoff = offset; continue; } // exponent expected if ((c != 'e') && (c != 'E')) throw new NumberFormatException(); offset++; c = in[offset]; len--; boolean negexp = false; // optional sign if (c == '-' || c == '+') { negexp = (c == '-'); offset++; c = in[offset]; len--; } if (len <= 0) // no exponent digits throw new NumberFormatException(); // skip leading zeros in the exponent while (len > 10 && Character.digit(c, 10) == 0) { offset++; c = in[offset]; len--; } if (len > 10) // too many nonzero exponent digits throw new NumberFormatException(); // c now holds first digit of exponent for (;; len--) { int v; if (c >= '0' && c <= '9') { v = c - '0'; } else { v = Character.digit(c, 10); if (v < 0) // not a digit throw new NumberFormatException(); } exp = exp * 10 + v; if (len == 1) break; // that was final character offset++; c = in[offset]; } if (negexp) // apply sign exp = -exp; // Next test is required for backwards compatibility if ((int)exp != exp) // overflow throw new NumberFormatException(); break; // [saves a test] } // here when no characters left if (precision == 0) // no digits found throw new NumberFormatException(); if (dotoff >= 0) { // had dot; set scale scale = precision - (dotoff - cfirst); // [cannot overflow] } if (exp != 0) { // had significant exponent try { scale = checkScale(-exp + scale); // adjust } catch (ArithmeticException e) { throw new NumberFormatException("Scale out of range."); } } // Remove leading zeros from precision (digits count) int first = 0; for (; (coeff[first] == '0' || Character.digit(coeff[first], 10) == 0) && precision > 1; first++) precision--; // Set the significand .. // Copy significand to exact-sized array, with sign if // negative // Later use: BigInteger(coeff, first, precision) for // both cases, by allowing an extra char at the front of // coeff. char quick[]; if (!isneg) { quick = new char[precision]; System.arraycopy(coeff, first, quick, 0, precision); } else { quick = new char[precision+1]; quick[0] = '-'; System.arraycopy(coeff, first, quick, 1, precision); } if (precision <= MAX_COMPACT_DIGITS) intCompact = Long.parseLong(new String(quick)); else intVal = new BigInteger(quick); // System.out.println(" new: " +intVal+" ["+scale+"] "+precision); } catch (ArrayIndexOutOfBoundsException e) { throw new NumberFormatException(); } catch (NegativeArraySizeException e) { throw new NumberFormatException(); } } /** * 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 and * with rounding according to the context settings. * * <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.. * @param mc the context to use. * @throws ArithmeticException if the result is inexact but the * rounding mode is <tt>UNNECESSARY</tt>. * @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, MathContext mc) { this(in, offset, len); if (mc.precision > 0) roundThis(mc); } /** * 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. * * <p>Note that if the sequence of characters is already available * as 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. * @throws NumberFormatException if <tt>in</tt> is not a valid * representation of a <tt>BigDecimal</tt>. * @since 1.5 */ public BigDecimal(char[] in) { this(in, 0, in.length); } /** * 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 and with rounding according to the context * settings. * * <p>Note that if the sequence of characters is already available * as 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 mc the context to use. * @throws ArithmeticException if the result is inexact but the * rounding mode is <tt>UNNECESSARY</tt>. * @throws NumberFormatException if <tt>in</tt> is not a valid * representation of a <tt>BigDecimal</tt>. * @since 1.5 */ public BigDecimal(char[] in, MathContext mc) { this(in, 0, in.length, mc); } /** * Translates the string representation of a <tt>BigDecimal</tt> * into a <tt>BigDecimal</tt>. The string representation consists * of an optional sign, <tt>'+'</tt> (<tt>'\u002B'</tt>) or * <tt>'-'</tt> (<tt>'\u002D'</tt>), followed by a sequence of * zero or more decimal digits ("the integer"), optionally * followed by a fraction, optionally followed by an exponent. * * <p>The fraction consists of a decimal point followed by zero * or more decimal digits. The string must contain at least one * digit in either the integer or the fraction. The number formed * by the sign, the integer and the fraction is referred to as the * <i>significand</i>. * * <p>The exponent consists of the character <tt>'e'</tt> * (<tt>'\u0065'</tt>) or <tt>'E'</tt> (<tt>'\u0045'</tt>) * followed by one or more decimal digits. The value of the * exponent must lie between -{@link Integer#MAX_VALUE} ({@link * Integer#MIN_VALUE}+1) and {@link Integer#MAX_VALUE}, inclusive. * * <p>More formally, the strings this constructor accepts are * described by the following grammar: * <blockquote> * <dl> * <dt><i>BigDecimalString:</i> * <dd><i>Sign<sub>opt</sub> Significand Exponent<sub>opt</sub></i> * <p> * <dt><i>Sign:</i> * <dd><tt>+</tt> * <dd><tt>-</tt> * <p> * <dt><i>Significand:</i> * <dd><i>IntegerPart</i> <tt>.</tt> <i>FractionPart<sub>opt</sub></i> * <dd><tt>.</tt> <i>FractionPart</i> * <dd><i>IntegerPart</i> * <p> * <dt><i>IntegerPart: * <dd>Digits</i> * <p> * <dt><i>FractionPart: * <dd>Digits</i> * <p> * <dt><i>Exponent: * <dd>ExponentIndicator SignedInteger</i> * <p> * <dt><i>ExponentIndicator:</i> * <dd><tt>e</tt> * <dd><tt>E</tt> * <p> * <dt><i>SignedInteger: * <dd>Sign<sub>opt</sub> Digits</i> * <p> * <dt><i>Digits: * <dd>Digit * <dd>Digits Digit</i> * <p> * <dt><i>Digit:</i> * <dd>any character for which {@link Character#isDigit} * returns <tt>true</tt>, including 0, 1, 2 ... * </dl> * </blockquote> * * <p>The scale of the returned <tt>BigDecimal</tt> will be the * number of digits in the fraction, or zero if the string * contains no decimal point, subject to adjustment for any * exponent; if the string contains an exponent, the exponent is * subtracted from the scale. The value of the resulting scale * must lie between <tt>Integer.MIN_VALUE</tt> and * <tt>Integer.MAX_VALUE</tt>, inclusive. * * <p>The character-to-digit mapping is provided by {@link * java.lang.Character#digit} set to convert to radix 10. The * String may not contain any extraneous characters (whitespace, * for example). * * <p><b>Examples:</b><br> * The value of the returned <tt>BigDecimal</tt> is equal to * <i>significand</i> × 10<sup> <i>exponent</i></sup>. * For each string on the left, the resulting representation * [<tt>BigInteger</tt>, <tt>scale</tt>] is shown on the right. * <pre> * "0" [0,0] * "0.00" [0,2] * "123" [123,0] * "-123" [-123,0] * "1.23E3" [123,-1] * "1.23E+3" [123,-1] * "12.3E+7" [123,-6] * "12.0" [120,1] * "12.3" [123,1] * "0.00123" [123,5] * "-1.23E-12" [-123,14] * "1234.5E-4" [12345,5] * "0E+7" [0,-7] * "-0" [0,0] * </pre> * * <p>Note: For values other than <tt>float</tt> and * <tt>double</tt> NaN and ±Infinity, this constructor is * compatible with the values returned by {@link Float#toString} * and {@link Double#toString}. This is generally the preferred * way to convert a <tt>float</tt> or <tt>double</tt> into a * BigDecimal, as it doesn't suffer from the unpredictability of * the {@link #BigDecimal(double)} constructor. * * @param val String representation of <tt>BigDecimal</tt>. * * @throws NumberFormatException if <tt>val</tt> is not a valid * representation of a <tt>BigDecimal</tt>. */ public BigDecimal(String val) { this(val.toCharArray(), 0, val.length()); } /** * Translates the string representation of a <tt>BigDecimal</tt> * into a <tt>BigDecimal</tt>, accepting the same strings as the * {@link #BigDecimal(String)} constructor, with rounding * according to the context settings.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -