151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 2cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. 351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.lang; 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 28694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniakimport sun.misc.FloatingDecimal; 2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.misc.FloatConsts; 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.misc.DoubleConsts; 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@code Float} class wraps a value of primitive type 3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} in an object. An object of type 3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Float} contains a single field whose type is 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}. 3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In addition, this class provides several methods for converting a 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} to a {@code String} and a 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} to a {@code float}, as well as other 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constants and methods useful when dealing with a 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}. 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Lee Boynton 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Arthur van Hoff 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Joseph D. Darcy 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic final class Float extends Number implements Comparable<Float> { 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A constant holding the positive infinity of type 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}. It is equal to the value returned by 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Float.intBitsToFloat(0x7f800000)}. 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static final float POSITIVE_INFINITY = 1.0f / 0.0f; 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A constant holding the negative infinity of type 5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}. It is equal to the value returned by 6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Float.intBitsToFloat(0xff800000)}. 6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static final float NEGATIVE_INFINITY = -1.0f / 0.0f; 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A constant holding a Not-a-Number (NaN) value of type 6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}. It is equivalent to the value returned by 6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Float.intBitsToFloat(0x7fc00000)}. 6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static final float NaN = 0.0f / 0.0f; 7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A constant holding the largest positive finite value of type 7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}, (2-2<sup>-23</sup>)·2<sup>127</sup>. 7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * It is equal to the hexadecimal floating-point literal 7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x1.fffffeP+127f} and also equal to 7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Float.intBitsToFloat(0x7f7fffff)}. 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A constant holding the smallest positive normal value of type 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}, 2<sup>-126</sup>. It is equal to the 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * hexadecimal floating-point literal {@code 0x1.0p-126f} and also 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equal to {@code Float.intBitsToFloat(0x00800000)}. 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A constant holding the smallest positive nonzero value of type 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}, 2<sup>-149</sup>. It is equal to the 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * hexadecimal floating-point literal {@code 0x0.000002P-126f} 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and also equal to {@code Float.intBitsToFloat(0x1)}. 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Maximum exponent a finite {@code float} variable may have. It 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is equal to the value returned by {@code 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Math.getExponent(Float.MAX_VALUE)}. 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static final int MAX_EXPONENT = 127; 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Minimum exponent a normalized {@code float} variable may have. 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * It is equal to the value returned by {@code 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Math.getExponent(Float.MIN_NORMAL)}. 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static final int MIN_EXPONENT = -126; 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The number of bits used to represent a {@code float} value. 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static final int SIZE = 32; 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 124cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * The number of bytes used to represent a {@code float} value. 125cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * 126cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @since 1.8 127cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong */ 128cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong public static final int BYTES = SIZE / Byte.SIZE; 129cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong 130cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong /** 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@code Class} instance representing the primitive type 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}. 13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 136694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 137a636fa97391c782e1249891c7c5a513bc2a384e1Vladimir Marko public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float"); 13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the {@code float} 14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument. All characters mentioned below are ASCII characters. 14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If the argument is NaN, the result is the string 14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "{@code NaN}". 14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>Otherwise, the result is a string that represents the sign and 14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * magnitude (absolute value) of the argument. If the sign is 14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * negative, the first character of the result is 148694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * '{@code -}' ({@code '\u005Cu002D'}); if the sign is 14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * positive, no sign character appears in the result. As for 15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the magnitude <i>m</i>: 15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If <i>m</i> is infinity, it is represented by the characters 15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "Infinity"}; thus, positive infinity produces 15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the result {@code "Infinity"} and negative infinity 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * produces the result {@code "-Infinity"}. 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If <i>m</i> is zero, it is represented by the characters 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "0.0"}; thus, negative zero produces the result 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "-0.0"} and positive zero produces the result 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "0.0"}. 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * less than 10<sup>7</sup>, then it is represented as the 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * integer part of <i>m</i>, in decimal form with no leading 16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * zeroes, followed by '{@code .}' 164694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * ({@code '\u005Cu002E'}), followed by one or more 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * decimal digits representing the fractional part of 16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>m</i>. 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or 16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equal to 10<sup>7</sup>, then it is represented in 16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so-called "computerized scientific notation." Let <i>n</i> 17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be the unique integer such that 10<sup><i>n</i> </sup>≤ 17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i> 17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be the mathematically exact quotient of <i>m</i> and 17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10. 17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The magnitude is then represented as the integer part of 17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>a</i>, as a single decimal digit, followed by 176694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * '{@code .}' ({@code '\u005Cu002E'}), followed by 17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * decimal digits representing the fractional part of 17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>a</i>, followed by the letter '{@code E}' 179694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * ({@code '\u005Cu0045'}), followed by a representation 18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of <i>n</i> as a decimal integer, as produced by the 18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method {@link java.lang.Integer#toString(int)}. 18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * How many digits must be printed for the fractional part of 18651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>m</i> or <i>a</i>? There must be at least one digit 18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to represent the fractional part, and beyond that as many, but 18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * only as many, more digits as are needed to uniquely distinguish 18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the argument value from adjacent values of type 19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}. That is, suppose that <i>x</i> is the 19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exact mathematical value represented by the decimal 19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation produced by this method for a finite nonzero 19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument <i>f</i>. Then <i>f</i> must be the {@code float} 19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value nearest to <i>x</i>; or, if two {@code float} values are 19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equally close to <i>x</i>, then <i>f</i> must be one of 19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * them and the least significant bit of the significand of 19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>f</i> must be {@code 0}. 19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>To create localized string representations of a floating-point 20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value, use subclasses of {@link java.text.NumberFormat}. 20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param f the float to be converted. 20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of the argument. 20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(float f) { 206694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak return FloatingDecimal.toJavaFormatString(f); 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hexadecimal string representation of the 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} argument. All characters mentioned below are 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ASCII characters. 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If the argument is NaN, the result is the string 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "{@code NaN}". 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>Otherwise, the result is a string that represents the sign and 21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * magnitude (absolute value) of the argument. If the sign is negative, 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the first character of the result is '{@code -}' 220694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * ({@code '\u005Cu002D'}); if the sign is positive, no sign character 22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * appears in the result. As for the magnitude <i>m</i>: 22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If <i>m</i> is infinity, it is represented by the string 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "Infinity"}; thus, positive infinity produces the 22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * result {@code "Infinity"} and negative infinity produces 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the result {@code "-Infinity"}. 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If <i>m</i> is zero, it is represented by the string 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "0x0.0p0"}; thus, negative zero produces the result 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "-0x0.0p0"} and positive zero produces the result 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "0x0.0p0"}. 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If <i>m</i> is a {@code float} value with a 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * normalized representation, substrings are used to represent the 23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * significand and exponent fields. The significand is 23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the characters {@code "0x1."} 23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * followed by a lowercase hexadecimal representation of the rest 23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the significand as a fraction. Trailing zeros in the 24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * hexadecimal representation are removed unless all the digits 24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are zero, in which case a single zero is used. Next, the 24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exponent is represented by {@code "p"} followed 24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by a decimal string of the unbiased exponent as if produced by 24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a call to {@link Integer#toString(int) Integer.toString} on the 24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exponent value. 24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If <i>m</i> is a {@code float} value with a subnormal 24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation, the significand is represented by the 24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * characters {@code "0x0."} followed by a 25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * hexadecimal representation of the rest of the significand as a 25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fraction. Trailing zeros in the hexadecimal representation are 25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * removed. Next, the exponent is represented by 25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "p-126"}. Note that there must be at 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * least one nonzero digit in a subnormal significand. 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <table border> 261694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * <caption>Examples</caption> 26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><th>Floating-point Value</th><th>Hexadecimal String</th> 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td> 26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td> 26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td> 26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td> 26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td> 26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td> 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code Float.MAX_VALUE}</td> 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td>{@code 0x1.fffffep127}</td> 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code Minimum Normal Value}</td> 27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td>{@code 0x1.0p-126}</td> 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code Maximum Subnormal Value}</td> 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td>{@code 0x0.fffffep-126}</td> 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td>{@code Float.MIN_VALUE}</td> 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td>{@code 0x0.000002p-126}</td> 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </table> 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param f the {@code float} to be converted. 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a hex string representation of the argument. 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Joseph D. Darcy 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toHexString(float f) { 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (Math.abs(f) < FloatConsts.MIN_NORMAL 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski && f != 0.0f ) {// float subnormal 28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Adjust exponent to create subnormal double, then 28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // replace subnormal double exponent with subnormal float 28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // exponent 289694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak String s = Double.toHexString(Math.scalb((double)f, 290694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak /* -1022+126 */ 291694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak DoubleConsts.MIN_EXPONENT- 292694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak FloatConsts.MIN_EXPONENT)); 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return s.replaceFirst("p-1022$", "p-126"); 29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else // double string will be the same as float string 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Double.toHexString(f); 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Float} object holding the 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} value represented by the argument string 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s}. 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If {@code s} is {@code null}, then a 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code NullPointerException} is thrown. 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Leading and trailing whitespace characters in {@code s} 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are ignored. Whitespace is removed as if by the {@link 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * String#trim} method; that is, both ASCII space and control 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * characters are removed. The rest of {@code s} should 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constitute a <i>FloatValue</i> as described by the lexical 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * syntax rules: 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dl> 31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dt><i>FloatValue:</i> 31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>Sign<sub>opt</sub></i> {@code NaN} 31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>Sign<sub>opt</sub></i> {@code Infinity} 31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i> 32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i> 32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>SignedInteger</i> 32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </dl> 32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dl> 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dt><i>HexFloatingPointLiteral</i>: 32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i> 32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </dl> 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dl> 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dt><i>HexSignificand:</i> 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>HexNumeral</i> 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>HexNumeral</i> {@code .} 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd>{@code 0x} <i>HexDigits<sub>opt</sub> 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </i>{@code .}<i> HexDigits</i> 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd>{@code 0X}<i> HexDigits<sub>opt</sub> 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </i>{@code .} <i>HexDigits</i> 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </dl> 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dl> 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dt><i>BinaryExponent:</i> 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>BinaryExponentIndicator SignedInteger</i> 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </dl> 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dl> 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dt><i>BinaryExponentIndicator:</i> 34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd>{@code p} 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd>{@code P} 34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </dl> 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * where <i>Sign</i>, <i>FloatingPointLiteral</i>, 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>FloatTypeSuffix</i> are as defined in the lexical structure 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sections of 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>, 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * except that underscores are not accepted between digits. 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If {@code s} does not have the form of 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a <i>FloatValue</i>, then a {@code NumberFormatException} 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is thrown. Otherwise, {@code s} is regarded as 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing an exact decimal value in the usual 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "computerized scientific notation" or as an exact 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * hexadecimal value; this exact numerical value is then 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conceptually converted to an "infinitely precise" 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary value that is then rounded to type {@code float} 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the usual round-to-nearest rule of IEEE 754 floating-point 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arithmetic, which includes preserving the sign of a zero 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value. 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that the round-to-nearest rule also implies overflow and 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * underflow behaviour; if the exact value of {@code s} is large 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enough in magnitude (greater than or equal to ({@link 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2), 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * rounding to {@code float} will result in an infinity and if the 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exact value of {@code s} is small enough in magnitude (less 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * than or equal to {@link #MIN_VALUE}/2), rounding to float will 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * result in a zero. 37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finally, after rounding a {@code Float} object representing 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code float} value is returned. 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>To interpret localized string representations of a 38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * floating-point value, use subclasses of {@link 38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.text.NumberFormat}. 38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that trailing format specifiers, specifiers that 38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * determine the type of a floating-point literal 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ({@code 1.0f} is a {@code float} value; 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 1.0d} is a {@code double} value), do 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <em>not</em> influence the results of this method. In other 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * words, the numerical value of the input string is converted 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * directly to the target floating-point type. In general, the 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two-step sequence of conversions, string to {@code double} 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * followed by {@code double} to {@code float}, is 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <em>not</em> equivalent to converting a string directly to 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}. For example, if first converted to an 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * intermediate {@code double} and then to 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}, the string<br> 39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br> 40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * results in the {@code float} value 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 1.0000002f}; if the string is converted directly to 40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float}, <code>1.000000<b>1</b>f</code> results. 40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>To avoid calling this method on an invalid string and having 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a {@code NumberFormatException} be thrown, the documentation 40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for {@link Double#valueOf Double.valueOf} lists a regular 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * expression which can be used to screen the input. 40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param s the string to be parsed. 41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a {@code Float} object holding the value 41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the {@code String} argument. 41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NumberFormatException if the string does not contain a 41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parsable number. 41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Float valueOf(String s) throws NumberFormatException { 416694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak return new Float(parseFloat(s)); 41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Float} instance representing the specified 42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} value. 42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a new {@code Float} instance is not required, this method 42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * should generally be used in preference to the constructor 42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #Float(float)}, as this method is likely to yield 42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * significantly better space and time performance by caching 42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * frequently requested values. 42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param f a float value. 42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a {@code Float} instance representing {@code f}. 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Float valueOf(float f) { 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return new Float(f); 43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a new {@code float} initialized to the value 43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the specified {@code String}, as performed 43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@code valueOf} method of class {@code Float}. 44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param s the string to be parsed. 44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code float} value represented by the string 44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument. 44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if the string is null 44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NumberFormatException if the string does not contain a 44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parsable {@code float}. 44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#valueOf(String) 44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static float parseFloat(String s) throws NumberFormatException { 451694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak return FloatingDecimal.parseFloat(s); 45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if the specified number is a 45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Not-a-Number (NaN) value, {@code false} otherwise. 45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param v the value to be tested. 45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if the argument is NaN; 46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 462694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak public static boolean isNaN(float v) { 46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (v != v); 46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if the specified number is infinitely 46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * large in magnitude, {@code false} otherwise. 46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param v the value to be tested. 47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if the argument is positive infinity or 47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * negative infinity; {@code false} otherwise. 47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 474694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak public static boolean isInfinite(float v) { 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY); 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 478694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4805bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak * Returns {@code true} if the argument is a finite floating-point 4815bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak * value; returns {@code false} otherwise (for NaN and infinity 4825bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak * arguments). 4835bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak * 4845bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak * @param f the {@code float} value to be tested 4855bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak * @return {@code true} if the argument is a finite 4865bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak * floating-point value, {@code false} otherwise. 4875bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak * @since 1.8 4885bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak */ 4895bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak public static boolean isFinite(float f) { 4905bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak return Math.abs(f) <= FloatConsts.MAX_VALUE; 4915bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak } 4925bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak 4935bad107355d440409eea313b60c40bc32c12e295Przemyslaw Szczepaniak /** 49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The value of the Float. 49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @serial 49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private final float value; 49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructs a newly allocated {@code Float} object that 50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents the primitive {@code float} argument. 50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param value the value to be represented by the {@code Float}. 50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Float(float value) { 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.value = value; 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructs a newly allocated {@code Float} object that 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents the argument converted to type {@code float}. 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param value the value to be represented by the {@code Float}. 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Float(double value) { 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.value = (float)value; 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructs a newly allocated {@code Float} object that 52251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents the floating-point value of type {@code float} 52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the string. The string is converted to a 52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} value as if by the {@code valueOf} method. 52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 52651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param s a string to be converted to a {@code Float}. 52751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NumberFormatException if the string does not contain a 52851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parsable number. 52951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#valueOf(java.lang.String) 53051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 53151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Float(String s) throws NumberFormatException { 532694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak value = parseFloat(s); 53351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 53451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 53551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 53651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this {@code Float} value is a 53751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Not-a-Number (NaN), {@code false} otherwise. 53851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 53951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if the value represented by this object is 54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * NaN; {@code false} otherwise. 54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 54251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isNaN() { 54351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return isNaN(value); 54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 54651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this {@code Float} value is 54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * infinitely large in magnitude, {@code false} otherwise. 54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if the value represented by this object is 55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * positive infinity or negative infinity; 55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isInfinite() { 55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return isInfinite(value); 55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of this {@code Float} object. 56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The primitive {@code float} value represented by this object 56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is converted to a {@code String} exactly as if by the method 56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code toString} of one argument. 56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a {@code String} representation of this object. 56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#toString(float) 56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String toString() { 56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Float.toString(value); 56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 572694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * Returns the value of this {@code Float} as a {@code byte} after 573694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * a narrowing primitive conversion. 57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code float} value represented by this object 57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * converted to type {@code byte} 577694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * @jls 5.1.3 Narrowing Primitive Conversions 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public byte byteValue() { 58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (byte)value; 58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 584694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * Returns the value of this {@code Float} as a {@code short} 585694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * after a narrowing primitive conversion. 58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code float} value represented by this object 58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * converted to type {@code short} 589694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * @jls 5.1.3 Narrowing Primitive Conversions 59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public short shortValue() { 59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (short)value; 59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 597694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * Returns the value of this {@code Float} as an {@code int} after 598694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * a narrowing primitive conversion. 59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code float} value represented by this object 60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * converted to type {@code int} 602694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * @jls 5.1.3 Narrowing Primitive Conversions 60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int intValue() { 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (int)value; 60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 60751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 609694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * Returns value of this {@code Float} as a {@code long} after a 610694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * narrowing primitive conversion. 61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code float} value represented by this object 61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * converted to type {@code long} 614694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * @jls 5.1.3 Narrowing Primitive Conversions 61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public long longValue() { 61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (long)value; 61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code float} value of this {@code Float} object. 62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code float} value represented by this object 62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public float floatValue() { 62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return value; 62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 630694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * Returns the value of this {@code Float} as a {@code double} 631694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * after a widening primitive conversion. 63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code float} value represented by this 634694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * object converted to type {@code double} 635694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * @jls 5.1.2 Widening Primitive Conversions 63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public double doubleValue() { 63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (double)value; 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code for this {@code Float} object. The 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * result is the integer bit representation, exactly as produced 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the method {@link #floatToIntBits(float)}, of the primitive 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} value represented by this {@code Float} 64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. 64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a hash code value for this object. 64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 650694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak @Override 65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int hashCode() { 652694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak return Float.hashCode(value); 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 656cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * Returns a hash code for a {@code float} value; compatible with 657cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * {@code Float.hashCode()}. 658cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * 659cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @param value the value to hash 660cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @return a hash code value for a {@code float} value. 661cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @since 1.8 662cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong */ 663cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong public static int hashCode(float value) { 664cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong return floatToIntBits(value); 665cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong } 666cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong 667cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong /** 66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Compares this object against the specified object. The result 67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is {@code true} if and only if the argument is not 67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} and is a {@code Float} object that 67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a {@code float} with the same value as the 67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} represented by this object. For this 67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * purpose, two {@code float} values are considered to be the 67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * same if and only if the method {@link #floatToIntBits(float)} 67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns the identical {@code int} value when applied to 67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * each. 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that in most cases, for two instances of class 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Float}, {@code f1} and {@code f2}, the value 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of {@code f1.equals(f2)} is {@code true} if and only if 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * f1.floatValue() == f2.floatValue() 68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>also has the value {@code true}. However, there are two exceptions: 68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If {@code f1} and {@code f2} both represent 69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Float.NaN}, then the {@code equals} method returns 69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true}, even though {@code Float.NaN==Float.NaN} 69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has the value {@code false}. 69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If {@code f1} represents {@code +0.0f} while 69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code f2} represents {@code -0.0f}, or vice 69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * versa, the {@code equal} test has the value 69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}, even though {@code 0.0f==-0.0f} 69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has the value {@code true}. 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This definition allows hash tables to operate properly. 70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param obj the object to be compared 70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if the objects are the same; 70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#floatToIntBits(float) 70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean equals(Object obj) { 70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (obj instanceof Float) 70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski && (floatToIntBits(((Float)obj).value) == floatToIntBits(value)); 71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a representation of the specified floating-point value 71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * according to the IEEE 754 floating-point "single format" bit 71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * layout. 71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Bit 31 (the bit that is selected by the mask 71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x80000000}) represents the sign of the floating-point 71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * number. 72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Bits 30-23 (the bits that are selected by the mask 72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x7f800000}) represent the exponent. 72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Bits 22-0 (the bits that are selected by the mask 72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x007fffff}) represent the significand (sometimes called 72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the mantissa) of the floating-point number. 72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the argument is positive infinity, the result is 72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x7f800000}. 72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the argument is negative infinity, the result is 73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0xff800000}. 73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the argument is NaN, the result is {@code 0x7fc00000}. 73351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In all cases, the result is an integer that, when given to the 73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #intBitsToFloat(int)} method, will produce a floating-point 73651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value the same as the argument to {@code floatToIntBits} 73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (except all NaN values are collapsed to a single 73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "canonical" NaN value). 73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param value a floating-point number. 74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the bits that represent the floating-point number. 74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int floatToIntBits(float value) { 74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = floatToRawIntBits(value); 74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Check for NaN based on values of bit fields, maximum 74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // exponent and nonzero significand. 74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if ( ((result & FloatConsts.EXP_BIT_MASK) == 74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski FloatConsts.EXP_BIT_MASK) && 74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski (result & FloatConsts.SIGNIF_BIT_MASK) != 0) 75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 0x7fc00000; 75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a representation of the specified floating-point value 75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * according to the IEEE 754 floating-point "single format" bit 75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * layout, preserving Not-a-Number (NaN) values. 75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Bit 31 (the bit that is selected by the mask 76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x80000000}) represents the sign of the floating-point 76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * number. 76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Bits 30-23 (the bits that are selected by the mask 76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x7f800000}) represent the exponent. 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Bits 22-0 (the bits that are selected by the mask 76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x007fffff}) represent the significand (sometimes called 76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the mantissa) of the floating-point number. 76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the argument is positive infinity, the result is 76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x7f800000}. 77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the argument is negative infinity, the result is 77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0xff800000}. 77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the argument is NaN, the result is the integer representing 77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the actual NaN value. Unlike the {@code floatToIntBits} 77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method, {@code floatToRawIntBits} does not collapse all the 77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * bit patterns encoding a NaN to a single "canonical" 77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * NaN value. 77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In all cases, the result is an integer that, when given to the 78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #intBitsToFloat(int)} method, will produce a 78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * floating-point value the same as the argument to 78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code floatToRawIntBits}. 78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param value a floating-point number. 78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the bits that represent the floating-point number. 78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.3 78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static native int floatToRawIntBits(float value); 79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code float} value corresponding to a given 79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * bit representation. 79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The argument is considered to be a representation of a 79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * floating-point value according to the IEEE 754 floating-point 79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "single format" bit layout. 79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the argument is {@code 0x7f800000}, the result is positive 79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * infinity. 80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the argument is {@code 0xff800000}, the result is negative 80251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * infinity. 80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the argument is any value in the range 80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x7f800001} through {@code 0x7fffffff} or in 80651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range {@code 0xff800001} through 80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0xffffffff}, the result is a NaN. No IEEE 754 80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * floating-point operation provided by Java can distinguish 80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * between two NaN values of the same type with different bit 81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * patterns. Distinct values of NaN are only distinguishable by 81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * use of the {@code Float.floatToRawIntBits} method. 81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three 81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values that can be computed from the argument: 81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 816694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * <blockquote><pre>{@code 817694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * int s = ((bits >> 31) == 0) ? 1 : -1; 818694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * int e = ((bits >> 23) & 0xff); 81951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * int m = (e == 0) ? 820694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * (bits & 0x7fffff) << 1 : 82151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (bits & 0x7fffff) | 0x800000; 822694e617f54a7bfbdad24913ce96f5d56f1a1960aPrzemyslaw Szczepaniak * }</pre></blockquote> 82351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 82451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Then the floating-point result equals the value of the mathematical 82551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-150</sup>. 82651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 82751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that this method may not be able to return a 82851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} NaN with exactly same bit pattern as the 82951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int} argument. IEEE 754 distinguishes between two 83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The 83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * differences between the two kinds of NaN are generally not 83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * visible in Java. Arithmetic operations on signaling NaNs turn 83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * them into quiet NaNs with a different, but often similar, bit 83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * pattern. However, on some processors merely copying a 83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signaling NaN also performs that conversion. In particular, 83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copying a signaling NaN to return it to the calling method may 83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * perform this conversion. So {@code intBitsToFloat} may 83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not be able to return a {@code float} with a signaling NaN 83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * bit pattern. Consequently, for some {@code int} values, 84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code floatToRawIntBits(intBitsToFloat(start))} may 84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>not</i> equal {@code start}. Moreover, which 84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular bit patterns represent signaling NaNs is platform 84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * dependent; although all NaN bit patterns, quiet or signaling, 84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * must be in the NaN range identified above. 84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param bits an integer. 84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code float} floating-point value with the same bit 84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * pattern. 84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 85051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static native float intBitsToFloat(int bits); 85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Compares two {@code Float} objects numerically. There are 85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two ways in which comparisons performed by this method differ 85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * from those performed by the Java language numerical comparison 85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operators ({@code <, <=, ==, >=, >}) when 85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * applied to primitive {@code float} values: 85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 85951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul><li> 86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Float.NaN} is considered by this method to 86151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be equal to itself and greater than all other 86251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code float} values 86351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (including {@code Float.POSITIVE_INFINITY}). 86451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> 86551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0.0f} is considered by this method to be greater 86651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * than {@code -0.0f}. 86751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 86951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This ensures that the <i>natural ordering</i> of {@code Float} 87051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects imposed by this method is <i>consistent with equals</i>. 87151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 87251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param anotherFloat the {@code Float} to be compared. 87351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value {@code 0} if {@code anotherFloat} is 87451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * numerically equal to this {@code Float}; a value 87551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * less than {@code 0} if this {@code Float} 87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is numerically less than {@code anotherFloat}; 87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and a value greater than {@code 0} if this 87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Float} is numerically greater than 87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code anotherFloat}. 88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Comparable#compareTo(Object) 88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int compareTo(Float anotherFloat) { 88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Float.compare(value, anotherFloat.value); 88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Compares the two specified {@code float} values. The sign 89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the integer value returned is the same as that of the 89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * integer that would be returned by the call: 89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> 89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * new Float(f1).compareTo(new Float(f2)) 89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre> 89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param f1 the first {@code float} to compare. 89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param f2 the second {@code float} to compare. 89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value {@code 0} if {@code f1} is 89951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * numerically equal to {@code f2}; a value less than 90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0} if {@code f1} is numerically less than 90151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code f2}; and a value greater than {@code 0} 90251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code f1} is numerically greater than 90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code f2}. 90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 90551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int compare(float f1, float f2) { 90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (f1 < f2) 90851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -1; // Neither val is NaN, thisVal is smaller 90951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (f1 > f2) 91051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 1; // Neither val is NaN, thisVal is larger 91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Cannot use floatToRawIntBits because of possibility of NaNs. 91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int thisBits = Float.floatToIntBits(f1); 91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int anotherBits = Float.floatToIntBits(f2); 91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 91651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (thisBits == anotherBits ? 0 : // Values are equal 91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN) 91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 1)); // (0.0, -0.0) or (NaN, !NaN) 91951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 92051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 921cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong /** 922cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * Adds two {@code float} values together as per the + operator. 923cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * 924cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @param a the first operand 925cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @param b the second operand 926cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @return the sum of {@code a} and {@code b} 927cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @jls 4.2.4 Floating-Point Operations 928cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @see java.util.function.BinaryOperator 929cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @since 1.8 930cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong */ 931cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong public static float sum(float a, float b) { 932cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong return a + b; 933cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong } 934cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong 935cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong /** 936cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * Returns the greater of two {@code float} values 937cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * as if by calling {@link Math#max(float, float) Math.max}. 938cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * 939cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @param a the first operand 940cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @param b the second operand 941cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @return the greater of {@code a} and {@code b} 942cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @see java.util.function.BinaryOperator 943cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @since 1.8 944cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong */ 945cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong public static float max(float a, float b) { 946cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong return Math.max(a, b); 947cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong } 948cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong 949cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong /** 950cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * Returns the smaller of two {@code float} values 951cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * as if by calling {@link Math#min(float, float) Math.min}. 952cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * 953cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @param a the first operand 954cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @param b the second operand 955cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @return the smaller of {@code a} and {@code b} 956cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @see java.util.function.BinaryOperator 957cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * @since 1.8 958cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong */ 959cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong public static float min(float a, float b) { 960cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong return Math.min(a, b); 961cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong } 962cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong 96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** use serialVersionUID from JDK 1.0.2 for interoperability */ 96451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final long serialVersionUID = -2671257302660747028L; 96551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 966