Back to index

plt-scheme  4.2.1
Float.java
Go to the documentation of this file.
00001 /* Float.java -- object wrapper for float
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 
00040 package java.lang;
00041 
00055 public final class Float extends Number implements Comparable
00056 {
00060   private static final long serialVersionUID = -2671257302660747028L;
00061 
00066   public static final float MAX_VALUE = 3.4028235e+38f;
00067 
00072   public static final float MIN_VALUE = 1.4e-45f;
00073 
00077   public static final float NEGATIVE_INFINITY = (float) Double.NEGATIVE_INFINITY;//-1.0f / 0.0f;
00078 
00082   public static final float POSITIVE_INFINITY = (float) Double.POSITIVE_INFINITY;//1.0f / 0.0f;
00083 
00087   public static final float NaN = (float) Double.NaN;//0.0f / 0.0f;
00088 
00094   //public static final Class TYPE = VMClassLoader.getPrimitiveClass('F');
00095 
00101   private final float value;
00102 
00109   public Float(float value)
00110   {
00111     this.value = value;
00112   }
00113 
00120   public Float(double value)
00121   {
00122     this.value = (float) value;
00123   }
00124 
00135   public Float(String s)
00136   {
00137     value = parseFloat(s);
00138   }
00139 
00174   public static String toString(float f)
00175   {
00176     return Double.toString(f, true);
00177   }
00178 
00189   public static Float valueOf(String s)
00190   {
00191     return new Float(parseFloat(s));
00192   }
00193 
00243   public static float parseFloat(String str)
00244   {
00245     // XXX Rounding parseDouble() causes some errors greater than 1 ulp from
00246     // the infinitely precise decimal.
00247     return (float) Double.parseDouble(str);
00248   }
00249 
00257   public static boolean isNaN(float v)
00258   {
00259     // This works since NaN != NaN is the only reflexive inequality
00260     // comparison which returns true.
00261     return v != v;
00262   }
00263 
00272   public static boolean isInfinite(float v)
00273   {
00274     return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
00275   }
00276 
00283   public boolean isNaN()
00284   {
00285     return isNaN(value);
00286   }
00287 
00295   public boolean isInfinite()
00296   {
00297     return isInfinite(value);
00298   }
00299 
00308   public String toString()
00309   {
00310     return toString(value);
00311   }
00312 
00319   public byte byteValue()
00320   {
00321     return (byte) value;
00322   }
00323 
00330   public short shortValue()
00331   {
00332     return (short) value;
00333   }
00334 
00340   public int intValue()
00341   {
00342     return (int) value;
00343   }
00344 
00350   public long longValue()
00351   {
00352     return (long) value;
00353   }
00354 
00360   public float floatValue()
00361   {
00362     return value;
00363   }
00364 
00370   public double doubleValue()
00371   {
00372     return value;
00373   }
00374 
00382   public int hashCode()
00383   {
00384     return floatToIntBits(value);
00385   }
00386 
00401   public boolean equals(Object obj)
00402   {
00403     if (! (obj instanceof Float))
00404       return false;
00405 
00406     float f = ((Float) obj).value;
00407 
00408     // Avoid call to native method. However, some implementations, like gcj,
00409     // are better off using floatToIntBits(value) == floatToIntBits(f).
00410     // Check common case first, then check NaN and 0.
00411     if (value == f)
00412       return (value != 0) || (1 / value == 1 / f);
00413     return isNaN(value) && isNaN(f);
00414   }
00415 
00429   public static int floatToIntBits(float value)
00430   {
00431       throw new RuntimeException("floatToIntBits has not been implemented");
00432 //    return VMFloat.floatToIntBits(value);
00433   }
00434 
00448   public static int floatToRawIntBits(float value)
00449   {
00450       throw new RuntimeException("floatToRawIntBits has not been implemented");
00451 //    return VMFloat.floatToRawIntBits(value);
00452   }
00453 
00467   public static float intBitsToFloat(int bits)
00468   {
00469       throw new RuntimeException("intBitsToFloat has not been implmented");
00470 //    return VMFloat.intBitsToFloat(bits);
00471   }
00472 
00485   public int compareTo(Float f)
00486   {
00487     return compare(value, f.value);
00488   }
00489 
00501   public int compareTo(Object o)
00502   {
00503     return compare(value, ((Float) o).value);
00504   }
00505 
00516   public static int compare(float x, float y)
00517   {
00518     if (isNaN(x))
00519       return isNaN(y) ? 0 : 1;
00520     if (isNaN(y))
00521       return -1;
00522     // recall that 0.0 == -0.0, so we convert to infinities and try again
00523     if (x == 0 && y == 0)
00524       return (int) (1 / x - 1 / y);
00525     if (x == y)
00526       return 0;
00527 
00528     return x > y ? 1 : -1;
00529   }
00530 }