|
Java example source code file (Double.java)
The Double.java Java example source code/* * Copyright (c) 1994, 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. */ package java.lang; import sun.misc.FloatingDecimal; import sun.misc.FpUtils; import sun.misc.DoubleConsts; /** * The {@code Double} class wraps a value of the primitive type * {@code double} in an object. An object of type * {@code Double} contains a single field whose type is * {@code double}. * * <p>In addition, this class provides several methods for converting a * {@code double} to a {@code String} and a * {@code String} to a {@code double}, as well as other * constants and methods useful when dealing with a * {@code double}. * * @author Lee Boynton * @author Arthur van Hoff * @author Joseph D. Darcy * @since JDK1.0 */ public final class Double extends Number implements Comparable<Double> { /** * A constant holding the positive infinity of type * {@code double}. It is equal to the value returned by * {@code Double.longBitsToDouble(0x7ff0000000000000L)}. */ public static final double POSITIVE_INFINITY = 1.0 / 0.0; /** * A constant holding the negative infinity of type * {@code double}. It is equal to the value returned by * {@code Double.longBitsToDouble(0xfff0000000000000L)}. */ public static final double NEGATIVE_INFINITY = -1.0 / 0.0; /** * A constant holding a Not-a-Number (NaN) value of type * {@code double}. It is equivalent to the value returned by * {@code Double.longBitsToDouble(0x7ff8000000000000L)}. */ public static final double NaN = 0.0d / 0.0; /** * A constant holding the largest positive finite value of type * {@code double}, * (2-2<sup>-52)·21023. It is equal to * the hexadecimal floating-point literal * {@code 0x1.fffffffffffffP+1023} and also equal to * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}. */ public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308 /** * A constant holding the smallest positive normal value of type * {@code double}, 2<sup>-1022. It is equal to the * hexadecimal floating-point literal {@code 0x1.0p-1022} and also * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}. * * @since 1.6 */ public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308 /** * A constant holding the smallest positive nonzero value of type * {@code double}, 2<sup>-1074. It is equal to the * hexadecimal floating-point literal * {@code 0x0.0000000000001P-1022} and also equal to * {@code Double.longBitsToDouble(0x1L)}. */ public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324 /** * Maximum exponent a finite {@code double} variable may have. * It is equal to the value returned by * {@code Math.getExponent(Double.MAX_VALUE)}. * * @since 1.6 */ public static final int MAX_EXPONENT = 1023; /** * Minimum exponent a normalized {@code double} variable may * have. It is equal to the value returned by * {@code Math.getExponent(Double.MIN_NORMAL)}. * * @since 1.6 */ public static final int MIN_EXPONENT = -1022; /** * The number of bits used to represent a {@code double} value. * * @since 1.5 */ public static final int SIZE = 64; /** * The number of bytes used to represent a {@code double} value. * * @since 1.8 */ public static final int BYTES = SIZE / Byte.SIZE; /** * The {@code Class} instance representing the primitive type * {@code double}. * * @since JDK1.1 */ @SuppressWarnings("unchecked") public static final Class<Double> TYPE = (Class |
* <tr>{@code Minimum Normal Value} | * <td>{@code 0x1.0p-1022} * <tr>{@code Maximum Subnormal Value} | * <td>{@code 0x0.fffffffffffffp-1022} * <tr>{@code Double.MIN_VALUE} | * <td>{@code 0x0.0000000000001p-1022} * </table> * @param d the {@code double} to be converted. * @return a hex string representation of the argument. * @since 1.5 * @author Joseph D. Darcy */ public static String toHexString(double d) { /* * Modeled after the "a" conversion specifier in C99, section * 7.19.6.1; however, the output of this method is more * tightly specified. */ if (!isFinite(d) ) // For infinity and NaN, use the decimal output. return Double.toString(d); else { // Initialized to maximum size of output. StringBuilder answer = new StringBuilder(24); if (Math.copySign(1.0, d) == -1.0) // value is negative, answer.append("-"); // so append sign info answer.append("0x"); d = Math.abs(d); if(d == 0.0) { answer.append("0.0p0"); } else { boolean subnormal = (d < DoubleConsts.MIN_NORMAL); // Isolate significand bits and OR in a high-order bit // so that the string representation has a known // length. long signifBits = (Double.doubleToLongBits(d) & DoubleConsts.SIGNIF_BIT_MASK) | 0x1000000000000000L; // Subnormal values have a 0 implicit bit; normal // values have a 1 implicit bit. answer.append(subnormal ? "0." : "1."); // Isolate the low-order 13 digits of the hex // representation. If all the digits are zero, // replace with a single 0; otherwise, remove all // trailing zeros. String signif = Long.toHexString(signifBits).substring(3,16); answer.append(signif.equals("0000000000000") ? // 13 zeros "0": signif.replaceFirst("0{1,12}$", "")); answer.append('p'); // If the value is subnormal, use the E_min exponent // value for double; otherwise, extract and report d's // exponent (the representation of a subnormal uses // E_min -1). answer.append(subnormal ? DoubleConsts.MIN_EXPONENT: Math.getExponent(d)); } return answer.toString(); } } /** * Returns a {@code Double} object holding the * {@code double} value represented by the argument string * {@code s}. * * <p>If {@code s} is {@code null}, then a * {@code NullPointerException} is thrown. * * <p>Leading and trailing whitespace characters in {@code s} * are ignored. Whitespace is removed as if by the {@link * String#trim} method; that is, both ASCII space and control * characters are removed. The rest of {@code s} should * constitute a <i>FloatValue as described by the lexical * syntax rules: * * <blockquote> * <dl> * <dt>FloatValue: * <dd>Signopt {@code NaN} * <dd>Signopt {@code Infinity} * <dd>Signopt FloatingPointLiteral * <dd>Signopt HexFloatingPointLiteral * <dd>SignedInteger * </dl> * * <dl> * <dt>HexFloatingPointLiteral: * <dd> HexSignificand BinaryExponent FloatTypeSuffixopt * </dl> * * <dl> * <dt>HexSignificand: * <dd>HexNumeral * <dd>HexNumeral {@code .} * <dd>{@code 0x} HexDigitsopt * </i>{@code .} HexDigits * <dd>{@code 0X} HexDigitsopt * </i>{@code .} HexDigits * </dl> * * <dl> * <dt>BinaryExponent: * <dd>BinaryExponentIndicator SignedInteger * </dl> * * <dl> * <dt>BinaryExponentIndicator: * <dd>{@code p} * <dd>{@code P} * </dl> * * </blockquote> * * where <i>Sign, FloatingPointLiteral, * <i>HexNumeral, HexDigits, SignedInteger and * <i>FloatTypeSuffix are as defined in the lexical structure * sections of * <cite>The Java™ Language Specification, * except that underscores are not accepted between digits. * If {@code s} does not have the form of * a <i>FloatValue, then a {@code NumberFormatException} * is thrown. Otherwise, {@code s} is regarded as * representing an exact decimal value in the usual * "computerized scientific notation" or as an exact * hexadecimal value; this exact numerical value is then * conceptually converted to an "infinitely precise" * binary value that is then rounded to type {@code double} * by the usual round-to-nearest rule of IEEE 754 floating-point * arithmetic, which includes preserving the sign of a zero * value. * * Note that the round-to-nearest rule also implies overflow and * underflow behaviour; if the exact value of {@code s} is large * enough in magnitude (greater than or equal to ({@link * #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2), * rounding to {@code double} will result in an infinity and if the * exact value of {@code s} is small enough in magnitude (less * than or equal to {@link #MIN_VALUE}/2), rounding to float will * result in a zero. * * Finally, after rounding a {@code Double} object representing * this {@code double} value is returned. * * <p> To interpret localized string representations of a * floating-point value, use subclasses of {@link * java.text.NumberFormat}. * * <p>Note that trailing format specifiers, specifiers that * determine the type of a floating-point literal * ({@code 1.0f} is a {@code float} value; * {@code 1.0d} is a {@code double} value), do * <em>not influence the results of this method. In other * words, the numerical value of the input string is converted * directly to the target floating-point type. The two-step * sequence of conversions, string to {@code float} followed * by {@code float} to {@code double}, is <em>not * equivalent to converting a string directly to * {@code double}. For example, the {@code float} * literal {@code 0.1f} is equal to the {@code double} * value {@code 0.10000000149011612}; the {@code float} * literal {@code 0.1f} represents a different numerical * value than the {@code double} literal * {@code 0.1}. (The numerical value 0.1 cannot be exactly * represented in a binary floating-point number.) * * <p>To avoid calling this method on an invalid string and having * a {@code NumberFormatException} be thrown, the regular * expression below can be used to screen the input string: * * <pre>{@code * final String Digits = "(\\p{Digit}+)"; * final String HexDigits = "(\\p{XDigit}+)"; * // an exponent is 'e' or 'E' followed by an optionally * // signed decimal integer. * final String Exp = "[eE][+-]?"+Digits; * final String fpRegex = * ("[\\x00-\\x20]*"+ // Optional leading "whitespace" * "[+-]?(" + // Optional sign character * "NaN|" + // "NaN" string * "Infinity|" + // "Infinity" string * * // A decimal floating-point string representing a finite positive * // number without a leading sign has at most five basic pieces: * // Digits . Digits ExponentPart FloatTypeSuffix * // * // Since this method allows integer-only strings as input * // in addition to strings of floating-point literals, the * // two sub-patterns below are simplifications of the grammar * // productions from section 3.10.2 of * // The Java Language Specification. * * // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt * "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ * * // . Digits ExponentPart_opt FloatTypeSuffix_opt * "(\\.("+Digits+")("+Exp+")?)|"+ * * // Hexadecimal strings * "((" + * // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt * "(0[xX]" + HexDigits + "(\\.)?)|" + * * // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt * "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + * * ")[pP][+-]?" + Digits + "))" + * "[fFdD]?))" + * "[\\x00-\\x20]*");// Optional trailing "whitespace" * * if (Pattern.matches(fpRegex, myString)) * Double.valueOf(myString); // Will not throw NumberFormatException * else { * // Perform suitable alternative action * } * }</pre> * * @param s the string to be parsed. * @return a {@code Double} object holding the value * represented by the {@code String} argument. * @throws NumberFormatException if the string does not contain a * parsable number. */ public static Double valueOf(String s) throws NumberFormatException { return new Double(parseDouble(s)); } /** * Returns a {@code Double} instance representing the specified * {@code double} value. * If a new {@code Double} instance is not required, this method * should generally be used in preference to the constructor * {@link #Double(double)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param d a double value. * @return a {@code Double} instance representing {@code d}. * @since 1.5 */ public static Double valueOf(double d) { return new Double(d); } /** * Returns a new {@code double} initialized to the value * represented by the specified {@code String}, as performed * by the {@code valueOf} method of class * {@code Double}. * * @param s the string to be parsed. * @return the {@code double} value represented by the string * argument. * @throws NullPointerException if the string is null * @throws NumberFormatException if the string does not contain * a parsable {@code double}. * @see java.lang.Double#valueOf(String) * @since 1.2 */ public static double parseDouble(String s) throws NumberFormatException { return FloatingDecimal.parseDouble(s); } /** * Returns {@code true} if the specified number is a * Not-a-Number (NaN) value, {@code false} otherwise. * * @param v the value to be tested. * @return {@code true} if the value of the argument is NaN; * {@code false} otherwise. */ public static boolean isNaN(double v) { return (v != v); } /** * Returns {@code true} if the specified number is infinitely * large in magnitude, {@code false} otherwise. * * @param v the value to be tested. * @return {@code true} if the value of the argument is positive * infinity or negative infinity; {@code false} otherwise. */ public static boolean isInfinite(double v) { return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY); } /** * Returns {@code true} if the argument is a finite floating-point * value; returns {@code false} otherwise (for NaN and infinity * arguments). * * @param d the {@code double} value to be tested * @return {@code true} if the argument is a finite * floating-point value, {@code false} otherwise. * @since 1.8 */ public static boolean isFinite(double d) { return Math.abs(d) <= DoubleConsts.MAX_VALUE; } /** * The value of the Double. * * @serial */ private final double value; /** * Constructs a newly allocated {@code Double} object that * represents the primitive {@code double} argument. * * @param value the value to be represented by the {@code Double}. */ public Double(double value) { this.value = value; } /** * Constructs a newly allocated {@code Double} object that * represents the floating-point value of type {@code double} * represented by the string. The string is converted to a * {@code double} value as if by the {@code valueOf} method. * * @param s a string to be converted to a {@code Double}. * @throws NumberFormatException if the string does not contain a * parsable number. * @see java.lang.Double#valueOf(java.lang.String) */ public Double(String s) throws NumberFormatException { value = parseDouble(s); } /** * Returns {@code true} if this {@code Double} value is * a Not-a-Number (NaN), {@code false} otherwise. * * @return {@code true} if the value represented by this object is * NaN; {@code false} otherwise. */ public boolean isNaN() { return isNaN(value); } /** * Returns {@code true} if this {@code Double} value is * infinitely large in magnitude, {@code false} otherwise. * * @return {@code true} if the value represented by this object is * positive infinity or negative infinity; * {@code false} otherwise. */ public boolean isInfinite() { return isInfinite(value); } /** * Returns a string representation of this {@code Double} object. * The primitive {@code double} value represented by this * object is converted to a string exactly as if by the method * {@code toString} of one argument. * * @return a {@code String} representation of this object. * @see java.lang.Double#toString(double) */ public String toString() { return toString(value); } /** * Returns the value of this {@code Double} as a {@code byte} * after a narrowing primitive conversion. * * @return the {@code double} value represented by this object * converted to type {@code byte} * @jls 5.1.3 Narrowing Primitive Conversions * @since JDK1.1 */ public byte byteValue() { return (byte)value; } /** * Returns the value of this {@code Double} as a {@code short} * after a narrowing primitive conversion. * * @return the {@code double} value represented by this object * converted to type {@code short} * @jls 5.1.3 Narrowing Primitive Conversions * @since JDK1.1 */ public short shortValue() { return (short)value; } /** * Returns the value of this {@code Double} as an {@code int} * after a narrowing primitive conversion. * @jls 5.1.3 Narrowing Primitive Conversions * * @return the {@code double} value represented by this object * converted to type {@code int} */ public int intValue() { return (int)value; } /** * Returns the value of this {@code Double} as a {@code long} * after a narrowing primitive conversion. * * @return the {@code double} value represented by this object * converted to type {@code long} * @jls 5.1.3 Narrowing Primitive Conversions */ public long longValue() { return (long)value; } /** * Returns the value of this {@code Double} as a {@code float} * after a narrowing primitive conversion. * * @return the {@code double} value represented by this object * converted to type {@code float} * @jls 5.1.3 Narrowing Primitive Conversions * @since JDK1.0 */ public float floatValue() { return (float)value; } /** * Returns the {@code double} value of this {@code Double} object. * * @return the {@code double} value represented by this object */ public double doubleValue() { return value; } /** * Returns a hash code for this {@code Double} object. The * result is the exclusive OR of the two halves of the * {@code long} integer bit representation, exactly as * produced by the method {@link #doubleToLongBits(double)}, of * the primitive {@code double} value represented by this * {@code Double} object. That is, the hash code is the value * of the expression: * * <blockquote> * {@code (int)(v^(v>>>32))} * </blockquote> * * where {@code v} is defined by: * * <blockquote> * {@code long v = Double.doubleToLongBits(this.doubleValue());} * </blockquote> * * @return a {@code hash code} value for this object. */ @Override public int hashCode() { return Double.hashCode(value); } /** * Returns a hash code for a {@code double} value; compatible with * {@code Double.hashCode()}. * * @param value the value to hash * @return a hash code value for a {@code double} value. * @since 1.8 */ public static int hashCode(double value) { long bits = doubleToLongBits(value); return (int)(bits ^ (bits >>> 32)); } /** * Compares this object against the specified object. The result * is {@code true} if and only if the argument is not * {@code null} and is a {@code Double} object that * represents a {@code double} that has the same value as the * {@code double} represented by this object. For this * purpose, two {@code double} values are considered to be * the same if and only if the method {@link * #doubleToLongBits(double)} returns the identical * {@code long} value when applied to each. * * <p>Note that in most cases, for two instances of class * {@code Double}, {@code d1} and {@code d2}, the * value of {@code d1.equals(d2)} is {@code true} if and * only if * * <blockquote> * {@code d1.doubleValue() == d2.doubleValue()} * </blockquote> * * <p>also has the value {@code true}. However, there are two * exceptions: * <ul> * <li>If {@code d1} and {@code d2} both represent * {@code Double.NaN}, then the {@code equals} method * returns {@code true}, even though * {@code Double.NaN==Double.NaN} has the value * {@code false}. * <li>If {@code d1} represents {@code +0.0} while * {@code d2} represents {@code -0.0}, or vice versa, * the {@code equal} test has the value {@code false}, * even though {@code +0.0==-0.0} has the value {@code true}. * </ul> * This definition allows hash tables to operate properly. * @param obj the object to compare with. * @return {@code true} if the objects are the same; * {@code false} otherwise. * @see java.lang.Double#doubleToLongBits(double) */ public boolean equals(Object obj) { return (obj instanceof Double) && (doubleToLongBits(((Double)obj).value) == doubleToLongBits(value)); } /** * Returns a representation of the specified floating-point value * according to the IEEE 754 floating-point "double * format" bit layout. * * <p>Bit 63 (the bit that is selected by the mask * {@code 0x8000000000000000L}) represents the sign of the * floating-point number. Bits * 62-52 (the bits that are selected by the mask * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0 * (the bits that are selected by the mask * {@code 0x000fffffffffffffL}) represent the significand * (sometimes called the mantissa) of the floating-point number. * * <p>If the argument is positive infinity, the result is * {@code 0x7ff0000000000000L}. * * <p>If the argument is negative infinity, the result is * {@code 0xfff0000000000000L}. * * <p>If the argument is NaN, the result is * {@code 0x7ff8000000000000L}. * * <p>In all cases, the result is a {@code long} integer that, when * given to the {@link #longBitsToDouble(long)} method, will produce a * floating-point value the same as the argument to * {@code doubleToLongBits} (except all NaN values are * collapsed to a single "canonical" NaN value). * * @param value a {@code double} precision floating-point number. * @return the bits that represent the floating-point number. */ public static long doubleToLongBits(double value) { long result = doubleToRawLongBits(value); // Check for NaN based on values of bit fields, maximum // exponent and nonzero significand. if ( ((result & DoubleConsts.EXP_BIT_MASK) == DoubleConsts.EXP_BIT_MASK) && (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L) result = 0x7ff8000000000000L; return result; } /** * Returns a representation of the specified floating-point value * according to the IEEE 754 floating-point "double * format" bit layout, preserving Not-a-Number (NaN) values. * * <p>Bit 63 (the bit that is selected by the mask * {@code 0x8000000000000000L}) represents the sign of the * floating-point number. Bits * 62-52 (the bits that are selected by the mask * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0 * (the bits that are selected by the mask * {@code 0x000fffffffffffffL}) represent the significand * (sometimes called the mantissa) of the floating-point number. * * <p>If the argument is positive infinity, the result is * {@code 0x7ff0000000000000L}. * * <p>If the argument is negative infinity, the result is * {@code 0xfff0000000000000L}. * * <p>If the argument is NaN, the result is the {@code long} * integer representing the actual NaN value. Unlike the * {@code doubleToLongBits} method, * {@code doubleToRawLongBits} does not collapse all the bit * patterns encoding a NaN to a single "canonical" NaN * value. * * <p>In all cases, the result is a {@code long} integer that, * when given to the {@link #longBitsToDouble(long)} method, will * produce a floating-point value the same as the argument to * {@code doubleToRawLongBits}. * * @param value a {@code double} precision floating-point number. * @return the bits that represent the floating-point number. * @since 1.3 */ public static native long doubleToRawLongBits(double value); /** * Returns the {@code double} value corresponding to a given * bit representation. * The argument is considered to be a representation of a * floating-point value according to the IEEE 754 floating-point * "double format" bit layout. * * <p>If the argument is {@code 0x7ff0000000000000L}, the result * is positive infinity. * * <p>If the argument is {@code 0xfff0000000000000L}, the result * is negative infinity. * * <p>If the argument is any value in the range * {@code 0x7ff0000000000001L} through * {@code 0x7fffffffffffffffL} or in the range * {@code 0xfff0000000000001L} through * {@code 0xffffffffffffffffL}, the result is a NaN. No IEEE * 754 floating-point operation provided by Java can distinguish * between two NaN values of the same type with different bit * patterns. Distinct values of NaN are only distinguishable by * use of the {@code Double.doubleToRawLongBits} method. * * <p>In all other cases, let s, e, and m be three * values that can be computed from the argument: * * <blockquote>
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.