src/share/classes/java/lang/Float.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 860      * <pre>
 861      *    new Float(f1).compareTo(new Float(f2))
 862      * </pre>
 863      *
 864      * @param   f1        the first {@code float} to compare.
 865      * @param   f2        the second {@code float} to compare.
 866      * @return  the value {@code 0} if {@code f1} is
 867      *          numerically equal to {@code f2}; a value less than
 868      *          {@code 0} if {@code f1} is numerically less than
 869      *          {@code f2}; and a value greater than {@code 0}
 870      *          if {@code f1} is numerically greater than
 871      *          {@code f2}.
 872      * @since 1.4
 873      */
 874     public static int compare(float f1, float f2) {
 875        if (f1 < f2)
 876             return -1;           // Neither val is NaN, thisVal is smaller
 877         if (f1 > f2)
 878             return 1;            // Neither val is NaN, thisVal is larger
 879 

 880         int thisBits = Float.floatToIntBits(f1);
 881         int anotherBits = Float.floatToIntBits(f2);
 882 
 883         return (thisBits == anotherBits ?  0 : // Values are equal
 884                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
 885                  1));                          // (0.0, -0.0) or (NaN, !NaN)
 886     }
 887 
 888     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 889     private static final long serialVersionUID = -2671257302660747028L;
 890 }
   1 /*
   2  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 860      * <pre>
 861      *    new Float(f1).compareTo(new Float(f2))
 862      * </pre>
 863      *
 864      * @param   f1        the first {@code float} to compare.
 865      * @param   f2        the second {@code float} to compare.
 866      * @return  the value {@code 0} if {@code f1} is
 867      *          numerically equal to {@code f2}; a value less than
 868      *          {@code 0} if {@code f1} is numerically less than
 869      *          {@code f2}; and a value greater than {@code 0}
 870      *          if {@code f1} is numerically greater than
 871      *          {@code f2}.
 872      * @since 1.4
 873      */
 874     public static int compare(float f1, float f2) {
 875         if (f1 < f2)
 876             return -1;           // Neither val is NaN, thisVal is smaller
 877         if (f1 > f2)
 878             return 1;            // Neither val is NaN, thisVal is larger
 879 
 880         // Cannot use floatToRawIntBits because of possibility of NaNs.
 881         int thisBits    = Float.floatToIntBits(f1);
 882         int anotherBits = Float.floatToIntBits(f2);
 883 
 884         return (thisBits == anotherBits ?  0 : // Values are equal
 885                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
 886                  1));                          // (0.0, -0.0) or (NaN, !NaN)
 887     }
 888 
 889     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 890     private static final long serialVersionUID = -2671257302660747028L;
 891 }