Path: blob/master/src/java.base/share/classes/java/lang/Float.java
41152 views
/*1* Copyright (c) 1994, 2021, 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 java.lang.invoke.MethodHandles;28import java.lang.constant.Constable;29import java.lang.constant.ConstantDesc;30import java.util.Optional;3132import jdk.internal.math.FloatingDecimal;33import jdk.internal.vm.annotation.IntrinsicCandidate;3435/**36* The {@code Float} class wraps a value of primitive type37* {@code float} in an object. An object of type38* {@code Float} contains a single field whose type is39* {@code float}.40*41* <p>In addition, this class provides several methods for converting a42* {@code float} to a {@code String} and a43* {@code String} to a {@code float}, as well as other44* constants and methods useful when dealing with a45* {@code float}.46*47* <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>48* class; programmers should treat instances that are49* {@linkplain #equals(Object) equal} as interchangeable and should not50* use instances for synchronization, or unpredictable behavior may51* occur. For example, in a future release, synchronization may fail.52*53* <h2><a id=equivalenceRelation>Floating-point Equality, Equivalence,54* and Comparison</a></h2>55*56* The class {@code java.lang.Double} has a <a57* href="Double.html#equivalenceRelation">discussion of equality,58* equivalence, and comparison of floating-point values</a> that is59* equality applicable to {@code float} values.60*61* @author Lee Boynton62* @author Arthur van Hoff63* @author Joseph D. Darcy64* @since 1.065*/66@jdk.internal.ValueBased67public final class Float extends Number68implements Comparable<Float>, Constable, ConstantDesc {69/**70* A constant holding the positive infinity of type71* {@code float}. It is equal to the value returned by72* {@code Float.intBitsToFloat(0x7f800000)}.73*/74public static final float POSITIVE_INFINITY = 1.0f / 0.0f;7576/**77* A constant holding the negative infinity of type78* {@code float}. It is equal to the value returned by79* {@code Float.intBitsToFloat(0xff800000)}.80*/81public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;8283/**84* A constant holding a Not-a-Number (NaN) value of type85* {@code float}. It is equivalent to the value returned by86* {@code Float.intBitsToFloat(0x7fc00000)}.87*/88public static final float NaN = 0.0f / 0.0f;8990/**91* A constant holding the largest positive finite value of type92* {@code float}, (2-2<sup>-23</sup>)·2<sup>127</sup>.93* It is equal to the hexadecimal floating-point literal94* {@code 0x1.fffffeP+127f} and also equal to95* {@code Float.intBitsToFloat(0x7f7fffff)}.96*/97public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f9899/**100* A constant holding the smallest positive normal value of type101* {@code float}, 2<sup>-126</sup>. It is equal to the102* hexadecimal floating-point literal {@code 0x1.0p-126f} and also103* equal to {@code Float.intBitsToFloat(0x00800000)}.104*105* @since 1.6106*/107public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f108109/**110* A constant holding the smallest positive nonzero value of type111* {@code float}, 2<sup>-149</sup>. It is equal to the112* hexadecimal floating-point literal {@code 0x0.000002P-126f}113* and also equal to {@code Float.intBitsToFloat(0x1)}.114*/115public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f116117/**118* Maximum exponent a finite {@code float} variable may have. It119* is equal to the value returned by {@code120* Math.getExponent(Float.MAX_VALUE)}.121*122* @since 1.6123*/124public static final int MAX_EXPONENT = 127;125126/**127* Minimum exponent a normalized {@code float} variable may have.128* It is equal to the value returned by {@code129* Math.getExponent(Float.MIN_NORMAL)}.130*131* @since 1.6132*/133public static final int MIN_EXPONENT = -126;134135/**136* The number of bits used to represent a {@code float} value.137*138* @since 1.5139*/140public static final int SIZE = 32;141142/**143* The number of bytes used to represent a {@code float} value.144*145* @since 1.8146*/147public static final int BYTES = SIZE / Byte.SIZE;148149/**150* The {@code Class} instance representing the primitive type151* {@code float}.152*153* @since 1.1154*/155@SuppressWarnings("unchecked")156public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float");157158/**159* Returns a string representation of the {@code float}160* argument. All characters mentioned below are ASCII characters.161* <ul>162* <li>If the argument is NaN, the result is the string163* "{@code NaN}".164* <li>Otherwise, the result is a string that represents the sign and165* magnitude (absolute value) of the argument. If the sign is166* negative, the first character of the result is167* '{@code -}' ({@code '\u005Cu002D'}); if the sign is168* positive, no sign character appears in the result. As for169* the magnitude <i>m</i>:170* <ul>171* <li>If <i>m</i> is infinity, it is represented by the characters172* {@code "Infinity"}; thus, positive infinity produces173* the result {@code "Infinity"} and negative infinity174* produces the result {@code "-Infinity"}.175* <li>If <i>m</i> is zero, it is represented by the characters176* {@code "0.0"}; thus, negative zero produces the result177* {@code "-0.0"} and positive zero produces the result178* {@code "0.0"}.179* <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but180* less than 10<sup>7</sup>, then it is represented as the181* integer part of <i>m</i>, in decimal form with no leading182* zeroes, followed by '{@code .}'183* ({@code '\u005Cu002E'}), followed by one or more184* decimal digits representing the fractional part of185* <i>m</i>.186* <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or187* equal to 10<sup>7</sup>, then it is represented in188* so-called "computerized scientific notation." Let <i>n</i>189* be the unique integer such that 10<sup><i>n</i> </sup>≤190* <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>191* be the mathematically exact quotient of <i>m</i> and192* 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10.193* The magnitude is then represented as the integer part of194* <i>a</i>, as a single decimal digit, followed by195* '{@code .}' ({@code '\u005Cu002E'}), followed by196* decimal digits representing the fractional part of197* <i>a</i>, followed by the letter '{@code E}'198* ({@code '\u005Cu0045'}), followed by a representation199* of <i>n</i> as a decimal integer, as produced by the200* method {@link java.lang.Integer#toString(int)}.201*202* </ul>203* </ul>204* How many digits must be printed for the fractional part of205* <i>m</i> or <i>a</i>? There must be at least one digit206* to represent the fractional part, and beyond that as many, but207* only as many, more digits as are needed to uniquely distinguish208* the argument value from adjacent values of type209* {@code float}. That is, suppose that <i>x</i> is the210* exact mathematical value represented by the decimal211* representation produced by this method for a finite nonzero212* argument <i>f</i>. Then <i>f</i> must be the {@code float}213* value nearest to <i>x</i>; or, if two {@code float} values are214* equally close to <i>x</i>, then <i>f</i> must be one of215* them and the least significant bit of the significand of216* <i>f</i> must be {@code 0}.217*218* <p>To create localized string representations of a floating-point219* value, use subclasses of {@link java.text.NumberFormat}.220*221* @param f the float to be converted.222* @return a string representation of the argument.223*/224public static String toString(float f) {225return FloatingDecimal.toJavaFormatString(f);226}227228/**229* Returns a hexadecimal string representation of the230* {@code float} argument. All characters mentioned below are231* ASCII characters.232*233* <ul>234* <li>If the argument is NaN, the result is the string235* "{@code NaN}".236* <li>Otherwise, the result is a string that represents the sign and237* magnitude (absolute value) of the argument. If the sign is negative,238* the first character of the result is '{@code -}'239* ({@code '\u005Cu002D'}); if the sign is positive, no sign character240* appears in the result. As for the magnitude <i>m</i>:241*242* <ul>243* <li>If <i>m</i> is infinity, it is represented by the string244* {@code "Infinity"}; thus, positive infinity produces the245* result {@code "Infinity"} and negative infinity produces246* the result {@code "-Infinity"}.247*248* <li>If <i>m</i> is zero, it is represented by the string249* {@code "0x0.0p0"}; thus, negative zero produces the result250* {@code "-0x0.0p0"} and positive zero produces the result251* {@code "0x0.0p0"}.252*253* <li>If <i>m</i> is a {@code float} value with a254* normalized representation, substrings are used to represent the255* significand and exponent fields. The significand is256* represented by the characters {@code "0x1."}257* followed by a lowercase hexadecimal representation of the rest258* of the significand as a fraction. Trailing zeros in the259* hexadecimal representation are removed unless all the digits260* are zero, in which case a single zero is used. Next, the261* exponent is represented by {@code "p"} followed262* by a decimal string of the unbiased exponent as if produced by263* a call to {@link Integer#toString(int) Integer.toString} on the264* exponent value.265*266* <li>If <i>m</i> is a {@code float} value with a subnormal267* representation, the significand is represented by the268* characters {@code "0x0."} followed by a269* hexadecimal representation of the rest of the significand as a270* fraction. Trailing zeros in the hexadecimal representation are271* removed. Next, the exponent is represented by272* {@code "p-126"}. Note that there must be at273* least one nonzero digit in a subnormal significand.274*275* </ul>276*277* </ul>278*279* <table class="striped">280* <caption>Examples</caption>281* <thead>282* <tr><th scope="col">Floating-point Value</th><th scope="col">Hexadecimal String</th>283* </thead>284* <tbody>285* <tr><th scope="row">{@code 1.0}</th> <td>{@code 0x1.0p0}</td>286* <tr><th scope="row">{@code -1.0}</th> <td>{@code -0x1.0p0}</td>287* <tr><th scope="row">{@code 2.0}</th> <td>{@code 0x1.0p1}</td>288* <tr><th scope="row">{@code 3.0}</th> <td>{@code 0x1.8p1}</td>289* <tr><th scope="row">{@code 0.5}</th> <td>{@code 0x1.0p-1}</td>290* <tr><th scope="row">{@code 0.25}</th> <td>{@code 0x1.0p-2}</td>291* <tr><th scope="row">{@code Float.MAX_VALUE}</th>292* <td>{@code 0x1.fffffep127}</td>293* <tr><th scope="row">{@code Minimum Normal Value}</th>294* <td>{@code 0x1.0p-126}</td>295* <tr><th scope="row">{@code Maximum Subnormal Value}</th>296* <td>{@code 0x0.fffffep-126}</td>297* <tr><th scope="row">{@code Float.MIN_VALUE}</th>298* <td>{@code 0x0.000002p-126}</td>299* </tbody>300* </table>301* @param f the {@code float} to be converted.302* @return a hex string representation of the argument.303* @since 1.5304* @author Joseph D. Darcy305*/306public static String toHexString(float f) {307if (Math.abs(f) < Float.MIN_NORMAL308&& f != 0.0f ) {// float subnormal309// Adjust exponent to create subnormal double, then310// replace subnormal double exponent with subnormal float311// exponent312String s = Double.toHexString(Math.scalb((double)f,313/* -1022+126 */314Double.MIN_EXPONENT-315Float.MIN_EXPONENT));316return s.replaceFirst("p-1022$", "p-126");317}318else // double string will be the same as float string319return Double.toHexString(f);320}321322/**323* Returns a {@code Float} object holding the324* {@code float} value represented by the argument string325* {@code s}.326*327* <p>If {@code s} is {@code null}, then a328* {@code NullPointerException} is thrown.329*330* <p>Leading and trailing whitespace characters in {@code s}331* are ignored. Whitespace is removed as if by the {@link332* String#trim} method; that is, both ASCII space and control333* characters are removed. The rest of {@code s} should334* constitute a <i>FloatValue</i> as described by the lexical335* syntax rules:336*337* <blockquote>338* <dl>339* <dt><i>FloatValue:</i>340* <dd><i>Sign<sub>opt</sub></i> {@code NaN}341* <dd><i>Sign<sub>opt</sub></i> {@code Infinity}342* <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>343* <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>344* <dd><i>SignedInteger</i>345* </dl>346*347* <dl>348* <dt><i>HexFloatingPointLiteral</i>:349* <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>350* </dl>351*352* <dl>353* <dt><i>HexSignificand:</i>354* <dd><i>HexNumeral</i>355* <dd><i>HexNumeral</i> {@code .}356* <dd>{@code 0x} <i>HexDigits<sub>opt</sub>357* </i>{@code .}<i> HexDigits</i>358* <dd>{@code 0X}<i> HexDigits<sub>opt</sub>359* </i>{@code .} <i>HexDigits</i>360* </dl>361*362* <dl>363* <dt><i>BinaryExponent:</i>364* <dd><i>BinaryExponentIndicator SignedInteger</i>365* </dl>366*367* <dl>368* <dt><i>BinaryExponentIndicator:</i>369* <dd>{@code p}370* <dd>{@code P}371* </dl>372*373* </blockquote>374*375* where <i>Sign</i>, <i>FloatingPointLiteral</i>,376* <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and377* <i>FloatTypeSuffix</i> are as defined in the lexical structure378* sections of379* <cite>The Java Language Specification</cite>,380* except that underscores are not accepted between digits.381* If {@code s} does not have the form of382* a <i>FloatValue</i>, then a {@code NumberFormatException}383* is thrown. Otherwise, {@code s} is regarded as384* representing an exact decimal value in the usual385* "computerized scientific notation" or as an exact386* hexadecimal value; this exact numerical value is then387* conceptually converted to an "infinitely precise"388* binary value that is then rounded to type {@code float}389* by the usual round-to-nearest rule of IEEE 754 floating-point390* arithmetic, which includes preserving the sign of a zero391* value.392*393* Note that the round-to-nearest rule also implies overflow and394* underflow behaviour; if the exact value of {@code s} is large395* enough in magnitude (greater than or equal to ({@link396* #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2),397* rounding to {@code float} will result in an infinity and if the398* exact value of {@code s} is small enough in magnitude (less399* than or equal to {@link #MIN_VALUE}/2), rounding to float will400* result in a zero.401*402* Finally, after rounding a {@code Float} object representing403* this {@code float} value is returned.404*405* <p>To interpret localized string representations of a406* floating-point value, use subclasses of {@link407* java.text.NumberFormat}.408*409* <p>Note that trailing format specifiers, specifiers that410* determine the type of a floating-point literal411* ({@code 1.0f} is a {@code float} value;412* {@code 1.0d} is a {@code double} value), do413* <em>not</em> influence the results of this method. In other414* words, the numerical value of the input string is converted415* directly to the target floating-point type. In general, the416* two-step sequence of conversions, string to {@code double}417* followed by {@code double} to {@code float}, is418* <em>not</em> equivalent to converting a string directly to419* {@code float}. For example, if first converted to an420* intermediate {@code double} and then to421* {@code float}, the string<br>422* {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>423* results in the {@code float} value424* {@code 1.0000002f}; if the string is converted directly to425* {@code float}, <code>1.000000<b>1</b>f</code> results.426*427* <p>To avoid calling this method on an invalid string and having428* a {@code NumberFormatException} be thrown, the documentation429* for {@link Double#valueOf Double.valueOf} lists a regular430* expression which can be used to screen the input.431*432* @param s the string to be parsed.433* @return a {@code Float} object holding the value434* represented by the {@code String} argument.435* @throws NumberFormatException if the string does not contain a436* parsable number.437*/438public static Float valueOf(String s) throws NumberFormatException {439return new Float(parseFloat(s));440}441442/**443* Returns a {@code Float} instance representing the specified444* {@code float} value.445* If a new {@code Float} instance is not required, this method446* should generally be used in preference to the constructor447* {@link #Float(float)}, as this method is likely to yield448* significantly better space and time performance by caching449* frequently requested values.450*451* @param f a float value.452* @return a {@code Float} instance representing {@code f}.453* @since 1.5454*/455@IntrinsicCandidate456public static Float valueOf(float f) {457return new Float(f);458}459460/**461* Returns a new {@code float} initialized to the value462* represented by the specified {@code String}, as performed463* by the {@code valueOf} method of class {@code Float}.464*465* @param s the string to be parsed.466* @return the {@code float} value represented by the string467* argument.468* @throws NullPointerException if the string is null469* @throws NumberFormatException if the string does not contain a470* parsable {@code float}.471* @see java.lang.Float#valueOf(String)472* @since 1.2473*/474public static float parseFloat(String s) throws NumberFormatException {475return FloatingDecimal.parseFloat(s);476}477478/**479* Returns {@code true} if the specified number is a480* Not-a-Number (NaN) value, {@code false} otherwise.481*482* @param v the value to be tested.483* @return {@code true} if the argument is NaN;484* {@code false} otherwise.485*/486public static boolean isNaN(float v) {487return (v != v);488}489490/**491* Returns {@code true} if the specified number is infinitely492* large in magnitude, {@code false} otherwise.493*494* @param v the value to be tested.495* @return {@code true} if the argument is positive infinity or496* negative infinity; {@code false} otherwise.497*/498public static boolean isInfinite(float v) {499return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);500}501502503/**504* Returns {@code true} if the argument is a finite floating-point505* value; returns {@code false} otherwise (for NaN and infinity506* arguments).507*508* @param f the {@code float} value to be tested509* @return {@code true} if the argument is a finite510* floating-point value, {@code false} otherwise.511* @since 1.8512*/513public static boolean isFinite(float f) {514return Math.abs(f) <= Float.MAX_VALUE;515}516517/**518* The value of the Float.519*520* @serial521*/522private final float value;523524/**525* Constructs a newly allocated {@code Float} object that526* represents the primitive {@code float} argument.527*528* @param value the value to be represented by the {@code Float}.529*530* @deprecated531* It is rarely appropriate to use this constructor. The static factory532* {@link #valueOf(float)} is generally a better choice, as it is533* likely to yield significantly better space and time performance.534*/535@Deprecated(since="9", forRemoval = true)536public Float(float value) {537this.value = value;538}539540/**541* Constructs a newly allocated {@code Float} object that542* represents the argument converted to type {@code float}.543*544* @param value the value to be represented by the {@code Float}.545*546* @deprecated547* It is rarely appropriate to use this constructor. Instead, use the548* static factory method {@link #valueOf(float)} method as follows:549* {@code Float.valueOf((float)value)}.550*/551@Deprecated(since="9", forRemoval = true)552public Float(double value) {553this.value = (float)value;554}555556/**557* Constructs a newly allocated {@code Float} object that558* represents the floating-point value of type {@code float}559* represented by the string. The string is converted to a560* {@code float} value as if by the {@code valueOf} method.561*562* @param s a string to be converted to a {@code Float}.563* @throws NumberFormatException if the string does not contain a564* parsable number.565*566* @deprecated567* It is rarely appropriate to use this constructor.568* Use {@link #parseFloat(String)} to convert a string to a569* {@code float} primitive, or use {@link #valueOf(String)}570* to convert a string to a {@code Float} object.571*/572@Deprecated(since="9", forRemoval = true)573public Float(String s) throws NumberFormatException {574value = parseFloat(s);575}576577/**578* Returns {@code true} if this {@code Float} value is a579* Not-a-Number (NaN), {@code false} otherwise.580*581* @return {@code true} if the value represented by this object is582* NaN; {@code false} otherwise.583*/584public boolean isNaN() {585return isNaN(value);586}587588/**589* Returns {@code true} if this {@code Float} value is590* infinitely large in magnitude, {@code false} otherwise.591*592* @return {@code true} if the value represented by this object is593* positive infinity or negative infinity;594* {@code false} otherwise.595*/596public boolean isInfinite() {597return isInfinite(value);598}599600/**601* Returns a string representation of this {@code Float} object.602* The primitive {@code float} value represented by this object603* is converted to a {@code String} exactly as if by the method604* {@code toString} of one argument.605*606* @return a {@code String} representation of this object.607* @see java.lang.Float#toString(float)608*/609public String toString() {610return Float.toString(value);611}612613/**614* Returns the value of this {@code Float} as a {@code byte} after615* a narrowing primitive conversion.616*617* @return the {@code float} value represented by this object618* converted to type {@code byte}619* @jls 5.1.3 Narrowing Primitive Conversion620*/621public byte byteValue() {622return (byte)value;623}624625/**626* Returns the value of this {@code Float} as a {@code short}627* after a narrowing primitive conversion.628*629* @return the {@code float} value represented by this object630* converted to type {@code short}631* @jls 5.1.3 Narrowing Primitive Conversion632* @since 1.1633*/634public short shortValue() {635return (short)value;636}637638/**639* Returns the value of this {@code Float} as an {@code int} after640* a narrowing primitive conversion.641*642* @return the {@code float} value represented by this object643* converted to type {@code int}644* @jls 5.1.3 Narrowing Primitive Conversion645*/646public int intValue() {647return (int)value;648}649650/**651* Returns value of this {@code Float} as a {@code long} after a652* narrowing primitive conversion.653*654* @return the {@code float} value represented by this object655* converted to type {@code long}656* @jls 5.1.3 Narrowing Primitive Conversion657*/658public long longValue() {659return (long)value;660}661662/**663* Returns the {@code float} value of this {@code Float} object.664*665* @return the {@code float} value represented by this object666*/667@IntrinsicCandidate668public float floatValue() {669return value;670}671672/**673* Returns the value of this {@code Float} as a {@code double}674* after a widening primitive conversion.675*676* @return the {@code float} value represented by this677* object converted to type {@code double}678* @jls 5.1.2 Widening Primitive Conversion679*/680public double doubleValue() {681return (double)value;682}683684/**685* Returns a hash code for this {@code Float} object. The686* result is the integer bit representation, exactly as produced687* by the method {@link #floatToIntBits(float)}, of the primitive688* {@code float} value represented by this {@code Float}689* object.690*691* @return a hash code value for this object.692*/693@Override694public int hashCode() {695return Float.hashCode(value);696}697698/**699* Returns a hash code for a {@code float} value; compatible with700* {@code Float.hashCode()}.701*702* @param value the value to hash703* @return a hash code value for a {@code float} value.704* @since 1.8705*/706public static int hashCode(float value) {707return floatToIntBits(value);708}709710/**711* Compares this object against the specified object. The result712* is {@code true} if and only if the argument is not713* {@code null} and is a {@code Float} object that714* represents a {@code float} with the same value as the715* {@code float} represented by this object. For this716* purpose, two {@code float} values are considered to be the717* same if and only if the method {@link #floatToIntBits(float)}718* returns the identical {@code int} value when applied to719* each.720*721* @apiNote722* This method is defined in terms of {@link723* #floatToIntBits(float)} rather than the {@code ==} operator on724* {@code float} values since the {@code ==} operator does725* <em>not</em> define an equivalence relation and to satisfy the726* {@linkplain Object#equals equals contract} an equivalence727* relation must be implemented; see <a728* href="Double.html#equivalenceRelation">this discussion</a> for729* details of floating-point equality and equivalence.730*731* @param obj the object to be compared732* @return {@code true} if the objects are the same;733* {@code false} otherwise.734* @see java.lang.Float#floatToIntBits(float)735* @jls 15.21.1 Numerical Equality Operators == and !=736*/737public boolean equals(Object obj) {738return (obj instanceof Float)739&& (floatToIntBits(((Float)obj).value) == floatToIntBits(value));740}741742/**743* Returns a representation of the specified floating-point value744* according to the IEEE 754 floating-point "single format" bit745* layout.746*747* <p>Bit 31 (the bit that is selected by the mask748* {@code 0x80000000}) represents the sign of the floating-point749* number.750* Bits 30-23 (the bits that are selected by the mask751* {@code 0x7f800000}) represent the exponent.752* Bits 22-0 (the bits that are selected by the mask753* {@code 0x007fffff}) represent the significand (sometimes called754* the mantissa) of the floating-point number.755*756* <p>If the argument is positive infinity, the result is757* {@code 0x7f800000}.758*759* <p>If the argument is negative infinity, the result is760* {@code 0xff800000}.761*762* <p>If the argument is NaN, the result is {@code 0x7fc00000}.763*764* <p>In all cases, the result is an integer that, when given to the765* {@link #intBitsToFloat(int)} method, will produce a floating-point766* value the same as the argument to {@code floatToIntBits}767* (except all NaN values are collapsed to a single768* "canonical" NaN value).769*770* @param value a floating-point number.771* @return the bits that represent the floating-point number.772*/773@IntrinsicCandidate774public static int floatToIntBits(float value) {775if (!isNaN(value)) {776return floatToRawIntBits(value);777}778return 0x7fc00000;779}780781/**782* Returns a representation of the specified floating-point value783* according to the IEEE 754 floating-point "single format" bit784* layout, preserving Not-a-Number (NaN) values.785*786* <p>Bit 31 (the bit that is selected by the mask787* {@code 0x80000000}) represents the sign of the floating-point788* number.789* Bits 30-23 (the bits that are selected by the mask790* {@code 0x7f800000}) represent the exponent.791* Bits 22-0 (the bits that are selected by the mask792* {@code 0x007fffff}) represent the significand (sometimes called793* the mantissa) of the floating-point number.794*795* <p>If the argument is positive infinity, the result is796* {@code 0x7f800000}.797*798* <p>If the argument is negative infinity, the result is799* {@code 0xff800000}.800*801* <p>If the argument is NaN, the result is the integer representing802* the actual NaN value. Unlike the {@code floatToIntBits}803* method, {@code floatToRawIntBits} does not collapse all the804* bit patterns encoding a NaN to a single "canonical"805* NaN value.806*807* <p>In all cases, the result is an integer that, when given to the808* {@link #intBitsToFloat(int)} method, will produce a809* floating-point value the same as the argument to810* {@code floatToRawIntBits}.811*812* @param value a floating-point number.813* @return the bits that represent the floating-point number.814* @since 1.3815*/816@IntrinsicCandidate817public static native int floatToRawIntBits(float value);818819/**820* Returns the {@code float} value corresponding to a given821* bit representation.822* The argument is considered to be a representation of a823* floating-point value according to the IEEE 754 floating-point824* "single format" bit layout.825*826* <p>If the argument is {@code 0x7f800000}, the result is positive827* infinity.828*829* <p>If the argument is {@code 0xff800000}, the result is negative830* infinity.831*832* <p>If the argument is any value in the range833* {@code 0x7f800001} through {@code 0x7fffffff} or in834* the range {@code 0xff800001} through835* {@code 0xffffffff}, the result is a NaN. No IEEE 754836* floating-point operation provided by Java can distinguish837* between two NaN values of the same type with different bit838* patterns. Distinct values of NaN are only distinguishable by839* use of the {@code Float.floatToRawIntBits} method.840*841* <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three842* values that can be computed from the argument:843*844* <blockquote><pre>{@code845* int s = ((bits >> 31) == 0) ? 1 : -1;846* int e = ((bits >> 23) & 0xff);847* int m = (e == 0) ?848* (bits & 0x7fffff) << 1 :849* (bits & 0x7fffff) | 0x800000;850* }</pre></blockquote>851*852* Then the floating-point result equals the value of the mathematical853* expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-150</sup>.854*855* <p>Note that this method may not be able to return a856* {@code float} NaN with exactly same bit pattern as the857* {@code int} argument. IEEE 754 distinguishes between two858* kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The859* differences between the two kinds of NaN are generally not860* visible in Java. Arithmetic operations on signaling NaNs turn861* them into quiet NaNs with a different, but often similar, bit862* pattern. However, on some processors merely copying a863* signaling NaN also performs that conversion. In particular,864* copying a signaling NaN to return it to the calling method may865* perform this conversion. So {@code intBitsToFloat} may866* not be able to return a {@code float} with a signaling NaN867* bit pattern. Consequently, for some {@code int} values,868* {@code floatToRawIntBits(intBitsToFloat(start))} may869* <i>not</i> equal {@code start}. Moreover, which870* particular bit patterns represent signaling NaNs is platform871* dependent; although all NaN bit patterns, quiet or signaling,872* must be in the NaN range identified above.873*874* @param bits an integer.875* @return the {@code float} floating-point value with the same bit876* pattern.877*/878@IntrinsicCandidate879public static native float intBitsToFloat(int bits);880881/**882* Compares two {@code Float} objects numerically.883*884* This method imposes a total order on {@code Float} objects885* with two differences compared to the incomplete order defined by886* the Java language numerical comparison operators ({@code <, <=,887* ==, >=, >}) on {@code float} values.888*889* <ul><li> A NaN is <em>unordered</em> with respect to other890* values and unequal to itself under the comparison891* operators. This method chooses to define {@code892* Float.NaN} to be equal to itself and greater than all893* other {@code double} values (including {@code894* Float.POSITIVE_INFINITY}).895*896* <li> Positive zero and negative zero compare equal897* numerically, but are distinct and distinguishable values.898* This method chooses to define positive zero ({@code +0.0f}),899* to be greater than negative zero ({@code -0.0f}).900* </ul>901*902* This ensures that the <i>natural ordering</i> of {@code Float}903* objects imposed by this method is <i>consistent with904* equals</i>; see <a href="Double.html#equivalenceRelation">this905* discussion</a> for details of floating-point comparison and906* ordering.907*908*909* @param anotherFloat the {@code Float} to be compared.910* @return the value {@code 0} if {@code anotherFloat} is911* numerically equal to this {@code Float}; a value912* less than {@code 0} if this {@code Float}913* is numerically less than {@code anotherFloat};914* and a value greater than {@code 0} if this915* {@code Float} is numerically greater than916* {@code anotherFloat}.917*918* @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}919* @since 1.2920*/921public int compareTo(Float anotherFloat) {922return Float.compare(value, anotherFloat.value);923}924925/**926* Compares the two specified {@code float} values. The sign927* of the integer value returned is the same as that of the928* integer that would be returned by the call:929* <pre>930* new Float(f1).compareTo(new Float(f2))931* </pre>932*933* @param f1 the first {@code float} to compare.934* @param f2 the second {@code float} to compare.935* @return the value {@code 0} if {@code f1} is936* numerically equal to {@code f2}; a value less than937* {@code 0} if {@code f1} is numerically less than938* {@code f2}; and a value greater than {@code 0}939* if {@code f1} is numerically greater than940* {@code f2}.941* @since 1.4942*/943public static int compare(float f1, float f2) {944if (f1 < f2)945return -1; // Neither val is NaN, thisVal is smaller946if (f1 > f2)947return 1; // Neither val is NaN, thisVal is larger948949// Cannot use floatToRawIntBits because of possibility of NaNs.950int thisBits = Float.floatToIntBits(f1);951int anotherBits = Float.floatToIntBits(f2);952953return (thisBits == anotherBits ? 0 : // Values are equal954(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)9551)); // (0.0, -0.0) or (NaN, !NaN)956}957958/**959* Adds two {@code float} values together as per the + operator.960*961* @param a the first operand962* @param b the second operand963* @return the sum of {@code a} and {@code b}964* @jls 4.2.4 Floating-Point Operations965* @see java.util.function.BinaryOperator966* @since 1.8967*/968public static float sum(float a, float b) {969return a + b;970}971972/**973* Returns the greater of two {@code float} values974* as if by calling {@link Math#max(float, float) Math.max}.975*976* @param a the first operand977* @param b the second operand978* @return the greater of {@code a} and {@code b}979* @see java.util.function.BinaryOperator980* @since 1.8981*/982public static float max(float a, float b) {983return Math.max(a, b);984}985986/**987* Returns the smaller of two {@code float} values988* as if by calling {@link Math#min(float, float) Math.min}.989*990* @param a the first operand991* @param b the second operand992* @return the smaller of {@code a} and {@code b}993* @see java.util.function.BinaryOperator994* @since 1.8995*/996public static float min(float a, float b) {997return Math.min(a, b);998}9991000/**1001* Returns an {@link Optional} containing the nominal descriptor for this1002* instance, which is the instance itself.1003*1004* @return an {@link Optional} describing the {@linkplain Float} instance1005* @since 121006*/1007@Override1008public Optional<Float> describeConstable() {1009return Optional.of(this);1010}10111012/**1013* Resolves this instance as a {@link ConstantDesc}, the result of which is1014* the instance itself.1015*1016* @param lookup ignored1017* @return the {@linkplain Float} instance1018* @since 121019*/1020@Override1021public Float resolveConstantDesc(MethodHandles.Lookup lookup) {1022return this;1023}10241025/** use serialVersionUID from JDK 1.0.2 for interoperability */1026@java.io.Serial1027private static final long serialVersionUID = -2671257302660747028L;1028}102910301031