Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/lang/Float.java
38829 views
/*1* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627import sun.misc.FloatingDecimal;28import sun.misc.FloatConsts;29import sun.misc.DoubleConsts;3031/**32* The {@code Float} class wraps a value of primitive type33* {@code float} in an object. An object of type34* {@code Float} contains a single field whose type is35* {@code float}.36*37* <p>In addition, this class provides several methods for converting a38* {@code float} to a {@code String} and a39* {@code String} to a {@code float}, as well as other40* constants and methods useful when dealing with a41* {@code float}.42*43* @author Lee Boynton44* @author Arthur van Hoff45* @author Joseph D. Darcy46* @since JDK1.047*/48public final class Float extends Number implements Comparable<Float> {49/**50* A constant holding the positive infinity of type51* {@code float}. It is equal to the value returned by52* {@code Float.intBitsToFloat(0x7f800000)}.53*/54public static final float POSITIVE_INFINITY = 1.0f / 0.0f;5556/**57* A constant holding the negative infinity of type58* {@code float}. It is equal to the value returned by59* {@code Float.intBitsToFloat(0xff800000)}.60*/61public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;6263/**64* A constant holding a Not-a-Number (NaN) value of type65* {@code float}. It is equivalent to the value returned by66* {@code Float.intBitsToFloat(0x7fc00000)}.67*/68public static final float NaN = 0.0f / 0.0f;6970/**71* A constant holding the largest positive finite value of type72* {@code float}, (2-2<sup>-23</sup>)·2<sup>127</sup>.73* It is equal to the hexadecimal floating-point literal74* {@code 0x1.fffffeP+127f} and also equal to75* {@code Float.intBitsToFloat(0x7f7fffff)}.76*/77public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f7879/**80* A constant holding the smallest positive normal value of type81* {@code float}, 2<sup>-126</sup>. It is equal to the82* hexadecimal floating-point literal {@code 0x1.0p-126f} and also83* equal to {@code Float.intBitsToFloat(0x00800000)}.84*85* @since 1.686*/87public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f8889/**90* A constant holding the smallest positive nonzero value of type91* {@code float}, 2<sup>-149</sup>. It is equal to the92* hexadecimal floating-point literal {@code 0x0.000002P-126f}93* and also equal to {@code Float.intBitsToFloat(0x1)}.94*/95public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f9697/**98* Maximum exponent a finite {@code float} variable may have. It99* is equal to the value returned by {@code100* Math.getExponent(Float.MAX_VALUE)}.101*102* @since 1.6103*/104public static final int MAX_EXPONENT = 127;105106/**107* Minimum exponent a normalized {@code float} variable may have.108* It is equal to the value returned by {@code109* Math.getExponent(Float.MIN_NORMAL)}.110*111* @since 1.6112*/113public static final int MIN_EXPONENT = -126;114115/**116* The number of bits used to represent a {@code float} value.117*118* @since 1.5119*/120public static final int SIZE = 32;121122/**123* The number of bytes used to represent a {@code float} value.124*125* @since 1.8126*/127public static final int BYTES = SIZE / Byte.SIZE;128129/**130* The {@code Class} instance representing the primitive type131* {@code float}.132*133* @since JDK1.1134*/135@SuppressWarnings("unchecked")136public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float");137138/**139* Returns a string representation of the {@code float}140* argument. All characters mentioned below are ASCII characters.141* <ul>142* <li>If the argument is NaN, the result is the string143* "{@code NaN}".144* <li>Otherwise, the result is a string that represents the sign and145* magnitude (absolute value) of the argument. If the sign is146* negative, the first character of the result is147* '{@code -}' ({@code '\u005Cu002D'}); if the sign is148* positive, no sign character appears in the result. As for149* the magnitude <i>m</i>:150* <ul>151* <li>If <i>m</i> is infinity, it is represented by the characters152* {@code "Infinity"}; thus, positive infinity produces153* the result {@code "Infinity"} and negative infinity154* produces the result {@code "-Infinity"}.155* <li>If <i>m</i> is zero, it is represented by the characters156* {@code "0.0"}; thus, negative zero produces the result157* {@code "-0.0"} and positive zero produces the result158* {@code "0.0"}.159* <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but160* less than 10<sup>7</sup>, then it is represented as the161* integer part of <i>m</i>, in decimal form with no leading162* zeroes, followed by '{@code .}'163* ({@code '\u005Cu002E'}), followed by one or more164* decimal digits representing the fractional part of165* <i>m</i>.166* <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or167* equal to 10<sup>7</sup>, then it is represented in168* so-called "computerized scientific notation." Let <i>n</i>169* be the unique integer such that 10<sup><i>n</i> </sup>≤170* <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>171* be the mathematically exact quotient of <i>m</i> and172* 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10.173* The magnitude is then represented as the integer part of174* <i>a</i>, as a single decimal digit, followed by175* '{@code .}' ({@code '\u005Cu002E'}), followed by176* decimal digits representing the fractional part of177* <i>a</i>, followed by the letter '{@code E}'178* ({@code '\u005Cu0045'}), followed by a representation179* of <i>n</i> as a decimal integer, as produced by the180* method {@link java.lang.Integer#toString(int)}.181*182* </ul>183* </ul>184* How many digits must be printed for the fractional part of185* <i>m</i> or <i>a</i>? There must be at least one digit186* to represent the fractional part, and beyond that as many, but187* only as many, more digits as are needed to uniquely distinguish188* the argument value from adjacent values of type189* {@code float}. That is, suppose that <i>x</i> is the190* exact mathematical value represented by the decimal191* representation produced by this method for a finite nonzero192* argument <i>f</i>. Then <i>f</i> must be the {@code float}193* value nearest to <i>x</i>; or, if two {@code float} values are194* equally close to <i>x</i>, then <i>f</i> must be one of195* them and the least significant bit of the significand of196* <i>f</i> must be {@code 0}.197*198* <p>To create localized string representations of a floating-point199* value, use subclasses of {@link java.text.NumberFormat}.200*201* @param f the float to be converted.202* @return a string representation of the argument.203*/204public static String toString(float f) {205return FloatingDecimal.toJavaFormatString(f);206}207208/**209* Returns a hexadecimal string representation of the210* {@code float} argument. All characters mentioned below are211* ASCII characters.212*213* <ul>214* <li>If the argument is NaN, the result is the string215* "{@code NaN}".216* <li>Otherwise, the result is a string that represents the sign and217* magnitude (absolute value) of the argument. If the sign is negative,218* the first character of the result is '{@code -}'219* ({@code '\u005Cu002D'}); if the sign is positive, no sign character220* appears in the result. As for the magnitude <i>m</i>:221*222* <ul>223* <li>If <i>m</i> is infinity, it is represented by the string224* {@code "Infinity"}; thus, positive infinity produces the225* result {@code "Infinity"} and negative infinity produces226* the result {@code "-Infinity"}.227*228* <li>If <i>m</i> is zero, it is represented by the string229* {@code "0x0.0p0"}; thus, negative zero produces the result230* {@code "-0x0.0p0"} and positive zero produces the result231* {@code "0x0.0p0"}.232*233* <li>If <i>m</i> is a {@code float} value with a234* normalized representation, substrings are used to represent the235* significand and exponent fields. The significand is236* represented by the characters {@code "0x1."}237* followed by a lowercase hexadecimal representation of the rest238* of the significand as a fraction. Trailing zeros in the239* hexadecimal representation are removed unless all the digits240* are zero, in which case a single zero is used. Next, the241* exponent is represented by {@code "p"} followed242* by a decimal string of the unbiased exponent as if produced by243* a call to {@link Integer#toString(int) Integer.toString} on the244* exponent value.245*246* <li>If <i>m</i> is a {@code float} value with a subnormal247* representation, the significand is represented by the248* characters {@code "0x0."} followed by a249* hexadecimal representation of the rest of the significand as a250* fraction. Trailing zeros in the hexadecimal representation are251* removed. Next, the exponent is represented by252* {@code "p-126"}. Note that there must be at253* least one nonzero digit in a subnormal significand.254*255* </ul>256*257* </ul>258*259* <table border>260* <caption>Examples</caption>261* <tr><th>Floating-point Value</th><th>Hexadecimal String</th>262* <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>263* <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>264* <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>265* <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>266* <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>267* <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>268* <tr><td>{@code Float.MAX_VALUE}</td>269* <td>{@code 0x1.fffffep127}</td>270* <tr><td>{@code Minimum Normal Value}</td>271* <td>{@code 0x1.0p-126}</td>272* <tr><td>{@code Maximum Subnormal Value}</td>273* <td>{@code 0x0.fffffep-126}</td>274* <tr><td>{@code Float.MIN_VALUE}</td>275* <td>{@code 0x0.000002p-126}</td>276* </table>277* @param f the {@code float} to be converted.278* @return a hex string representation of the argument.279* @since 1.5280* @author Joseph D. Darcy281*/282public static String toHexString(float f) {283if (Math.abs(f) < FloatConsts.MIN_NORMAL284&& f != 0.0f ) {// float subnormal285// Adjust exponent to create subnormal double, then286// replace subnormal double exponent with subnormal float287// exponent288String s = Double.toHexString(Math.scalb((double)f,289/* -1022+126 */290DoubleConsts.MIN_EXPONENT-291FloatConsts.MIN_EXPONENT));292return s.replaceFirst("p-1022$", "p-126");293}294else // double string will be the same as float string295return Double.toHexString(f);296}297298/**299* Returns a {@code Float} object holding the300* {@code float} value represented by the argument string301* {@code s}.302*303* <p>If {@code s} is {@code null}, then a304* {@code NullPointerException} is thrown.305*306* <p>Leading and trailing whitespace characters in {@code s}307* are ignored. Whitespace is removed as if by the {@link308* String#trim} method; that is, both ASCII space and control309* characters are removed. The rest of {@code s} should310* constitute a <i>FloatValue</i> as described by the lexical311* syntax rules:312*313* <blockquote>314* <dl>315* <dt><i>FloatValue:</i>316* <dd><i>Sign<sub>opt</sub></i> {@code NaN}317* <dd><i>Sign<sub>opt</sub></i> {@code Infinity}318* <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>319* <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>320* <dd><i>SignedInteger</i>321* </dl>322*323* <dl>324* <dt><i>HexFloatingPointLiteral</i>:325* <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>326* </dl>327*328* <dl>329* <dt><i>HexSignificand:</i>330* <dd><i>HexNumeral</i>331* <dd><i>HexNumeral</i> {@code .}332* <dd>{@code 0x} <i>HexDigits<sub>opt</sub>333* </i>{@code .}<i> HexDigits</i>334* <dd>{@code 0X}<i> HexDigits<sub>opt</sub>335* </i>{@code .} <i>HexDigits</i>336* </dl>337*338* <dl>339* <dt><i>BinaryExponent:</i>340* <dd><i>BinaryExponentIndicator SignedInteger</i>341* </dl>342*343* <dl>344* <dt><i>BinaryExponentIndicator:</i>345* <dd>{@code p}346* <dd>{@code P}347* </dl>348*349* </blockquote>350*351* where <i>Sign</i>, <i>FloatingPointLiteral</i>,352* <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and353* <i>FloatTypeSuffix</i> are as defined in the lexical structure354* sections of355* <cite>The Java™ Language Specification</cite>,356* except that underscores are not accepted between digits.357* If {@code s} does not have the form of358* a <i>FloatValue</i>, then a {@code NumberFormatException}359* is thrown. Otherwise, {@code s} is regarded as360* representing an exact decimal value in the usual361* "computerized scientific notation" or as an exact362* hexadecimal value; this exact numerical value is then363* conceptually converted to an "infinitely precise"364* binary value that is then rounded to type {@code float}365* by the usual round-to-nearest rule of IEEE 754 floating-point366* arithmetic, which includes preserving the sign of a zero367* value.368*369* Note that the round-to-nearest rule also implies overflow and370* underflow behaviour; if the exact value of {@code s} is large371* enough in magnitude (greater than or equal to ({@link372* #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2),373* rounding to {@code float} will result in an infinity and if the374* exact value of {@code s} is small enough in magnitude (less375* than or equal to {@link #MIN_VALUE}/2), rounding to float will376* result in a zero.377*378* Finally, after rounding a {@code Float} object representing379* this {@code float} value is returned.380*381* <p>To interpret localized string representations of a382* floating-point value, use subclasses of {@link383* java.text.NumberFormat}.384*385* <p>Note that trailing format specifiers, specifiers that386* determine the type of a floating-point literal387* ({@code 1.0f} is a {@code float} value;388* {@code 1.0d} is a {@code double} value), do389* <em>not</em> influence the results of this method. In other390* words, the numerical value of the input string is converted391* directly to the target floating-point type. In general, the392* two-step sequence of conversions, string to {@code double}393* followed by {@code double} to {@code float}, is394* <em>not</em> equivalent to converting a string directly to395* {@code float}. For example, if first converted to an396* intermediate {@code double} and then to397* {@code float}, the string<br>398* {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>399* results in the {@code float} value400* {@code 1.0000002f}; if the string is converted directly to401* {@code float}, <code>1.000000<b>1</b>f</code> results.402*403* <p>To avoid calling this method on an invalid string and having404* a {@code NumberFormatException} be thrown, the documentation405* for {@link Double#valueOf Double.valueOf} lists a regular406* expression which can be used to screen the input.407*408* @param s the string to be parsed.409* @return a {@code Float} object holding the value410* represented by the {@code String} argument.411* @throws NumberFormatException if the string does not contain a412* parsable number.413*/414public static Float valueOf(String s) throws NumberFormatException {415return new Float(parseFloat(s));416}417418/**419* Returns a {@code Float} instance representing the specified420* {@code float} value.421* If a new {@code Float} instance is not required, this method422* should generally be used in preference to the constructor423* {@link #Float(float)}, as this method is likely to yield424* significantly better space and time performance by caching425* frequently requested values.426*427* @param f a float value.428* @return a {@code Float} instance representing {@code f}.429* @since 1.5430*/431public static Float valueOf(float f) {432return new Float(f);433}434435/**436* Returns a new {@code float} initialized to the value437* represented by the specified {@code String}, as performed438* by the {@code valueOf} method of class {@code Float}.439*440* @param s the string to be parsed.441* @return the {@code float} value represented by the string442* argument.443* @throws NullPointerException if the string is null444* @throws NumberFormatException if the string does not contain a445* parsable {@code float}.446* @see java.lang.Float#valueOf(String)447* @since 1.2448*/449public static float parseFloat(String s) throws NumberFormatException {450return FloatingDecimal.parseFloat(s);451}452453/**454* Returns {@code true} if the specified number is a455* Not-a-Number (NaN) value, {@code false} otherwise.456*457* @param v the value to be tested.458* @return {@code true} if the argument is NaN;459* {@code false} otherwise.460*/461public static boolean isNaN(float v) {462return (v != v);463}464465/**466* Returns {@code true} if the specified number is infinitely467* large in magnitude, {@code false} otherwise.468*469* @param v the value to be tested.470* @return {@code true} if the argument is positive infinity or471* negative infinity; {@code false} otherwise.472*/473public static boolean isInfinite(float v) {474return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);475}476477478/**479* Returns {@code true} if the argument is a finite floating-point480* value; returns {@code false} otherwise (for NaN and infinity481* arguments).482*483* @param f the {@code float} value to be tested484* @return {@code true} if the argument is a finite485* floating-point value, {@code false} otherwise.486* @since 1.8487*/488public static boolean isFinite(float f) {489return Math.abs(f) <= FloatConsts.MAX_VALUE;490}491492/**493* The value of the Float.494*495* @serial496*/497private final float value;498499/**500* Constructs a newly allocated {@code Float} object that501* represents the primitive {@code float} argument.502*503* @param value the value to be represented by the {@code Float}.504*/505public Float(float value) {506this.value = value;507}508509/**510* Constructs a newly allocated {@code Float} object that511* represents the argument converted to type {@code float}.512*513* @param value the value to be represented by the {@code Float}.514*/515public Float(double value) {516this.value = (float)value;517}518519/**520* Constructs a newly allocated {@code Float} object that521* represents the floating-point value of type {@code float}522* represented by the string. The string is converted to a523* {@code float} value as if by the {@code valueOf} method.524*525* @param s a string to be converted to a {@code Float}.526* @throws NumberFormatException if the string does not contain a527* parsable number.528* @see java.lang.Float#valueOf(java.lang.String)529*/530public Float(String s) throws NumberFormatException {531value = parseFloat(s);532}533534/**535* Returns {@code true} if this {@code Float} value is a536* Not-a-Number (NaN), {@code false} otherwise.537*538* @return {@code true} if the value represented by this object is539* NaN; {@code false} otherwise.540*/541public boolean isNaN() {542return isNaN(value);543}544545/**546* Returns {@code true} if this {@code Float} value is547* infinitely large in magnitude, {@code false} otherwise.548*549* @return {@code true} if the value represented by this object is550* positive infinity or negative infinity;551* {@code false} otherwise.552*/553public boolean isInfinite() {554return isInfinite(value);555}556557/**558* Returns a string representation of this {@code Float} object.559* The primitive {@code float} value represented by this object560* is converted to a {@code String} exactly as if by the method561* {@code toString} of one argument.562*563* @return a {@code String} representation of this object.564* @see java.lang.Float#toString(float)565*/566public String toString() {567return Float.toString(value);568}569570/**571* Returns the value of this {@code Float} as a {@code byte} after572* a narrowing primitive conversion.573*574* @return the {@code float} value represented by this object575* converted to type {@code byte}576* @jls 5.1.3 Narrowing Primitive Conversions577*/578public byte byteValue() {579return (byte)value;580}581582/**583* Returns the value of this {@code Float} as a {@code short}584* after a narrowing primitive conversion.585*586* @return the {@code float} value represented by this object587* converted to type {@code short}588* @jls 5.1.3 Narrowing Primitive Conversions589* @since JDK1.1590*/591public short shortValue() {592return (short)value;593}594595/**596* Returns the value of this {@code Float} as an {@code int} after597* a narrowing primitive conversion.598*599* @return the {@code float} value represented by this object600* converted to type {@code int}601* @jls 5.1.3 Narrowing Primitive Conversions602*/603public int intValue() {604return (int)value;605}606607/**608* Returns value of this {@code Float} as a {@code long} after a609* narrowing primitive conversion.610*611* @return the {@code float} value represented by this object612* converted to type {@code long}613* @jls 5.1.3 Narrowing Primitive Conversions614*/615public long longValue() {616return (long)value;617}618619/**620* Returns the {@code float} value of this {@code Float} object.621*622* @return the {@code float} value represented by this object623*/624public float floatValue() {625return value;626}627628/**629* Returns the value of this {@code Float} as a {@code double}630* after a widening primitive conversion.631*632* @return the {@code float} value represented by this633* object converted to type {@code double}634* @jls 5.1.2 Widening Primitive Conversions635*/636public double doubleValue() {637return (double)value;638}639640/**641* Returns a hash code for this {@code Float} object. The642* result is the integer bit representation, exactly as produced643* by the method {@link #floatToIntBits(float)}, of the primitive644* {@code float} value represented by this {@code Float}645* object.646*647* @return a hash code value for this object.648*/649@Override650public int hashCode() {651return Float.hashCode(value);652}653654/**655* Returns a hash code for a {@code float} value; compatible with656* {@code Float.hashCode()}.657*658* @param value the value to hash659* @return a hash code value for a {@code float} value.660* @since 1.8661*/662public static int hashCode(float value) {663return floatToIntBits(value);664}665666/**667668* Compares this object against the specified object. The result669* is {@code true} if and only if the argument is not670* {@code null} and is a {@code Float} object that671* represents a {@code float} with the same value as the672* {@code float} represented by this object. For this673* purpose, two {@code float} values are considered to be the674* same if and only if the method {@link #floatToIntBits(float)}675* returns the identical {@code int} value when applied to676* each.677*678* <p>Note that in most cases, for two instances of class679* {@code Float}, {@code f1} and {@code f2}, the value680* of {@code f1.equals(f2)} is {@code true} if and only if681*682* <blockquote><pre>683* f1.floatValue() == f2.floatValue()684* </pre></blockquote>685*686* <p>also has the value {@code true}. However, there are two exceptions:687* <ul>688* <li>If {@code f1} and {@code f2} both represent689* {@code Float.NaN}, then the {@code equals} method returns690* {@code true}, even though {@code Float.NaN==Float.NaN}691* has the value {@code false}.692* <li>If {@code f1} represents {@code +0.0f} while693* {@code f2} represents {@code -0.0f}, or vice694* versa, the {@code equal} test has the value695* {@code false}, even though {@code 0.0f==-0.0f}696* has the value {@code true}.697* </ul>698*699* This definition allows hash tables to operate properly.700*701* @param obj the object to be compared702* @return {@code true} if the objects are the same;703* {@code false} otherwise.704* @see java.lang.Float#floatToIntBits(float)705*/706public boolean equals(Object obj) {707return (obj instanceof Float)708&& (floatToIntBits(((Float)obj).value) == floatToIntBits(value));709}710711/**712* Returns a representation of the specified floating-point value713* according to the IEEE 754 floating-point "single format" bit714* layout.715*716* <p>Bit 31 (the bit that is selected by the mask717* {@code 0x80000000}) represents the sign of the floating-point718* number.719* Bits 30-23 (the bits that are selected by the mask720* {@code 0x7f800000}) represent the exponent.721* Bits 22-0 (the bits that are selected by the mask722* {@code 0x007fffff}) represent the significand (sometimes called723* the mantissa) of the floating-point number.724*725* <p>If the argument is positive infinity, the result is726* {@code 0x7f800000}.727*728* <p>If the argument is negative infinity, the result is729* {@code 0xff800000}.730*731* <p>If the argument is NaN, the result is {@code 0x7fc00000}.732*733* <p>In all cases, the result is an integer that, when given to the734* {@link #intBitsToFloat(int)} method, will produce a floating-point735* value the same as the argument to {@code floatToIntBits}736* (except all NaN values are collapsed to a single737* "canonical" NaN value).738*739* @param value a floating-point number.740* @return the bits that represent the floating-point number.741*/742public static int floatToIntBits(float value) {743int result = floatToRawIntBits(value);744// Check for NaN based on values of bit fields, maximum745// exponent and nonzero significand.746if ( ((result & FloatConsts.EXP_BIT_MASK) ==747FloatConsts.EXP_BIT_MASK) &&748(result & FloatConsts.SIGNIF_BIT_MASK) != 0)749result = 0x7fc00000;750return result;751}752753/**754* Returns a representation of the specified floating-point value755* according to the IEEE 754 floating-point "single format" bit756* layout, preserving Not-a-Number (NaN) values.757*758* <p>Bit 31 (the bit that is selected by the mask759* {@code 0x80000000}) represents the sign of the floating-point760* number.761* Bits 30-23 (the bits that are selected by the mask762* {@code 0x7f800000}) represent the exponent.763* Bits 22-0 (the bits that are selected by the mask764* {@code 0x007fffff}) represent the significand (sometimes called765* the mantissa) of the floating-point number.766*767* <p>If the argument is positive infinity, the result is768* {@code 0x7f800000}.769*770* <p>If the argument is negative infinity, the result is771* {@code 0xff800000}.772*773* <p>If the argument is NaN, the result is the integer representing774* the actual NaN value. Unlike the {@code floatToIntBits}775* method, {@code floatToRawIntBits} does not collapse all the776* bit patterns encoding a NaN to a single "canonical"777* NaN value.778*779* <p>In all cases, the result is an integer that, when given to the780* {@link #intBitsToFloat(int)} method, will produce a781* floating-point value the same as the argument to782* {@code floatToRawIntBits}.783*784* @param value a floating-point number.785* @return the bits that represent the floating-point number.786* @since 1.3787*/788public static native int floatToRawIntBits(float value);789790/**791* Returns the {@code float} value corresponding to a given792* bit representation.793* The argument is considered to be a representation of a794* floating-point value according to the IEEE 754 floating-point795* "single format" bit layout.796*797* <p>If the argument is {@code 0x7f800000}, the result is positive798* infinity.799*800* <p>If the argument is {@code 0xff800000}, the result is negative801* infinity.802*803* <p>If the argument is any value in the range804* {@code 0x7f800001} through {@code 0x7fffffff} or in805* the range {@code 0xff800001} through806* {@code 0xffffffff}, the result is a NaN. No IEEE 754807* floating-point operation provided by Java can distinguish808* between two NaN values of the same type with different bit809* patterns. Distinct values of NaN are only distinguishable by810* use of the {@code Float.floatToRawIntBits} method.811*812* <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three813* values that can be computed from the argument:814*815* <blockquote><pre>{@code816* int s = ((bits >> 31) == 0) ? 1 : -1;817* int e = ((bits >> 23) & 0xff);818* int m = (e == 0) ?819* (bits & 0x7fffff) << 1 :820* (bits & 0x7fffff) | 0x800000;821* }</pre></blockquote>822*823* Then the floating-point result equals the value of the mathematical824* expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-150</sup>.825*826* <p>Note that this method may not be able to return a827* {@code float} NaN with exactly same bit pattern as the828* {@code int} argument. IEEE 754 distinguishes between two829* kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The830* differences between the two kinds of NaN are generally not831* visible in Java. Arithmetic operations on signaling NaNs turn832* them into quiet NaNs with a different, but often similar, bit833* pattern. However, on some processors merely copying a834* signaling NaN also performs that conversion. In particular,835* copying a signaling NaN to return it to the calling method may836* perform this conversion. So {@code intBitsToFloat} may837* not be able to return a {@code float} with a signaling NaN838* bit pattern. Consequently, for some {@code int} values,839* {@code floatToRawIntBits(intBitsToFloat(start))} may840* <i>not</i> equal {@code start}. Moreover, which841* particular bit patterns represent signaling NaNs is platform842* dependent; although all NaN bit patterns, quiet or signaling,843* must be in the NaN range identified above.844*845* @param bits an integer.846* @return the {@code float} floating-point value with the same bit847* pattern.848*/849public static native float intBitsToFloat(int bits);850851/**852* Compares two {@code Float} objects numerically. There are853* two ways in which comparisons performed by this method differ854* from those performed by the Java language numerical comparison855* operators ({@code <, <=, ==, >=, >}) when856* applied to primitive {@code float} values:857*858* <ul><li>859* {@code Float.NaN} is considered by this method to860* be equal to itself and greater than all other861* {@code float} values862* (including {@code Float.POSITIVE_INFINITY}).863* <li>864* {@code 0.0f} is considered by this method to be greater865* than {@code -0.0f}.866* </ul>867*868* This ensures that the <i>natural ordering</i> of {@code Float}869* objects imposed by this method is <i>consistent with equals</i>.870*871* @param anotherFloat the {@code Float} to be compared.872* @return the value {@code 0} if {@code anotherFloat} is873* numerically equal to this {@code Float}; a value874* less than {@code 0} if this {@code Float}875* is numerically less than {@code anotherFloat};876* and a value greater than {@code 0} if this877* {@code Float} is numerically greater than878* {@code anotherFloat}.879*880* @since 1.2881* @see Comparable#compareTo(Object)882*/883public int compareTo(Float anotherFloat) {884return Float.compare(value, anotherFloat.value);885}886887/**888* Compares the two specified {@code float} values. The sign889* of the integer value returned is the same as that of the890* integer that would be returned by the call:891* <pre>892* new Float(f1).compareTo(new Float(f2))893* </pre>894*895* @param f1 the first {@code float} to compare.896* @param f2 the second {@code float} to compare.897* @return the value {@code 0} if {@code f1} is898* numerically equal to {@code f2}; a value less than899* {@code 0} if {@code f1} is numerically less than900* {@code f2}; and a value greater than {@code 0}901* if {@code f1} is numerically greater than902* {@code f2}.903* @since 1.4904*/905public static int compare(float f1, float f2) {906if (f1 < f2)907return -1; // Neither val is NaN, thisVal is smaller908if (f1 > f2)909return 1; // Neither val is NaN, thisVal is larger910911// Cannot use floatToRawIntBits because of possibility of NaNs.912int thisBits = Float.floatToIntBits(f1);913int anotherBits = Float.floatToIntBits(f2);914915return (thisBits == anotherBits ? 0 : // Values are equal916(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)9171)); // (0.0, -0.0) or (NaN, !NaN)918}919920/**921* Adds two {@code float} values together as per the + operator.922*923* @param a the first operand924* @param b the second operand925* @return the sum of {@code a} and {@code b}926* @jls 4.2.4 Floating-Point Operations927* @see java.util.function.BinaryOperator928* @since 1.8929*/930public static float sum(float a, float b) {931return a + b;932}933934/**935* Returns the greater of two {@code float} values936* as if by calling {@link Math#max(float, float) Math.max}.937*938* @param a the first operand939* @param b the second operand940* @return the greater of {@code a} and {@code b}941* @see java.util.function.BinaryOperator942* @since 1.8943*/944public static float max(float a, float b) {945return Math.max(a, b);946}947948/**949* Returns the smaller of two {@code float} values950* as if by calling {@link Math#min(float, float) Math.min}.951*952* @param a the first operand953* @param b the second operand954* @return the smaller of {@code a} and {@code b}955* @see java.util.function.BinaryOperator956* @since 1.8957*/958public static float min(float a, float b) {959return Math.min(a, b);960}961962/** use serialVersionUID from JDK 1.0.2 for interoperability */963private static final long serialVersionUID = -2671257302660747028L;964}965966967