1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/java/lang/Float.java Wed Jan 23 20:39:23 2013 +0100
1.3 @@ -0,0 +1,905 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.32 +
1.33 +/**
1.34 + * The {@code Float} class wraps a value of primitive type
1.35 + * {@code float} in an object. An object of type
1.36 + * {@code Float} contains a single field whose type is
1.37 + * {@code float}.
1.38 + *
1.39 + * <p>In addition, this class provides several methods for converting a
1.40 + * {@code float} to a {@code String} and a
1.41 + * {@code String} to a {@code float}, as well as other
1.42 + * constants and methods useful when dealing with a
1.43 + * {@code float}.
1.44 + *
1.45 + * @author Lee Boynton
1.46 + * @author Arthur van Hoff
1.47 + * @author Joseph D. Darcy
1.48 + * @since JDK1.0
1.49 + */
1.50 +public final class Float extends Number implements Comparable<Float> {
1.51 + /**
1.52 + * A constant holding the positive infinity of type
1.53 + * {@code float}. It is equal to the value returned by
1.54 + * {@code Float.intBitsToFloat(0x7f800000)}.
1.55 + */
1.56 + public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
1.57 +
1.58 + /**
1.59 + * A constant holding the negative infinity of type
1.60 + * {@code float}. It is equal to the value returned by
1.61 + * {@code Float.intBitsToFloat(0xff800000)}.
1.62 + */
1.63 + public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
1.64 +
1.65 + /**
1.66 + * A constant holding a Not-a-Number (NaN) value of type
1.67 + * {@code float}. It is equivalent to the value returned by
1.68 + * {@code Float.intBitsToFloat(0x7fc00000)}.
1.69 + */
1.70 + public static final float NaN = 0.0f / 0.0f;
1.71 +
1.72 + /**
1.73 + * A constant holding the largest positive finite value of type
1.74 + * {@code float}, (2-2<sup>-23</sup>)·2<sup>127</sup>.
1.75 + * It is equal to the hexadecimal floating-point literal
1.76 + * {@code 0x1.fffffeP+127f} and also equal to
1.77 + * {@code Float.intBitsToFloat(0x7f7fffff)}.
1.78 + */
1.79 + public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f
1.80 +
1.81 + /**
1.82 + * A constant holding the smallest positive normal value of type
1.83 + * {@code float}, 2<sup>-126</sup>. It is equal to the
1.84 + * hexadecimal floating-point literal {@code 0x1.0p-126f} and also
1.85 + * equal to {@code Float.intBitsToFloat(0x00800000)}.
1.86 + *
1.87 + * @since 1.6
1.88 + */
1.89 + public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f
1.90 +
1.91 + /**
1.92 + * A constant holding the smallest positive nonzero value of type
1.93 + * {@code float}, 2<sup>-149</sup>. It is equal to the
1.94 + * hexadecimal floating-point literal {@code 0x0.000002P-126f}
1.95 + * and also equal to {@code Float.intBitsToFloat(0x1)}.
1.96 + */
1.97 + public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f
1.98 +
1.99 + /**
1.100 + * Maximum exponent a finite {@code float} variable may have. It
1.101 + * is equal to the value returned by {@code
1.102 + * Math.getExponent(Float.MAX_VALUE)}.
1.103 + *
1.104 + * @since 1.6
1.105 + */
1.106 + public static final int MAX_EXPONENT = 127;
1.107 +
1.108 + /**
1.109 + * Minimum exponent a normalized {@code float} variable may have.
1.110 + * It is equal to the value returned by {@code
1.111 + * Math.getExponent(Float.MIN_NORMAL)}.
1.112 + *
1.113 + * @since 1.6
1.114 + */
1.115 + public static final int MIN_EXPONENT = -126;
1.116 +
1.117 + /**
1.118 + * The number of bits used to represent a {@code float} value.
1.119 + *
1.120 + * @since 1.5
1.121 + */
1.122 + public static final int SIZE = 32;
1.123 +
1.124 + /**
1.125 + * The {@code Class} instance representing the primitive type
1.126 + * {@code float}.
1.127 + *
1.128 + * @since JDK1.1
1.129 + */
1.130 + public static final Class<Float> TYPE = Class.getPrimitiveClass("float");
1.131 +
1.132 + /**
1.133 + * Returns a string representation of the {@code float}
1.134 + * argument. All characters mentioned below are ASCII characters.
1.135 + * <ul>
1.136 + * <li>If the argument is NaN, the result is the string
1.137 + * "{@code NaN}".
1.138 + * <li>Otherwise, the result is a string that represents the sign and
1.139 + * magnitude (absolute value) of the argument. If the sign is
1.140 + * negative, the first character of the result is
1.141 + * '{@code -}' (<code>'\u002D'</code>); if the sign is
1.142 + * positive, no sign character appears in the result. As for
1.143 + * the magnitude <i>m</i>:
1.144 + * <ul>
1.145 + * <li>If <i>m</i> is infinity, it is represented by the characters
1.146 + * {@code "Infinity"}; thus, positive infinity produces
1.147 + * the result {@code "Infinity"} and negative infinity
1.148 + * produces the result {@code "-Infinity"}.
1.149 + * <li>If <i>m</i> is zero, it is represented by the characters
1.150 + * {@code "0.0"}; thus, negative zero produces the result
1.151 + * {@code "-0.0"} and positive zero produces the result
1.152 + * {@code "0.0"}.
1.153 + * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but
1.154 + * less than 10<sup>7</sup>, then it is represented as the
1.155 + * integer part of <i>m</i>, in decimal form with no leading
1.156 + * zeroes, followed by '{@code .}'
1.157 + * (<code>'\u002E'</code>), followed by one or more
1.158 + * decimal digits representing the fractional part of
1.159 + * <i>m</i>.
1.160 + * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or
1.161 + * equal to 10<sup>7</sup>, then it is represented in
1.162 + * so-called "computerized scientific notation." Let <i>n</i>
1.163 + * be the unique integer such that 10<sup><i>n</i> </sup>≤
1.164 + * <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>
1.165 + * be the mathematically exact quotient of <i>m</i> and
1.166 + * 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10.
1.167 + * The magnitude is then represented as the integer part of
1.168 + * <i>a</i>, as a single decimal digit, followed by
1.169 + * '{@code .}' (<code>'\u002E'</code>), followed by
1.170 + * decimal digits representing the fractional part of
1.171 + * <i>a</i>, followed by the letter '{@code E}'
1.172 + * (<code>'\u0045'</code>), followed by a representation
1.173 + * of <i>n</i> as a decimal integer, as produced by the
1.174 + * method {@link java.lang.Integer#toString(int)}.
1.175 + *
1.176 + * </ul>
1.177 + * </ul>
1.178 + * How many digits must be printed for the fractional part of
1.179 + * <i>m</i> or <i>a</i>? There must be at least one digit
1.180 + * to represent the fractional part, and beyond that as many, but
1.181 + * only as many, more digits as are needed to uniquely distinguish
1.182 + * the argument value from adjacent values of type
1.183 + * {@code float}. That is, suppose that <i>x</i> is the
1.184 + * exact mathematical value represented by the decimal
1.185 + * representation produced by this method for a finite nonzero
1.186 + * argument <i>f</i>. Then <i>f</i> must be the {@code float}
1.187 + * value nearest to <i>x</i>; or, if two {@code float} values are
1.188 + * equally close to <i>x</i>, then <i>f</i> must be one of
1.189 + * them and the least significant bit of the significand of
1.190 + * <i>f</i> must be {@code 0}.
1.191 + *
1.192 + * <p>To create localized string representations of a floating-point
1.193 + * value, use subclasses of {@link java.text.NumberFormat}.
1.194 + *
1.195 + * @param f the float to be converted.
1.196 + * @return a string representation of the argument.
1.197 + */
1.198 + public static String toString(float f) {
1.199 + return Double.toString(f);
1.200 + }
1.201 +
1.202 + /**
1.203 + * Returns a hexadecimal string representation of the
1.204 + * {@code float} argument. All characters mentioned below are
1.205 + * ASCII characters.
1.206 + *
1.207 + * <ul>
1.208 + * <li>If the argument is NaN, the result is the string
1.209 + * "{@code NaN}".
1.210 + * <li>Otherwise, the result is a string that represents the sign and
1.211 + * magnitude (absolute value) of the argument. If the sign is negative,
1.212 + * the first character of the result is '{@code -}'
1.213 + * (<code>'\u002D'</code>); if the sign is positive, no sign character
1.214 + * appears in the result. As for the magnitude <i>m</i>:
1.215 + *
1.216 + * <ul>
1.217 + * <li>If <i>m</i> is infinity, it is represented by the string
1.218 + * {@code "Infinity"}; thus, positive infinity produces the
1.219 + * result {@code "Infinity"} and negative infinity produces
1.220 + * the result {@code "-Infinity"}.
1.221 + *
1.222 + * <li>If <i>m</i> is zero, it is represented by the string
1.223 + * {@code "0x0.0p0"}; thus, negative zero produces the result
1.224 + * {@code "-0x0.0p0"} and positive zero produces the result
1.225 + * {@code "0x0.0p0"}.
1.226 + *
1.227 + * <li>If <i>m</i> is a {@code float} value with a
1.228 + * normalized representation, substrings are used to represent the
1.229 + * significand and exponent fields. The significand is
1.230 + * represented by the characters {@code "0x1."}
1.231 + * followed by a lowercase hexadecimal representation of the rest
1.232 + * of the significand as a fraction. Trailing zeros in the
1.233 + * hexadecimal representation are removed unless all the digits
1.234 + * are zero, in which case a single zero is used. Next, the
1.235 + * exponent is represented by {@code "p"} followed
1.236 + * by a decimal string of the unbiased exponent as if produced by
1.237 + * a call to {@link Integer#toString(int) Integer.toString} on the
1.238 + * exponent value.
1.239 + *
1.240 + * <li>If <i>m</i> is a {@code float} value with a subnormal
1.241 + * representation, the significand is represented by the
1.242 + * characters {@code "0x0."} followed by a
1.243 + * hexadecimal representation of the rest of the significand as a
1.244 + * fraction. Trailing zeros in the hexadecimal representation are
1.245 + * removed. Next, the exponent is represented by
1.246 + * {@code "p-126"}. Note that there must be at
1.247 + * least one nonzero digit in a subnormal significand.
1.248 + *
1.249 + * </ul>
1.250 + *
1.251 + * </ul>
1.252 + *
1.253 + * <table border>
1.254 + * <caption><h3>Examples</h3></caption>
1.255 + * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
1.256 + * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
1.257 + * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
1.258 + * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
1.259 + * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
1.260 + * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
1.261 + * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
1.262 + * <tr><td>{@code Float.MAX_VALUE}</td>
1.263 + * <td>{@code 0x1.fffffep127}</td>
1.264 + * <tr><td>{@code Minimum Normal Value}</td>
1.265 + * <td>{@code 0x1.0p-126}</td>
1.266 + * <tr><td>{@code Maximum Subnormal Value}</td>
1.267 + * <td>{@code 0x0.fffffep-126}</td>
1.268 + * <tr><td>{@code Float.MIN_VALUE}</td>
1.269 + * <td>{@code 0x0.000002p-126}</td>
1.270 + * </table>
1.271 + * @param f the {@code float} to be converted.
1.272 + * @return a hex string representation of the argument.
1.273 + * @since 1.5
1.274 + * @author Joseph D. Darcy
1.275 + */
1.276 + public static String toHexString(float f) {
1.277 + throw new UnsupportedOperationException();
1.278 +// if (Math.abs(f) < FloatConsts.MIN_NORMAL
1.279 +// && f != 0.0f ) {// float subnormal
1.280 +// // Adjust exponent to create subnormal double, then
1.281 +// // replace subnormal double exponent with subnormal float
1.282 +// // exponent
1.283 +// String s = Double.toHexString(FpUtils.scalb((double)f,
1.284 +// /* -1022+126 */
1.285 +// DoubleConsts.MIN_EXPONENT-
1.286 +// FloatConsts.MIN_EXPONENT));
1.287 +// return s.replaceFirst("p-1022$", "p-126");
1.288 +// }
1.289 +// else // double string will be the same as float string
1.290 +// return Double.toHexString(f);
1.291 + }
1.292 +
1.293 + /**
1.294 + * Returns a {@code Float} object holding the
1.295 + * {@code float} value represented by the argument string
1.296 + * {@code s}.
1.297 + *
1.298 + * <p>If {@code s} is {@code null}, then a
1.299 + * {@code NullPointerException} is thrown.
1.300 + *
1.301 + * <p>Leading and trailing whitespace characters in {@code s}
1.302 + * are ignored. Whitespace is removed as if by the {@link
1.303 + * String#trim} method; that is, both ASCII space and control
1.304 + * characters are removed. The rest of {@code s} should
1.305 + * constitute a <i>FloatValue</i> as described by the lexical
1.306 + * syntax rules:
1.307 + *
1.308 + * <blockquote>
1.309 + * <dl>
1.310 + * <dt><i>FloatValue:</i>
1.311 + * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
1.312 + * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
1.313 + * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
1.314 + * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
1.315 + * <dd><i>SignedInteger</i>
1.316 + * </dl>
1.317 + *
1.318 + * <p>
1.319 + *
1.320 + * <dl>
1.321 + * <dt><i>HexFloatingPointLiteral</i>:
1.322 + * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
1.323 + * </dl>
1.324 + *
1.325 + * <p>
1.326 + *
1.327 + * <dl>
1.328 + * <dt><i>HexSignificand:</i>
1.329 + * <dd><i>HexNumeral</i>
1.330 + * <dd><i>HexNumeral</i> {@code .}
1.331 + * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
1.332 + * </i>{@code .}<i> HexDigits</i>
1.333 + * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
1.334 + * </i>{@code .} <i>HexDigits</i>
1.335 + * </dl>
1.336 + *
1.337 + * <p>
1.338 + *
1.339 + * <dl>
1.340 + * <dt><i>BinaryExponent:</i>
1.341 + * <dd><i>BinaryExponentIndicator SignedInteger</i>
1.342 + * </dl>
1.343 + *
1.344 + * <p>
1.345 + *
1.346 + * <dl>
1.347 + * <dt><i>BinaryExponentIndicator:</i>
1.348 + * <dd>{@code p}
1.349 + * <dd>{@code P}
1.350 + * </dl>
1.351 + *
1.352 + * </blockquote>
1.353 + *
1.354 + * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
1.355 + * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
1.356 + * <i>FloatTypeSuffix</i> are as defined in the lexical structure
1.357 + * sections of
1.358 + * <cite>The Java™ Language Specification</cite>,
1.359 + * except that underscores are not accepted between digits.
1.360 + * If {@code s} does not have the form of
1.361 + * a <i>FloatValue</i>, then a {@code NumberFormatException}
1.362 + * is thrown. Otherwise, {@code s} is regarded as
1.363 + * representing an exact decimal value in the usual
1.364 + * "computerized scientific notation" or as an exact
1.365 + * hexadecimal value; this exact numerical value is then
1.366 + * conceptually converted to an "infinitely precise"
1.367 + * binary value that is then rounded to type {@code float}
1.368 + * by the usual round-to-nearest rule of IEEE 754 floating-point
1.369 + * arithmetic, which includes preserving the sign of a zero
1.370 + * value.
1.371 + *
1.372 + * Note that the round-to-nearest rule also implies overflow and
1.373 + * underflow behaviour; if the exact value of {@code s} is large
1.374 + * enough in magnitude (greater than or equal to ({@link
1.375 + * #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2),
1.376 + * rounding to {@code float} will result in an infinity and if the
1.377 + * exact value of {@code s} is small enough in magnitude (less
1.378 + * than or equal to {@link #MIN_VALUE}/2), rounding to float will
1.379 + * result in a zero.
1.380 + *
1.381 + * Finally, after rounding a {@code Float} object representing
1.382 + * this {@code float} value is returned.
1.383 + *
1.384 + * <p>To interpret localized string representations of a
1.385 + * floating-point value, use subclasses of {@link
1.386 + * java.text.NumberFormat}.
1.387 + *
1.388 + * <p>Note that trailing format specifiers, specifiers that
1.389 + * determine the type of a floating-point literal
1.390 + * ({@code 1.0f} is a {@code float} value;
1.391 + * {@code 1.0d} is a {@code double} value), do
1.392 + * <em>not</em> influence the results of this method. In other
1.393 + * words, the numerical value of the input string is converted
1.394 + * directly to the target floating-point type. In general, the
1.395 + * two-step sequence of conversions, string to {@code double}
1.396 + * followed by {@code double} to {@code float}, is
1.397 + * <em>not</em> equivalent to converting a string directly to
1.398 + * {@code float}. For example, if first converted to an
1.399 + * intermediate {@code double} and then to
1.400 + * {@code float}, the string<br>
1.401 + * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>
1.402 + * results in the {@code float} value
1.403 + * {@code 1.0000002f}; if the string is converted directly to
1.404 + * {@code float}, <code>1.000000<b>1</b>f</code> results.
1.405 + *
1.406 + * <p>To avoid calling this method on an invalid string and having
1.407 + * a {@code NumberFormatException} be thrown, the documentation
1.408 + * for {@link Double#valueOf Double.valueOf} lists a regular
1.409 + * expression which can be used to screen the input.
1.410 + *
1.411 + * @param s the string to be parsed.
1.412 + * @return a {@code Float} object holding the value
1.413 + * represented by the {@code String} argument.
1.414 + * @throws NumberFormatException if the string does not contain a
1.415 + * parsable number.
1.416 + */
1.417 + public static Float valueOf(String s) throws NumberFormatException {
1.418 + throw new UnsupportedOperationException();
1.419 +// return new Float(FloatingDecimal.readJavaFormatString(s).floatValue());
1.420 + }
1.421 +
1.422 + /**
1.423 + * Returns a {@code Float} instance representing the specified
1.424 + * {@code float} value.
1.425 + * If a new {@code Float} instance is not required, this method
1.426 + * should generally be used in preference to the constructor
1.427 + * {@link #Float(float)}, as this method is likely to yield
1.428 + * significantly better space and time performance by caching
1.429 + * frequently requested values.
1.430 + *
1.431 + * @param f a float value.
1.432 + * @return a {@code Float} instance representing {@code f}.
1.433 + * @since 1.5
1.434 + */
1.435 + public static Float valueOf(float f) {
1.436 + return new Float(f);
1.437 + }
1.438 +
1.439 + /**
1.440 + * Returns a new {@code float} initialized to the value
1.441 + * represented by the specified {@code String}, as performed
1.442 + * by the {@code valueOf} method of class {@code Float}.
1.443 + *
1.444 + * @param s the string to be parsed.
1.445 + * @return the {@code float} value represented by the string
1.446 + * argument.
1.447 + * @throws NullPointerException if the string is null
1.448 + * @throws NumberFormatException if the string does not contain a
1.449 + * parsable {@code float}.
1.450 + * @see java.lang.Float#valueOf(String)
1.451 + * @since 1.2
1.452 + */
1.453 + public static float parseFloat(String s) throws NumberFormatException {
1.454 + throw new UnsupportedOperationException();
1.455 +// return FloatingDecimal.readJavaFormatString(s).floatValue();
1.456 + }
1.457 +
1.458 + /**
1.459 + * Returns {@code true} if the specified number is a
1.460 + * Not-a-Number (NaN) value, {@code false} otherwise.
1.461 + *
1.462 + * @param v the value to be tested.
1.463 + * @return {@code true} if the argument is NaN;
1.464 + * {@code false} otherwise.
1.465 + */
1.466 + static public boolean isNaN(float v) {
1.467 + return (v != v);
1.468 + }
1.469 +
1.470 + /**
1.471 + * Returns {@code true} if the specified number is infinitely
1.472 + * large in magnitude, {@code false} otherwise.
1.473 + *
1.474 + * @param v the value to be tested.
1.475 + * @return {@code true} if the argument is positive infinity or
1.476 + * negative infinity; {@code false} otherwise.
1.477 + */
1.478 + static public boolean isInfinite(float v) {
1.479 + return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
1.480 + }
1.481 +
1.482 + /**
1.483 + * The value of the Float.
1.484 + *
1.485 + * @serial
1.486 + */
1.487 + private final float value;
1.488 +
1.489 + /**
1.490 + * Constructs a newly allocated {@code Float} object that
1.491 + * represents the primitive {@code float} argument.
1.492 + *
1.493 + * @param value the value to be represented by the {@code Float}.
1.494 + */
1.495 + public Float(float value) {
1.496 + this.value = value;
1.497 + }
1.498 +
1.499 + /**
1.500 + * Constructs a newly allocated {@code Float} object that
1.501 + * represents the argument converted to type {@code float}.
1.502 + *
1.503 + * @param value the value to be represented by the {@code Float}.
1.504 + */
1.505 + public Float(double value) {
1.506 + this.value = (float)value;
1.507 + }
1.508 +
1.509 + /**
1.510 + * Constructs a newly allocated {@code Float} object that
1.511 + * represents the floating-point value of type {@code float}
1.512 + * represented by the string. The string is converted to a
1.513 + * {@code float} value as if by the {@code valueOf} method.
1.514 + *
1.515 + * @param s a string to be converted to a {@code Float}.
1.516 + * @throws NumberFormatException if the string does not contain a
1.517 + * parsable number.
1.518 + * @see java.lang.Float#valueOf(java.lang.String)
1.519 + */
1.520 + public Float(String s) throws NumberFormatException {
1.521 + // REMIND: this is inefficient
1.522 + this(valueOf(s).floatValue());
1.523 + }
1.524 +
1.525 + /**
1.526 + * Returns {@code true} if this {@code Float} value is a
1.527 + * Not-a-Number (NaN), {@code false} otherwise.
1.528 + *
1.529 + * @return {@code true} if the value represented by this object is
1.530 + * NaN; {@code false} otherwise.
1.531 + */
1.532 + public boolean isNaN() {
1.533 + return isNaN(value);
1.534 + }
1.535 +
1.536 + /**
1.537 + * Returns {@code true} if this {@code Float} value is
1.538 + * infinitely large in magnitude, {@code false} otherwise.
1.539 + *
1.540 + * @return {@code true} if the value represented by this object is
1.541 + * positive infinity or negative infinity;
1.542 + * {@code false} otherwise.
1.543 + */
1.544 + public boolean isInfinite() {
1.545 + return isInfinite(value);
1.546 + }
1.547 +
1.548 + /**
1.549 + * Returns a string representation of this {@code Float} object.
1.550 + * The primitive {@code float} value represented by this object
1.551 + * is converted to a {@code String} exactly as if by the method
1.552 + * {@code toString} of one argument.
1.553 + *
1.554 + * @return a {@code String} representation of this object.
1.555 + * @see java.lang.Float#toString(float)
1.556 + */
1.557 + public String toString() {
1.558 + return Float.toString(value);
1.559 + }
1.560 +
1.561 + /**
1.562 + * Returns the value of this {@code Float} as a {@code byte} (by
1.563 + * casting to a {@code byte}).
1.564 + *
1.565 + * @return the {@code float} value represented by this object
1.566 + * converted to type {@code byte}
1.567 + */
1.568 + public byte byteValue() {
1.569 + return (byte)value;
1.570 + }
1.571 +
1.572 + /**
1.573 + * Returns the value of this {@code Float} as a {@code short} (by
1.574 + * casting to a {@code short}).
1.575 + *
1.576 + * @return the {@code float} value represented by this object
1.577 + * converted to type {@code short}
1.578 + * @since JDK1.1
1.579 + */
1.580 + public short shortValue() {
1.581 + return (short)value;
1.582 + }
1.583 +
1.584 + /**
1.585 + * Returns the value of this {@code Float} as an {@code int} (by
1.586 + * casting to type {@code int}).
1.587 + *
1.588 + * @return the {@code float} value represented by this object
1.589 + * converted to type {@code int}
1.590 + */
1.591 + public int intValue() {
1.592 + return (int)value;
1.593 + }
1.594 +
1.595 + /**
1.596 + * Returns value of this {@code Float} as a {@code long} (by
1.597 + * casting to type {@code long}).
1.598 + *
1.599 + * @return the {@code float} value represented by this object
1.600 + * converted to type {@code long}
1.601 + */
1.602 + public long longValue() {
1.603 + return (long)value;
1.604 + }
1.605 +
1.606 + /**
1.607 + * Returns the {@code float} value of this {@code Float} object.
1.608 + *
1.609 + * @return the {@code float} value represented by this object
1.610 + */
1.611 + public float floatValue() {
1.612 + return value;
1.613 + }
1.614 +
1.615 + /**
1.616 + * Returns the {@code double} value of this {@code Float} object.
1.617 + *
1.618 + * @return the {@code float} value represented by this
1.619 + * object is converted to type {@code double} and the
1.620 + * result of the conversion is returned.
1.621 + */
1.622 + public double doubleValue() {
1.623 + return (double)value;
1.624 + }
1.625 +
1.626 + /**
1.627 + * Returns a hash code for this {@code Float} object. The
1.628 + * result is the integer bit representation, exactly as produced
1.629 + * by the method {@link #floatToIntBits(float)}, of the primitive
1.630 + * {@code float} value represented by this {@code Float}
1.631 + * object.
1.632 + *
1.633 + * @return a hash code value for this object.
1.634 + */
1.635 + public int hashCode() {
1.636 + return floatToIntBits(value);
1.637 + }
1.638 +
1.639 + /**
1.640 +
1.641 + * Compares this object against the specified object. The result
1.642 + * is {@code true} if and only if the argument is not
1.643 + * {@code null} and is a {@code Float} object that
1.644 + * represents a {@code float} with the same value as the
1.645 + * {@code float} represented by this object. For this
1.646 + * purpose, two {@code float} values are considered to be the
1.647 + * same if and only if the method {@link #floatToIntBits(float)}
1.648 + * returns the identical {@code int} value when applied to
1.649 + * each.
1.650 + *
1.651 + * <p>Note that in most cases, for two instances of class
1.652 + * {@code Float}, {@code f1} and {@code f2}, the value
1.653 + * of {@code f1.equals(f2)} is {@code true} if and only if
1.654 + *
1.655 + * <blockquote><pre>
1.656 + * f1.floatValue() == f2.floatValue()
1.657 + * </pre></blockquote>
1.658 + *
1.659 + * <p>also has the value {@code true}. However, there are two exceptions:
1.660 + * <ul>
1.661 + * <li>If {@code f1} and {@code f2} both represent
1.662 + * {@code Float.NaN}, then the {@code equals} method returns
1.663 + * {@code true}, even though {@code Float.NaN==Float.NaN}
1.664 + * has the value {@code false}.
1.665 + * <li>If {@code f1} represents {@code +0.0f} while
1.666 + * {@code f2} represents {@code -0.0f}, or vice
1.667 + * versa, the {@code equal} test has the value
1.668 + * {@code false}, even though {@code 0.0f==-0.0f}
1.669 + * has the value {@code true}.
1.670 + * </ul>
1.671 + *
1.672 + * This definition allows hash tables to operate properly.
1.673 + *
1.674 + * @param obj the object to be compared
1.675 + * @return {@code true} if the objects are the same;
1.676 + * {@code false} otherwise.
1.677 + * @see java.lang.Float#floatToIntBits(float)
1.678 + */
1.679 + public boolean equals(Object obj) {
1.680 + return (obj instanceof Float)
1.681 + && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
1.682 + }
1.683 +
1.684 + /**
1.685 + * Returns a representation of the specified floating-point value
1.686 + * according to the IEEE 754 floating-point "single format" bit
1.687 + * layout.
1.688 + *
1.689 + * <p>Bit 31 (the bit that is selected by the mask
1.690 + * {@code 0x80000000}) represents the sign of the floating-point
1.691 + * number.
1.692 + * Bits 30-23 (the bits that are selected by the mask
1.693 + * {@code 0x7f800000}) represent the exponent.
1.694 + * Bits 22-0 (the bits that are selected by the mask
1.695 + * {@code 0x007fffff}) represent the significand (sometimes called
1.696 + * the mantissa) of the floating-point number.
1.697 + *
1.698 + * <p>If the argument is positive infinity, the result is
1.699 + * {@code 0x7f800000}.
1.700 + *
1.701 + * <p>If the argument is negative infinity, the result is
1.702 + * {@code 0xff800000}.
1.703 + *
1.704 + * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
1.705 + *
1.706 + * <p>In all cases, the result is an integer that, when given to the
1.707 + * {@link #intBitsToFloat(int)} method, will produce a floating-point
1.708 + * value the same as the argument to {@code floatToIntBits}
1.709 + * (except all NaN values are collapsed to a single
1.710 + * "canonical" NaN value).
1.711 + *
1.712 + * @param value a floating-point number.
1.713 + * @return the bits that represent the floating-point number.
1.714 + */
1.715 + public static int floatToIntBits(float value) {
1.716 + throw new UnsupportedOperationException();
1.717 +// int result = floatToRawIntBits(value);
1.718 +// // Check for NaN based on values of bit fields, maximum
1.719 +// // exponent and nonzero significand.
1.720 +// if ( ((result & FloatConsts.EXP_BIT_MASK) ==
1.721 +// FloatConsts.EXP_BIT_MASK) &&
1.722 +// (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
1.723 +// result = 0x7fc00000;
1.724 +// return result;
1.725 + }
1.726 +
1.727 + /**
1.728 + * Returns a representation of the specified floating-point value
1.729 + * according to the IEEE 754 floating-point "single format" bit
1.730 + * layout, preserving Not-a-Number (NaN) values.
1.731 + *
1.732 + * <p>Bit 31 (the bit that is selected by the mask
1.733 + * {@code 0x80000000}) represents the sign of the floating-point
1.734 + * number.
1.735 + * Bits 30-23 (the bits that are selected by the mask
1.736 + * {@code 0x7f800000}) represent the exponent.
1.737 + * Bits 22-0 (the bits that are selected by the mask
1.738 + * {@code 0x007fffff}) represent the significand (sometimes called
1.739 + * the mantissa) of the floating-point number.
1.740 + *
1.741 + * <p>If the argument is positive infinity, the result is
1.742 + * {@code 0x7f800000}.
1.743 + *
1.744 + * <p>If the argument is negative infinity, the result is
1.745 + * {@code 0xff800000}.
1.746 + *
1.747 + * <p>If the argument is NaN, the result is the integer representing
1.748 + * the actual NaN value. Unlike the {@code floatToIntBits}
1.749 + * method, {@code floatToRawIntBits} does not collapse all the
1.750 + * bit patterns encoding a NaN to a single "canonical"
1.751 + * NaN value.
1.752 + *
1.753 + * <p>In all cases, the result is an integer that, when given to the
1.754 + * {@link #intBitsToFloat(int)} method, will produce a
1.755 + * floating-point value the same as the argument to
1.756 + * {@code floatToRawIntBits}.
1.757 + *
1.758 + * @param value a floating-point number.
1.759 + * @return the bits that represent the floating-point number.
1.760 + * @since 1.3
1.761 + */
1.762 + public static native int floatToRawIntBits(float value);
1.763 +
1.764 + /**
1.765 + * Returns the {@code float} value corresponding to a given
1.766 + * bit representation.
1.767 + * The argument is considered to be a representation of a
1.768 + * floating-point value according to the IEEE 754 floating-point
1.769 + * "single format" bit layout.
1.770 + *
1.771 + * <p>If the argument is {@code 0x7f800000}, the result is positive
1.772 + * infinity.
1.773 + *
1.774 + * <p>If the argument is {@code 0xff800000}, the result is negative
1.775 + * infinity.
1.776 + *
1.777 + * <p>If the argument is any value in the range
1.778 + * {@code 0x7f800001} through {@code 0x7fffffff} or in
1.779 + * the range {@code 0xff800001} through
1.780 + * {@code 0xffffffff}, the result is a NaN. No IEEE 754
1.781 + * floating-point operation provided by Java can distinguish
1.782 + * between two NaN values of the same type with different bit
1.783 + * patterns. Distinct values of NaN are only distinguishable by
1.784 + * use of the {@code Float.floatToRawIntBits} method.
1.785 + *
1.786 + * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
1.787 + * values that can be computed from the argument:
1.788 + *
1.789 + * <blockquote><pre>
1.790 + * int s = ((bits >> 31) == 0) ? 1 : -1;
1.791 + * int e = ((bits >> 23) & 0xff);
1.792 + * int m = (e == 0) ?
1.793 + * (bits & 0x7fffff) << 1 :
1.794 + * (bits & 0x7fffff) | 0x800000;
1.795 + * </pre></blockquote>
1.796 + *
1.797 + * Then the floating-point result equals the value of the mathematical
1.798 + * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-150</sup>.
1.799 + *
1.800 + * <p>Note that this method may not be able to return a
1.801 + * {@code float} NaN with exactly same bit pattern as the
1.802 + * {@code int} argument. IEEE 754 distinguishes between two
1.803 + * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
1.804 + * differences between the two kinds of NaN are generally not
1.805 + * visible in Java. Arithmetic operations on signaling NaNs turn
1.806 + * them into quiet NaNs with a different, but often similar, bit
1.807 + * pattern. However, on some processors merely copying a
1.808 + * signaling NaN also performs that conversion. In particular,
1.809 + * copying a signaling NaN to return it to the calling method may
1.810 + * perform this conversion. So {@code intBitsToFloat} may
1.811 + * not be able to return a {@code float} with a signaling NaN
1.812 + * bit pattern. Consequently, for some {@code int} values,
1.813 + * {@code floatToRawIntBits(intBitsToFloat(start))} may
1.814 + * <i>not</i> equal {@code start}. Moreover, which
1.815 + * particular bit patterns represent signaling NaNs is platform
1.816 + * dependent; although all NaN bit patterns, quiet or signaling,
1.817 + * must be in the NaN range identified above.
1.818 + *
1.819 + * @param bits an integer.
1.820 + * @return the {@code float} floating-point value with the same bit
1.821 + * pattern.
1.822 + */
1.823 + @JavaScriptBody(args = "bits",
1.824 + body =
1.825 + "if (bits === 0x7f800000) return Number.POSITIVE_INFINITY;\n"
1.826 + + "if (bits === 0xff800000) return Number.NEGATIVE_INFINITY;\n"
1.827 + + "if (bits >= 0x7f800001 && bits <= 0xffffffff) return Number.NaN;\n"
1.828 + + "var s = ((bits >> 31) == 0) ? 1 : -1;\n"
1.829 + + "var e = ((bits >> 23) & 0xff);\n"
1.830 + + "var m = (e == 0) ?\n"
1.831 + + " (bits & 0x7fffff) << 1 :\n"
1.832 + + " (bits & 0x7fffff) | 0x800000;\n"
1.833 + + "return s * m * Math.pow(2.0, e - 150);\n"
1.834 + )
1.835 + public static native float intBitsToFloat(int bits);
1.836 +
1.837 + /**
1.838 + * Compares two {@code Float} objects numerically. There are
1.839 + * two ways in which comparisons performed by this method differ
1.840 + * from those performed by the Java language numerical comparison
1.841 + * operators ({@code <, <=, ==, >=, >}) when
1.842 + * applied to primitive {@code float} values:
1.843 + *
1.844 + * <ul><li>
1.845 + * {@code Float.NaN} is considered by this method to
1.846 + * be equal to itself and greater than all other
1.847 + * {@code float} values
1.848 + * (including {@code Float.POSITIVE_INFINITY}).
1.849 + * <li>
1.850 + * {@code 0.0f} is considered by this method to be greater
1.851 + * than {@code -0.0f}.
1.852 + * </ul>
1.853 + *
1.854 + * This ensures that the <i>natural ordering</i> of {@code Float}
1.855 + * objects imposed by this method is <i>consistent with equals</i>.
1.856 + *
1.857 + * @param anotherFloat the {@code Float} to be compared.
1.858 + * @return the value {@code 0} if {@code anotherFloat} is
1.859 + * numerically equal to this {@code Float}; a value
1.860 + * less than {@code 0} if this {@code Float}
1.861 + * is numerically less than {@code anotherFloat};
1.862 + * and a value greater than {@code 0} if this
1.863 + * {@code Float} is numerically greater than
1.864 + * {@code anotherFloat}.
1.865 + *
1.866 + * @since 1.2
1.867 + * @see Comparable#compareTo(Object)
1.868 + */
1.869 + public int compareTo(Float anotherFloat) {
1.870 + return Float.compare(value, anotherFloat.value);
1.871 + }
1.872 +
1.873 + /**
1.874 + * Compares the two specified {@code float} values. The sign
1.875 + * of the integer value returned is the same as that of the
1.876 + * integer that would be returned by the call:
1.877 + * <pre>
1.878 + * new Float(f1).compareTo(new Float(f2))
1.879 + * </pre>
1.880 + *
1.881 + * @param f1 the first {@code float} to compare.
1.882 + * @param f2 the second {@code float} to compare.
1.883 + * @return the value {@code 0} if {@code f1} is
1.884 + * numerically equal to {@code f2}; a value less than
1.885 + * {@code 0} if {@code f1} is numerically less than
1.886 + * {@code f2}; and a value greater than {@code 0}
1.887 + * if {@code f1} is numerically greater than
1.888 + * {@code f2}.
1.889 + * @since 1.4
1.890 + */
1.891 + public static int compare(float f1, float f2) {
1.892 + if (f1 < f2)
1.893 + return -1; // Neither val is NaN, thisVal is smaller
1.894 + if (f1 > f2)
1.895 + return 1; // Neither val is NaN, thisVal is larger
1.896 +
1.897 + // Cannot use floatToRawIntBits because of possibility of NaNs.
1.898 + int thisBits = Float.floatToIntBits(f1);
1.899 + int anotherBits = Float.floatToIntBits(f2);
1.900 +
1.901 + return (thisBits == anotherBits ? 0 : // Values are equal
1.902 + (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1.903 + 1)); // (0.0, -0.0) or (NaN, !NaN)
1.904 + }
1.905 +
1.906 + /** use serialVersionUID from JDK 1.0.2 for interoperability */
1.907 + private static final long serialVersionUID = -2671257302660747028L;
1.908 +}