home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Double.java < prev    next >
Text File  |  1997-05-20  |  14KB  |  402 lines

  1. /*
  2.  * @(#)Double.java    1.41 97/01/20
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.lang;
  24.  
  25. /**
  26.  * The Double class wraps a value of the primitive type 
  27.  * <code>double</code> in an object. An object of type 
  28.  * <code>Double</code> contains a single field whose type is 
  29.  * <code>double</code>. 
  30.  * <p>
  31.  * In addition, this class provides several methods for converting a 
  32.  * <code>double</code> to a <code>String</code> and a 
  33.  * <code>String</code> to a <code>double</code>, as well as other 
  34.  * constants and methods useful when dealing with a 
  35.  * <code>double</code>. 
  36.  *
  37.  * @author  Lee Boynton
  38.  * @author  Arthur van Hoff
  39.  * @version 1.41, 01/20/97
  40.  * @since   JDK1.0
  41.  */
  42. public final
  43. class Double extends Number {
  44.     /**
  45.      * The positive infinity of type <code>double</code>. 
  46.      *
  47.      * @since   JDK1.0
  48.      */
  49.     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
  50.  
  51.     /**
  52.      * The negative infinity of type <code>double</code>. 
  53.      *
  54.      * @since   JDK1.0
  55.      */
  56.     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
  57.  
  58.     /** 
  59.      * A NaN value of type <code>double</code>. 
  60.      *
  61.      * @since   JDK1.0
  62.      */
  63.     public static final double NaN = 0.0d / 0.0;
  64.  
  65.     /**
  66.      * The largest positive value of type <code>double</code>. 
  67.      *
  68.      * @since   JDK1.0
  69.      */
  70.     public static final double MAX_VALUE = 1.79769313486231570e+308;
  71.  
  72.     /**
  73.      * The smallest positive value of type <code>double</code>. 
  74.      *
  75.      * @since   JDK1.0
  76.      */
  77. //  public static final double MIN_VALUE = 4.94065645841246544e-324;
  78.     public static final double MIN_VALUE = longBitsToDouble(1L);
  79.  
  80.     /**
  81.      * The Class object representing the primitive type double.
  82.      *
  83.      * @since   JDK1.1
  84.      */
  85.     public static final Class    TYPE = Class.getPrimitiveClass("double");
  86.  
  87.     /**
  88.      * Creates a string representation of the <code>double</code> 
  89.      * argument. 
  90.      * <p>
  91.      * The values <code>NaN</code>, <code>NEGATIVE_INFINITY</code>, 
  92.      * <code>POSITIVE_INFINITY</code>, <code>-0.0</code>, and 
  93.      * <code>+0.0</code> are represented by the strings 
  94.      * <code>"NaN"</code>, <code>"-Infinity"</code>, 
  95.      * <code>"Infinity"</code>, <code>"-0.0"</code>, and 
  96.      * <code>"0.0"</code>, respectively. 
  97.      * <p>
  98.      * If <code>d</code> is in the range 
  99.      * <code>10<sup>-3</sup> <= |d| <=10<sup>7</sup></code>,
  100.      * then it is converted to a string in the style 
  101.      * <code>[-]ddd.ddd</code>. Otherwise, it is converted to a 
  102.      * string in the style <code>[-]m.ddddE±xx</code>.
  103.      * <p>
  104.      * There is always a minimum of one digit after the decimal point. 
  105.      * The number of digits is the minimum needed to uniquely distinguish 
  106.      * the argument value from adjacent values of type 
  107.      * <code>double</code>. 
  108.      *
  109.      * @param   d   the double to be converted.
  110.      * @return  a string representation of the argument.
  111.      * @since   JDK1.0
  112.      */
  113.     public static String toString(double d){
  114.     return new FloatingDecimal(d).toJavaFormatString();
  115.     }
  116.  
  117.     /**
  118.      * Returns a new Double value initialized to the value represented by the 
  119.      * specified String.
  120.      *
  121.      * @param      s   the string to be parsed.
  122.      * @return     a newly constructed <code>Double</code> initialized to the
  123.      *             value represented by the string argument.
  124.      * @exception  NumberFormatException  if the string does not contain a
  125.      *               parsable number.
  126.      * @since      JDK1.0
  127.      */
  128.     public static Double valueOf(String s) throws NumberFormatException { 
  129.     return new Double(valueOf0(s));
  130.     }
  131.  
  132.     /**
  133.      * Returns true if the specified number is the special Not-a-Number (NaN)
  134.      * value.
  135.      *
  136.      * @param   v   the value to be tested.
  137.      * @return  <code>true</code> if the value of the argument is NaN;
  138.      *          <code>false</code> otherwise.
  139.      * @since   JDK1.0
  140.      */
  141.     static public boolean isNaN(double v) {
  142.     return (v != v);
  143.     }
  144.  
  145.     /**
  146.      * Returns true if the specified number is infinitely large in magnitude.
  147.      *
  148.      * @param   v   the value to be tested.
  149.      * @return  <code>true</code> if the value of the argument is positive
  150.      *          infinity or negative infinity; <code>false</code> otherwise.
  151.      * @since   JDK1.0
  152.      */
  153.     static public boolean isInfinite(double v) {
  154.     return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  155.     }
  156.  
  157.     /**
  158.      * The value of the Double.
  159.      */
  160.     private double value;
  161.  
  162.     /**
  163.      * Constructs a newly allocated <code>Double</code> object that 
  164.      * represents the primitive <code>double</code> argument. 
  165.      *
  166.      * @param   value   the value to be represented by the <code>Double</code>.
  167.      * @since   JDK1.0
  168.      */
  169.     public Double(double value) {
  170.     this.value = value;
  171.     }
  172.  
  173.     /**
  174.      * Constructs a newly allocated <code>Double</code> object that 
  175.      * represents the floating- point value of type <code>double</code> 
  176.      * represented by the string. The string is converted to a 
  177.      * <code>double</code> value as if by the <code>valueOf</code> method. 
  178.      *
  179.      * @param      s   a string to be converted to a <code>Double</code>.
  180.      * @exception  NumberFormatException  if the string does not contain a
  181.      *               parsable number.
  182.      * @see        java.lang.Double#valueOf(java.lang.String)
  183.      * @since      JDK1.0
  184.      */
  185.     public Double(String s) throws NumberFormatException {
  186.     // REMIND: this is inefficient
  187.     this(valueOf(s).doubleValue());
  188.     }
  189.  
  190.     /**
  191.      * Returns true if this Double value is the special Not-a-Number (NaN)
  192.      * value.
  193.      *
  194.      * @return  <code>true</code> if the value represented by this object is
  195.      *          NaN; <code>false</code> otherwise.
  196.      * @since   JDK1.0
  197.      */
  198.     public boolean isNaN() {
  199.     return isNaN(value);
  200.     }
  201.  
  202.     /**
  203.      * Returns true if this Double value is infinitely large in magnitude.
  204.      *
  205.      * @return  <code>true</code> if the value represented by this object is
  206.      *          positive infinity or negative infinity;
  207.      *          <code>false</code> otherwise.
  208.      * @since   JDK1.0
  209.      */
  210.     public boolean isInfinite() {
  211.     return isInfinite(value);
  212.     }
  213.  
  214.     /**
  215.      * Returns a String representation of this Double object.
  216.      * The primitive <code>double</code> value represented by this 
  217.      * object is converted to a string exactly as if by the method 
  218.      * <code>toString</code> of one argument. 
  219.      *
  220.      * @return  a <code>String</code> representation of this object.
  221.      * @see     java.lang.Double#toString(double)
  222.      * @since   JDK1.0
  223.      */
  224.     public String toString() {
  225.     return String.valueOf(value);
  226.     }
  227.  
  228.     /**
  229.      * Returns the value of this Double as a byte (by casting to a byte).
  230.      *
  231.      * @since   JDK1.1
  232.      */
  233.     public byte byteValue() {
  234.     return (byte)value;
  235.     }
  236.  
  237.     /**
  238.      * Returns the value of this Double as a short (by casting to a short).
  239.      *
  240.      * @since   JDK1.1
  241.      */
  242.     public short shortValue() {
  243.     return (short)value;
  244.     }
  245.  
  246.     /**
  247.      * Returns the integer value of this Double (by casting to an int).
  248.      *
  249.      * @return  the <code>double</code> value represented by this object is
  250.      *          converted to type <code>int</code> and the result of the
  251.      *          conversion is returned.
  252.      * @since   JDK1.0
  253.      */
  254.     public int intValue() {
  255.     return (int)value;
  256.     }
  257.  
  258.     /**
  259.      * Returns the long value of this Double (by casting to a long).
  260.      *
  261.      * @return  the <code>double</code> value represented by this object is
  262.      *          converted to type <code>long</code> and the result of the
  263.      *          conversion is returned.
  264.      * @since   JDK1.0
  265.      */
  266.     public long longValue() {
  267.     return (long)value;
  268.     }
  269.  
  270.     /**
  271.      * Returns the float value of this Double.
  272.      *
  273.      * @return  the <code>double</code> value represented by this object is
  274.      *          converted to type <code>float</code> and the result of the
  275.      *          conversion is returned.
  276.      * @since   JDK1.0     */
  277.     public float floatValue() {
  278.     return (float)value;
  279.     }
  280.  
  281.     /**
  282.      * Returns the double value of this Double.
  283.      *
  284.      * @return  the <code>double</code> value represented by this object.
  285.      * @since   JDK1.0
  286.      */
  287.     public double doubleValue() {
  288.     return (double)value;
  289.     }
  290.  
  291.     /**
  292.      * Returns a hashcode for this Double.
  293.      *
  294.      * @return  a <code>hash code</code> value for this object. 
  295.      * @since   JDK1.0
  296.      */
  297.     public int hashCode() {
  298.     long bits = doubleToLongBits(value);
  299.     return (int)(bits ^ (bits >> 32));
  300.     }
  301.  
  302.     /**
  303.      * Compares this object against the specified object.
  304.      * The result is <code>true</code> if and only if the argument is 
  305.      * not <code>null</code> and is a <code>Double</code> object that 
  306.      * represents a double that has the identical bit pattern to the bit 
  307.      * pattern of the double represented by this object. 
  308.      * <p>
  309.      * Note that in most cases, for two instances of class 
  310.      * <code>Double</code>, <code>d1</code> and <code>d2</code>, the 
  311.      * value of <code>d1.equals(d2)</code> is <code>true</code> if and 
  312.      * only if 
  313.      * <ul><code>
  314.      *   d1.doubleValue() == d2.doubleValue()
  315.      * </code></ul>
  316.      * <p>
  317.      * also has the value <code>true</code>. However, there are two 
  318.      * exceptions: 
  319.      * <ul>
  320.      * <li>If <code>d1</code> and <code>d2</code> both represent 
  321.      *     <code>Double.NaN</code>, then the <code>equals</code> method 
  322.      *     returns <code>true</code>, even though 
  323.      *     <code>Double.NaN==Double.NaN</code> has the value 
  324.      *     <code>false</code>.
  325.      * <li>If <code>d1</code> represents <code>+0.0</code> while
  326.      *     <code>d2</code> represents <code>-0.0</code>, or vice versa,
  327.      *     the <code>equal</code> test has the value <code>false</code>,
  328.      *     even though <code>+0.0==-0.0</code> has the value <code>true.</code>
  329.      * </ul>
  330.      *
  331.      * @param   obj   the object to compare with.
  332.      * @return  <code>true</code> if the objects are the same;
  333.      *          <code>false</code> otherwise.
  334.      * @since   JDK1.0
  335.      */
  336.     public boolean equals(Object obj) {
  337.     return (obj != null)
  338.            && (obj instanceof Double) 
  339.            && (doubleToLongBits(((Double)obj).value) == 
  340.               doubleToLongBits(value));
  341.     }
  342.  
  343.     /**
  344.      * Returns a representation of the specified floating-point value 
  345.      * according to the IEEE 754 floating-point "double 
  346.      * format" bit layout. 
  347.      * <p>
  348.      * Bit 63 represents the sign of the floating-point number. Bits 
  349.      * 62-52 represent the exponent. Bits 51-0 represent 
  350.      * the significand (sometimes called the mantissa) of the 
  351.      * floating-point number. 
  352.      * <p>
  353.      * If the argument is positive infinity, the result is 
  354.      * <code>0x7ff0000000000000L</code>. 
  355.      * <p>
  356.      * If the argument is negative infinity, the result is 
  357.      * <code>0xfff0000000000000L</code>. 
  358.      * <p>
  359.      * If the argument is NaN, the result is 
  360.      * <code>0x7ff8000000000000L</code>. 
  361.      *
  362.      * @param   value   a double precision floating-point number.
  363.      * @return  the bits that represent the floating-point number.
  364.      * @since   JDK1.0
  365.      */
  366.     public static native long doubleToLongBits(double value);
  367.  
  368.     /**
  369.      * Returns the double-float corresponding to a given bit represention.
  370.      * The argument is considered to be a representation of a 
  371.      * floating-point value according to the IEEE 754 floating-point 
  372.      * "double precision" bit layout. That floating-point 
  373.      * value is returned as the result. 
  374.      * <p>
  375.      * If the argument is <code>0x7f80000000000000L</code>, the result 
  376.      * is positive infinity. 
  377.      * <p>
  378.      * If the argument is <code>0xff80000000000000L</code>, the result 
  379.      * is negative infinity. 
  380.      * <p>
  381.      * If the argument is any value in the range 
  382.      * <code>0x7ff0000000000001L</code> through 
  383.      * <code>0x7fffffffffffffffL</code> or in the range 
  384.      * <code>0xfff0000000000001L</code> through 
  385.      * <code>0xffffffffffffffffL</code>, the result is NaN. All IEEE 754 
  386.      * NaN values are, in effect, lumped together by the Java language 
  387.      * into a single value. 
  388.      *
  389.      * @param   bits   any <code>long</code> integer.
  390.      * @return  the <code>double</code> floating-point value with the same
  391.      *          bit pattern.
  392.      * @since   JDK1.0
  393.      */
  394.     public static native double longBitsToDouble(long bits);
  395.  
  396.     /* Converts a string to a double.  Also used by Float.valueOf */
  397.     static native double valueOf0(String s) throws NumberFormatException;
  398.  
  399.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  400.     private static final long serialVersionUID = -9172774392245257468L;
  401. }
  402.