Back to index

plt-scheme  4.2.1
Double.java
Go to the documentation of this file.
00001 /* Double.java -- object wrapper for double
00002    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
00003    Free Software Foundation, Inc.
00004 
00005 This file is part of GNU Classpath.
00006 
00007 GNU Classpath is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2, or (at your option)
00010 any later version.
00011 
00012 GNU Classpath is distributed in the hope that it will be useful, but
00013 WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with GNU Classpath; see the file COPYING.  If not, write to the
00019 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020 02111-1307 USA.
00021 
00022 Linking this library statically or dynamically with other modules is
00023 making a combined work based on this library.  Thus, the terms and
00024 conditions of the GNU General Public License cover the whole
00025 combination.
00026 
00027 As a special exception, the copyright holders of this library give you
00028 permission to link this library with independent modules to produce an
00029 executable, regardless of the license terms of these independent
00030 modules, and to copy and distribute the resulting executable under
00031 terms of your choice, provided that you also meet, for each linked
00032 independent module, the terms and conditions of the license of that
00033 module.  An independent module is a module which is not derived from
00034 or based on this library.  If you modify this library, you may extend
00035 this exception to your version of the library, but you are not
00036 obligated to do so.  If you do not wish to do so, delete this
00037 exception statement from your version. */
00038 
00039 package java.lang;
00040 
00041 //import gnu.classpath.Configuration;
00042 
00056 public final class Double extends Number implements Comparable
00057 {
00061   private static final long serialVersionUID = -9172774392245257468L;
00062 
00067   public static final double MAX_VALUE = 1.7976931348623157e+308;
00068 
00073   public static final double MIN_VALUE = 5e-324;
00074 
00079   public static final double NEGATIVE_INFINITY =  getNegInf(); //-1.0 / 0.0;
00080 
00084   public static final double POSITIVE_INFINITY =  getPosInf(); //1.0 / 0.0;
00085 
00089   public static final double NaN = getNaN(); //0.0 / 0.0;
00090 
00091   private static native double getNegInf();
00092   private static native double getPosInf();
00093   private static native double getNaN();
00094 
00100 //  public static final Class TYPE = VMClassLoader.getPrimitiveClass('D');
00101 
00107   private final double value;
00108 
00112 /*  static
00113   {
00114     if (Configuration.INIT_LOAD_LIBRARY)
00115       {
00116        System.loadLibrary("javalang");
00117        initIDs();
00118       }
00119   }
00120 */
00127   public Double(double value)
00128   {
00129     this.value = value;
00130   }
00131 
00142   public Double(String s)
00143   {
00144     value = parseDouble(s);
00145   }
00146 
00181   public static String toString(double d)
00182   {
00183     return toString(d, false);
00184   }
00185 
00196   public static Double valueOf(String s)
00197   {
00198     return new Double(parseDouble(s));
00199   }
00200 
00250   static native double parseDoubleHelper(String str);
00251   
00252   public static double parseDouble(String str) {
00253     if (null == str) 
00254       throw new NullPointerException("parseDouble expected to receive String, received null value");      
00255     try {
00256       return parseDoubleHelper(str);
00257       }
00258     catch (RuntimeException e) {
00259       throw new NumberFormatException(e.getMessage());
00260       }
00261     }
00262 
00270   public static boolean isNaN(double v)
00271   {
00272     // This works since NaN != NaN is the only reflexive inequality
00273     // comparison which returns true.
00274     return v != v;
00275   }
00276 
00285   public static boolean isInfinite(double v)
00286   {
00287     return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
00288   }
00289 
00296   public boolean isNaN()
00297   {
00298     return isNaN(value);
00299   }
00300 
00308   public boolean isInfinite()
00309   {
00310     return isInfinite(value);
00311   }
00312 
00321   public String toString()
00322   {
00323     return toString(value);
00324   }
00325 
00332   public byte byteValue()
00333   {
00334     return (byte) value;
00335   }
00336 
00343   public short shortValue()
00344   {
00345     return (short) value;
00346   }
00347 
00353   public int intValue()
00354   {
00355     return (int) value;
00356   }
00357 
00363   public long longValue()
00364   {
00365     return (long) value;
00366   }
00367 
00373   public float floatValue()
00374   {
00375     return (float) value;
00376   }
00377 
00383   public double doubleValue()
00384   {
00385     return value;
00386   }
00387 
00397   public int hashCode()
00398   {
00399     long v = doubleToLongBits(value);
00400     return (int) (v ^ (v >>> 32));
00401   }
00402 
00417   public boolean equals(Object obj)
00418   {
00419     if (! (obj instanceof Double))
00420       return false;
00421 
00422     double d = ((Double) obj).value;
00423 
00424     // Avoid call to native method. However, some implementations, like gcj,
00425     // are better off using floatToIntBits(value) == floatToIntBits(f).
00426     // Check common case first, then check NaN and 0.
00427     if (value == d)
00428       return (value != 0) || (1 / value == 1 / d);
00429     return isNaN(value) && isNaN(d);
00430   }
00431 
00446   public static native long doubleToLongBits(double value);
00447 //  {
00448 //    return VMDouble.doubleToLongBits(value);
00449 //  }
00450 
00465   public static native long doubleToRawLongBits(double value);
00466   //{
00467   //  return VMDouble.doubleToRawLongBits(value);
00468   //}
00469 
00483   public static native double longBitsToDouble(long bits);
00484 //  {
00485 //    return VMDouble.longBitsToDouble(bits);
00486 //  }
00487 
00500   public int compareTo(Double d)
00501   {
00502     return compare(value, d.value);
00503   }
00504 
00516   public int compareTo(Object o)
00517   {
00518     return compare(value, ((Double) o).value);
00519   }
00520 
00531   public static int compare(double x, double y)
00532   {
00533     if (isNaN(x))
00534       return isNaN(y) ? 0 : 1;
00535     if (isNaN(y))
00536       return -1;
00537     // recall that 0.0 == -0.0, so we convert to infinites and try again
00538     if (x == 0 && y == 0)
00539       return (int) (1 / x - 1 / y);
00540     if (x == y)
00541       return 0;
00542 
00543     return x > y ? 1 : -1;
00544   }
00545 
00553   // Package visible for use by Float.
00554   static native String toString(double d, boolean isFloat);
00555 
00560   private static native void initIDs();
00561 }