1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/src/main/java/java/lang/Byte.java Sat Sep 29 10:56:23 2012 +0200
1.3 @@ -0,0 +1,452 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2009, 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 +/**
1.32 + *
1.33 + * The {@code Byte} class wraps a value of primitive type {@code byte}
1.34 + * in an object. An object of type {@code Byte} contains a single
1.35 + * field whose type is {@code byte}.
1.36 + *
1.37 + * <p>In addition, this class provides several methods for converting
1.38 + * a {@code byte} to a {@code String} and a {@code String} to a {@code
1.39 + * byte}, as well as other constants and methods useful when dealing
1.40 + * with a {@code byte}.
1.41 + *
1.42 + * @author Nakul Saraiya
1.43 + * @author Joseph D. Darcy
1.44 + * @see java.lang.Number
1.45 + * @since JDK1.1
1.46 + */
1.47 +public final class Byte extends Number implements Comparable<Byte> {
1.48 +
1.49 + /**
1.50 + * A constant holding the minimum value a {@code byte} can
1.51 + * have, -2<sup>7</sup>.
1.52 + */
1.53 + public static final byte MIN_VALUE = -128;
1.54 +
1.55 + /**
1.56 + * A constant holding the maximum value a {@code byte} can
1.57 + * have, 2<sup>7</sup>-1.
1.58 + */
1.59 + public static final byte MAX_VALUE = 127;
1.60 +
1.61 + /**
1.62 + * The {@code Class} instance representing the primitive type
1.63 + * {@code byte}.
1.64 + */
1.65 + public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
1.66 +
1.67 + /**
1.68 + * Returns a new {@code String} object representing the
1.69 + * specified {@code byte}. The radix is assumed to be 10.
1.70 + *
1.71 + * @param b the {@code byte} to be converted
1.72 + * @return the string representation of the specified {@code byte}
1.73 + * @see java.lang.Integer#toString(int)
1.74 + */
1.75 + public static String toString(byte b) {
1.76 + return Integer.toString((int)b, 10);
1.77 + }
1.78 +
1.79 + private static class ByteCache {
1.80 + private ByteCache(){}
1.81 +
1.82 + static final Byte cache[] = new Byte[-(-128) + 127 + 1];
1.83 +
1.84 + static {
1.85 + for(int i = 0; i < cache.length; i++)
1.86 + cache[i] = new Byte((byte)(i - 128));
1.87 + }
1.88 + }
1.89 +
1.90 + /**
1.91 + * Returns a {@code Byte} instance representing the specified
1.92 + * {@code byte} value.
1.93 + * If a new {@code Byte} instance is not required, this method
1.94 + * should generally be used in preference to the constructor
1.95 + * {@link #Byte(byte)}, as this method is likely to yield
1.96 + * significantly better space and time performance since
1.97 + * all byte values are cached.
1.98 + *
1.99 + * @param b a byte value.
1.100 + * @return a {@code Byte} instance representing {@code b}.
1.101 + * @since 1.5
1.102 + */
1.103 + public static Byte valueOf(byte b) {
1.104 + final int offset = 128;
1.105 + return ByteCache.cache[(int)b + offset];
1.106 + }
1.107 +
1.108 + /**
1.109 + * Parses the string argument as a signed {@code byte} in the
1.110 + * radix specified by the second argument. The characters in the
1.111 + * string must all be digits, of the specified radix (as
1.112 + * determined by whether {@link java.lang.Character#digit(char,
1.113 + * int)} returns a nonnegative value) except that the first
1.114 + * character may be an ASCII minus sign {@code '-'}
1.115 + * (<code>'\u002D'</code>) to indicate a negative value or an
1.116 + * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
1.117 + * indicate a positive value. The resulting {@code byte} value is
1.118 + * returned.
1.119 + *
1.120 + * <p>An exception of type {@code NumberFormatException} is
1.121 + * thrown if any of the following situations occurs:
1.122 + * <ul>
1.123 + * <li> The first argument is {@code null} or is a string of
1.124 + * length zero.
1.125 + *
1.126 + * <li> The radix is either smaller than {@link
1.127 + * java.lang.Character#MIN_RADIX} or larger than {@link
1.128 + * java.lang.Character#MAX_RADIX}.
1.129 + *
1.130 + * <li> Any character of the string is not a digit of the
1.131 + * specified radix, except that the first character may be a minus
1.132 + * sign {@code '-'} (<code>'\u002D'</code>) or plus sign
1.133 + * {@code '+'} (<code>'\u002B'</code>) provided that the
1.134 + * string is longer than length 1.
1.135 + *
1.136 + * <li> The value represented by the string is not a value of type
1.137 + * {@code byte}.
1.138 + * </ul>
1.139 + *
1.140 + * @param s the {@code String} containing the
1.141 + * {@code byte}
1.142 + * representation to be parsed
1.143 + * @param radix the radix to be used while parsing {@code s}
1.144 + * @return the {@code byte} value represented by the string
1.145 + * argument in the specified radix
1.146 + * @throws NumberFormatException If the string does
1.147 + * not contain a parsable {@code byte}.
1.148 + */
1.149 + public static byte parseByte(String s, int radix)
1.150 + throws NumberFormatException {
1.151 + int i = Integer.parseInt(s, radix);
1.152 + if (i < MIN_VALUE || i > MAX_VALUE)
1.153 + throw new NumberFormatException(
1.154 + "Value out of range. Value:\"" + s + "\" Radix:" + radix);
1.155 + return (byte)i;
1.156 + }
1.157 +
1.158 + /**
1.159 + * Parses the string argument as a signed decimal {@code
1.160 + * byte}. The characters in the string must all be decimal digits,
1.161 + * except that the first character may be an ASCII minus sign
1.162 + * {@code '-'} (<code>'\u002D'</code>) to indicate a negative
1.163 + * value or an ASCII plus sign {@code '+'}
1.164 + * (<code>'\u002B'</code>) to indicate a positive value. The
1.165 + * resulting {@code byte} value is returned, exactly as if the
1.166 + * argument and the radix 10 were given as arguments to the {@link
1.167 + * #parseByte(java.lang.String, int)} method.
1.168 + *
1.169 + * @param s a {@code String} containing the
1.170 + * {@code byte} representation to be parsed
1.171 + * @return the {@code byte} value represented by the
1.172 + * argument in decimal
1.173 + * @throws NumberFormatException if the string does not
1.174 + * contain a parsable {@code byte}.
1.175 + */
1.176 + public static byte parseByte(String s) throws NumberFormatException {
1.177 + return parseByte(s, 10);
1.178 + }
1.179 +
1.180 + /**
1.181 + * Returns a {@code Byte} object holding the value
1.182 + * extracted from the specified {@code String} when parsed
1.183 + * with the radix given by the second argument. The first argument
1.184 + * is interpreted as representing a signed {@code byte} in
1.185 + * the radix specified by the second argument, exactly as if the
1.186 + * argument were given to the {@link #parseByte(java.lang.String,
1.187 + * int)} method. The result is a {@code Byte} object that
1.188 + * represents the {@code byte} value specified by the string.
1.189 + *
1.190 + * <p> In other words, this method returns a {@code Byte} object
1.191 + * equal to the value of:
1.192 + *
1.193 + * <blockquote>
1.194 + * {@code new Byte(Byte.parseByte(s, radix))}
1.195 + * </blockquote>
1.196 + *
1.197 + * @param s the string to be parsed
1.198 + * @param radix the radix to be used in interpreting {@code s}
1.199 + * @return a {@code Byte} object holding the value
1.200 + * represented by the string argument in the
1.201 + * specified radix.
1.202 + * @throws NumberFormatException If the {@code String} does
1.203 + * not contain a parsable {@code byte}.
1.204 + */
1.205 + public static Byte valueOf(String s, int radix)
1.206 + throws NumberFormatException {
1.207 + return valueOf(parseByte(s, radix));
1.208 + }
1.209 +
1.210 + /**
1.211 + * Returns a {@code Byte} object holding the value
1.212 + * given by the specified {@code String}. The argument is
1.213 + * interpreted as representing a signed decimal {@code byte},
1.214 + * exactly as if the argument were given to the {@link
1.215 + * #parseByte(java.lang.String)} method. The result is a
1.216 + * {@code Byte} object that represents the {@code byte}
1.217 + * value specified by the string.
1.218 + *
1.219 + * <p> In other words, this method returns a {@code Byte} object
1.220 + * equal to the value of:
1.221 + *
1.222 + * <blockquote>
1.223 + * {@code new Byte(Byte.parseByte(s))}
1.224 + * </blockquote>
1.225 + *
1.226 + * @param s the string to be parsed
1.227 + * @return a {@code Byte} object holding the value
1.228 + * represented by the string argument
1.229 + * @throws NumberFormatException If the {@code String} does
1.230 + * not contain a parsable {@code byte}.
1.231 + */
1.232 + public static Byte valueOf(String s) throws NumberFormatException {
1.233 + return valueOf(s, 10);
1.234 + }
1.235 +
1.236 + /**
1.237 + * Decodes a {@code String} into a {@code Byte}.
1.238 + * Accepts decimal, hexadecimal, and octal numbers given by
1.239 + * the following grammar:
1.240 + *
1.241 + * <blockquote>
1.242 + * <dl>
1.243 + * <dt><i>DecodableString:</i>
1.244 + * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1.245 + * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1.246 + * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1.247 + * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1.248 + * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
1.249 + * <p>
1.250 + * <dt><i>Sign:</i>
1.251 + * <dd>{@code -}
1.252 + * <dd>{@code +}
1.253 + * </dl>
1.254 + * </blockquote>
1.255 + *
1.256 + * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
1.257 + * are as defined in section 3.10.1 of
1.258 + * <cite>The Java™ Language Specification</cite>,
1.259 + * except that underscores are not accepted between digits.
1.260 + *
1.261 + * <p>The sequence of characters following an optional
1.262 + * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
1.263 + * "{@code #}", or leading zero) is parsed as by the {@code
1.264 + * Byte.parseByte} method with the indicated radix (10, 16, or 8).
1.265 + * This sequence of characters must represent a positive value or
1.266 + * a {@link NumberFormatException} will be thrown. The result is
1.267 + * negated if first character of the specified {@code String} is
1.268 + * the minus sign. No whitespace characters are permitted in the
1.269 + * {@code String}.
1.270 + *
1.271 + * @param nm the {@code String} to decode.
1.272 + * @return a {@code Byte} object holding the {@code byte}
1.273 + * value represented by {@code nm}
1.274 + * @throws NumberFormatException if the {@code String} does not
1.275 + * contain a parsable {@code byte}.
1.276 + * @see java.lang.Byte#parseByte(java.lang.String, int)
1.277 + */
1.278 + public static Byte decode(String nm) throws NumberFormatException {
1.279 + int i = Integer.decode(nm);
1.280 + if (i < MIN_VALUE || i > MAX_VALUE)
1.281 + throw new NumberFormatException(
1.282 + "Value " + i + " out of range from input " + nm);
1.283 + return valueOf((byte)i);
1.284 + }
1.285 +
1.286 + /**
1.287 + * The value of the {@code Byte}.
1.288 + *
1.289 + * @serial
1.290 + */
1.291 + private final byte value;
1.292 +
1.293 + /**
1.294 + * Constructs a newly allocated {@code Byte} object that
1.295 + * represents the specified {@code byte} value.
1.296 + *
1.297 + * @param value the value to be represented by the
1.298 + * {@code Byte}.
1.299 + */
1.300 + public Byte(byte value) {
1.301 + this.value = value;
1.302 + }
1.303 +
1.304 + /**
1.305 + * Constructs a newly allocated {@code Byte} object that
1.306 + * represents the {@code byte} value indicated by the
1.307 + * {@code String} parameter. The string is converted to a
1.308 + * {@code byte} value in exactly the manner used by the
1.309 + * {@code parseByte} method for radix 10.
1.310 + *
1.311 + * @param s the {@code String} to be converted to a
1.312 + * {@code Byte}
1.313 + * @throws NumberFormatException If the {@code String}
1.314 + * does not contain a parsable {@code byte}.
1.315 + * @see java.lang.Byte#parseByte(java.lang.String, int)
1.316 + */
1.317 + public Byte(String s) throws NumberFormatException {
1.318 + this.value = parseByte(s, 10);
1.319 + }
1.320 +
1.321 + /**
1.322 + * Returns the value of this {@code Byte} as a
1.323 + * {@code byte}.
1.324 + */
1.325 + public byte byteValue() {
1.326 + return value;
1.327 + }
1.328 +
1.329 + /**
1.330 + * Returns the value of this {@code Byte} as a
1.331 + * {@code short}.
1.332 + */
1.333 + public short shortValue() {
1.334 + return (short)value;
1.335 + }
1.336 +
1.337 + /**
1.338 + * Returns the value of this {@code Byte} as an
1.339 + * {@code int}.
1.340 + */
1.341 + public int intValue() {
1.342 + return (int)value;
1.343 + }
1.344 +
1.345 + /**
1.346 + * Returns the value of this {@code Byte} as a
1.347 + * {@code long}.
1.348 + */
1.349 + public long longValue() {
1.350 + return (long)value;
1.351 + }
1.352 +
1.353 + /**
1.354 + * Returns the value of this {@code Byte} as a
1.355 + * {@code float}.
1.356 + */
1.357 + public float floatValue() {
1.358 + return (float)value;
1.359 + }
1.360 +
1.361 + /**
1.362 + * Returns the value of this {@code Byte} as a
1.363 + * {@code double}.
1.364 + */
1.365 + public double doubleValue() {
1.366 + return (double)value;
1.367 + }
1.368 +
1.369 + /**
1.370 + * Returns a {@code String} object representing this
1.371 + * {@code Byte}'s value. The value is converted to signed
1.372 + * decimal representation and returned as a string, exactly as if
1.373 + * the {@code byte} value were given as an argument to the
1.374 + * {@link java.lang.Byte#toString(byte)} method.
1.375 + *
1.376 + * @return a string representation of the value of this object in
1.377 + * base 10.
1.378 + */
1.379 + public String toString() {
1.380 + return Integer.toString((int)value);
1.381 + }
1.382 +
1.383 + /**
1.384 + * Returns a hash code for this {@code Byte}; equal to the result
1.385 + * of invoking {@code intValue()}.
1.386 + *
1.387 + * @return a hash code value for this {@code Byte}
1.388 + */
1.389 + public int hashCode() {
1.390 + return (int)value;
1.391 + }
1.392 +
1.393 + /**
1.394 + * Compares this object to the specified object. The result is
1.395 + * {@code true} if and only if the argument is not
1.396 + * {@code null} and is a {@code Byte} object that
1.397 + * contains the same {@code byte} value as this object.
1.398 + *
1.399 + * @param obj the object to compare with
1.400 + * @return {@code true} if the objects are the same;
1.401 + * {@code false} otherwise.
1.402 + */
1.403 + public boolean equals(Object obj) {
1.404 + if (obj instanceof Byte) {
1.405 + return value == ((Byte)obj).byteValue();
1.406 + }
1.407 + return false;
1.408 + }
1.409 +
1.410 + /**
1.411 + * Compares two {@code Byte} objects numerically.
1.412 + *
1.413 + * @param anotherByte the {@code Byte} to be compared.
1.414 + * @return the value {@code 0} if this {@code Byte} is
1.415 + * equal to the argument {@code Byte}; a value less than
1.416 + * {@code 0} if this {@code Byte} is numerically less
1.417 + * than the argument {@code Byte}; and a value greater than
1.418 + * {@code 0} if this {@code Byte} is numerically
1.419 + * greater than the argument {@code Byte} (signed
1.420 + * comparison).
1.421 + * @since 1.2
1.422 + */
1.423 + public int compareTo(Byte anotherByte) {
1.424 + return compare(this.value, anotherByte.value);
1.425 + }
1.426 +
1.427 + /**
1.428 + * Compares two {@code byte} values numerically.
1.429 + * The value returned is identical to what would be returned by:
1.430 + * <pre>
1.431 + * Byte.valueOf(x).compareTo(Byte.valueOf(y))
1.432 + * </pre>
1.433 + *
1.434 + * @param x the first {@code byte} to compare
1.435 + * @param y the second {@code byte} to compare
1.436 + * @return the value {@code 0} if {@code x == y};
1.437 + * a value less than {@code 0} if {@code x < y}; and
1.438 + * a value greater than {@code 0} if {@code x > y}
1.439 + * @since 1.7
1.440 + */
1.441 + public static int compare(byte x, byte y) {
1.442 + return x - y;
1.443 + }
1.444 +
1.445 + /**
1.446 + * The number of bits used to represent a {@code byte} value in two's
1.447 + * complement binary form.
1.448 + *
1.449 + * @since 1.5
1.450 + */
1.451 + public static final int SIZE = 8;
1.452 +
1.453 + /** use serialVersionUID from JDK 1.1. for interoperability */
1.454 + private static final long serialVersionUID = -7183698231559129828L;
1.455 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/emul/src/main/java/java/lang/Double.java Sat Sep 29 10:56:23 2012 +0200
2.3 @@ -0,0 +1,988 @@
2.4 +/*
2.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2.7 + *
2.8 + * This code is free software; you can redistribute it and/or modify it
2.9 + * under the terms of the GNU General Public License version 2 only, as
2.10 + * published by the Free Software Foundation. Oracle designates this
2.11 + * particular file as subject to the "Classpath" exception as provided
2.12 + * by Oracle in the LICENSE file that accompanied this code.
2.13 + *
2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2.17 + * version 2 for more details (a copy is included in the LICENSE file that
2.18 + * accompanied this code).
2.19 + *
2.20 + * You should have received a copy of the GNU General Public License version
2.21 + * 2 along with this work; if not, write to the Free Software Foundation,
2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2.23 + *
2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2.25 + * or visit www.oracle.com if you need additional information or have any
2.26 + * questions.
2.27 + */
2.28 +
2.29 +package java.lang;
2.30 +
2.31 +import sun.misc.FloatingDecimal;
2.32 +import sun.misc.FpUtils;
2.33 +import sun.misc.DoubleConsts;
2.34 +
2.35 +/**
2.36 + * The {@code Double} class wraps a value of the primitive type
2.37 + * {@code double} in an object. An object of type
2.38 + * {@code Double} contains a single field whose type is
2.39 + * {@code double}.
2.40 + *
2.41 + * <p>In addition, this class provides several methods for converting a
2.42 + * {@code double} to a {@code String} and a
2.43 + * {@code String} to a {@code double}, as well as other
2.44 + * constants and methods useful when dealing with a
2.45 + * {@code double}.
2.46 + *
2.47 + * @author Lee Boynton
2.48 + * @author Arthur van Hoff
2.49 + * @author Joseph D. Darcy
2.50 + * @since JDK1.0
2.51 + */
2.52 +public final class Double extends Number implements Comparable<Double> {
2.53 + /**
2.54 + * A constant holding the positive infinity of type
2.55 + * {@code double}. It is equal to the value returned by
2.56 + * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
2.57 + */
2.58 + public static final double POSITIVE_INFINITY = 1.0 / 0.0;
2.59 +
2.60 + /**
2.61 + * A constant holding the negative infinity of type
2.62 + * {@code double}. It is equal to the value returned by
2.63 + * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
2.64 + */
2.65 + public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
2.66 +
2.67 + /**
2.68 + * A constant holding a Not-a-Number (NaN) value of type
2.69 + * {@code double}. It is equivalent to the value returned by
2.70 + * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
2.71 + */
2.72 + public static final double NaN = 0.0d / 0.0;
2.73 +
2.74 + /**
2.75 + * A constant holding the largest positive finite value of type
2.76 + * {@code double},
2.77 + * (2-2<sup>-52</sup>)·2<sup>1023</sup>. It is equal to
2.78 + * the hexadecimal floating-point literal
2.79 + * {@code 0x1.fffffffffffffP+1023} and also equal to
2.80 + * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
2.81 + */
2.82 + public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
2.83 +
2.84 + /**
2.85 + * A constant holding the smallest positive normal value of type
2.86 + * {@code double}, 2<sup>-1022</sup>. It is equal to the
2.87 + * hexadecimal floating-point literal {@code 0x1.0p-1022} and also
2.88 + * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
2.89 + *
2.90 + * @since 1.6
2.91 + */
2.92 + public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
2.93 +
2.94 + /**
2.95 + * A constant holding the smallest positive nonzero value of type
2.96 + * {@code double}, 2<sup>-1074</sup>. It is equal to the
2.97 + * hexadecimal floating-point literal
2.98 + * {@code 0x0.0000000000001P-1022} and also equal to
2.99 + * {@code Double.longBitsToDouble(0x1L)}.
2.100 + */
2.101 + public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
2.102 +
2.103 + /**
2.104 + * Maximum exponent a finite {@code double} variable may have.
2.105 + * It is equal to the value returned by
2.106 + * {@code Math.getExponent(Double.MAX_VALUE)}.
2.107 + *
2.108 + * @since 1.6
2.109 + */
2.110 + public static final int MAX_EXPONENT = 1023;
2.111 +
2.112 + /**
2.113 + * Minimum exponent a normalized {@code double} variable may
2.114 + * have. It is equal to the value returned by
2.115 + * {@code Math.getExponent(Double.MIN_NORMAL)}.
2.116 + *
2.117 + * @since 1.6
2.118 + */
2.119 + public static final int MIN_EXPONENT = -1022;
2.120 +
2.121 + /**
2.122 + * The number of bits used to represent a {@code double} value.
2.123 + *
2.124 + * @since 1.5
2.125 + */
2.126 + public static final int SIZE = 64;
2.127 +
2.128 + /**
2.129 + * The {@code Class} instance representing the primitive type
2.130 + * {@code double}.
2.131 + *
2.132 + * @since JDK1.1
2.133 + */
2.134 + public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");
2.135 +
2.136 + /**
2.137 + * Returns a string representation of the {@code double}
2.138 + * argument. All characters mentioned below are ASCII characters.
2.139 + * <ul>
2.140 + * <li>If the argument is NaN, the result is the string
2.141 + * "{@code NaN}".
2.142 + * <li>Otherwise, the result is a string that represents the sign and
2.143 + * magnitude (absolute value) of the argument. If the sign is negative,
2.144 + * the first character of the result is '{@code -}'
2.145 + * (<code>'\u002D'</code>); if the sign is positive, no sign character
2.146 + * appears in the result. As for the magnitude <i>m</i>:
2.147 + * <ul>
2.148 + * <li>If <i>m</i> is infinity, it is represented by the characters
2.149 + * {@code "Infinity"}; thus, positive infinity produces the result
2.150 + * {@code "Infinity"} and negative infinity produces the result
2.151 + * {@code "-Infinity"}.
2.152 + *
2.153 + * <li>If <i>m</i> is zero, it is represented by the characters
2.154 + * {@code "0.0"}; thus, negative zero produces the result
2.155 + * {@code "-0.0"} and positive zero produces the result
2.156 + * {@code "0.0"}.
2.157 + *
2.158 + * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
2.159 + * than 10<sup>7</sup>, then it is represented as the integer part of
2.160 + * <i>m</i>, in decimal form with no leading zeroes, followed by
2.161 + * '{@code .}' (<code>'\u002E'</code>), followed by one or
2.162 + * more decimal digits representing the fractional part of <i>m</i>.
2.163 + *
2.164 + * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
2.165 + * equal to 10<sup>7</sup>, then it is represented in so-called
2.166 + * "computerized scientific notation." Let <i>n</i> be the unique
2.167 + * integer such that 10<sup><i>n</i></sup> ≤ <i>m</i> {@literal <}
2.168 + * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
2.169 + * mathematically exact quotient of <i>m</i> and
2.170 + * 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10. The
2.171 + * magnitude is then represented as the integer part of <i>a</i>,
2.172 + * as a single decimal digit, followed by '{@code .}'
2.173 + * (<code>'\u002E'</code>), followed by decimal digits
2.174 + * representing the fractional part of <i>a</i>, followed by the
2.175 + * letter '{@code E}' (<code>'\u0045'</code>), followed
2.176 + * by a representation of <i>n</i> as a decimal integer, as
2.177 + * produced by the method {@link Integer#toString(int)}.
2.178 + * </ul>
2.179 + * </ul>
2.180 + * How many digits must be printed for the fractional part of
2.181 + * <i>m</i> or <i>a</i>? There must be at least one digit to represent
2.182 + * the fractional part, and beyond that as many, but only as many, more
2.183 + * digits as are needed to uniquely distinguish the argument value from
2.184 + * adjacent values of type {@code double}. That is, suppose that
2.185 + * <i>x</i> is the exact mathematical value represented by the decimal
2.186 + * representation produced by this method for a finite nonzero argument
2.187 + * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
2.188 + * to <i>x</i>; or if two {@code double} values are equally close
2.189 + * to <i>x</i>, then <i>d</i> must be one of them and the least
2.190 + * significant bit of the significand of <i>d</i> must be {@code 0}.
2.191 + *
2.192 + * <p>To create localized string representations of a floating-point
2.193 + * value, use subclasses of {@link java.text.NumberFormat}.
2.194 + *
2.195 + * @param d the {@code double} to be converted.
2.196 + * @return a string representation of the argument.
2.197 + */
2.198 + public static String toString(double d) {
2.199 + return new FloatingDecimal(d).toJavaFormatString();
2.200 + }
2.201 +
2.202 + /**
2.203 + * Returns a hexadecimal string representation of the
2.204 + * {@code double} argument. All characters mentioned below
2.205 + * are ASCII characters.
2.206 + *
2.207 + * <ul>
2.208 + * <li>If the argument is NaN, the result is the string
2.209 + * "{@code NaN}".
2.210 + * <li>Otherwise, the result is a string that represents the sign
2.211 + * and magnitude of the argument. If the sign is negative, the
2.212 + * first character of the result is '{@code -}'
2.213 + * (<code>'\u002D'</code>); if the sign is positive, no sign
2.214 + * character appears in the result. As for the magnitude <i>m</i>:
2.215 + *
2.216 + * <ul>
2.217 + * <li>If <i>m</i> is infinity, it is represented by the string
2.218 + * {@code "Infinity"}; thus, positive infinity produces the
2.219 + * result {@code "Infinity"} and negative infinity produces
2.220 + * the result {@code "-Infinity"}.
2.221 + *
2.222 + * <li>If <i>m</i> is zero, it is represented by the string
2.223 + * {@code "0x0.0p0"}; thus, negative zero produces the result
2.224 + * {@code "-0x0.0p0"} and positive zero produces the result
2.225 + * {@code "0x0.0p0"}.
2.226 + *
2.227 + * <li>If <i>m</i> is a {@code double} value with a
2.228 + * normalized representation, substrings are used to represent the
2.229 + * significand and exponent fields. The significand is
2.230 + * represented by the characters {@code "0x1."}
2.231 + * followed by a lowercase hexadecimal representation of the rest
2.232 + * of the significand as a fraction. Trailing zeros in the
2.233 + * hexadecimal representation are removed unless all the digits
2.234 + * are zero, in which case a single zero is used. Next, the
2.235 + * exponent is represented by {@code "p"} followed
2.236 + * by a decimal string of the unbiased exponent as if produced by
2.237 + * a call to {@link Integer#toString(int) Integer.toString} on the
2.238 + * exponent value.
2.239 + *
2.240 + * <li>If <i>m</i> is a {@code double} value with a subnormal
2.241 + * representation, the significand is represented by the
2.242 + * characters {@code "0x0."} followed by a
2.243 + * hexadecimal representation of the rest of the significand as a
2.244 + * fraction. Trailing zeros in the hexadecimal representation are
2.245 + * removed. Next, the exponent is represented by
2.246 + * {@code "p-1022"}. Note that there must be at
2.247 + * least one nonzero digit in a subnormal significand.
2.248 + *
2.249 + * </ul>
2.250 + *
2.251 + * </ul>
2.252 + *
2.253 + * <table border>
2.254 + * <caption><h3>Examples</h3></caption>
2.255 + * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
2.256 + * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
2.257 + * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
2.258 + * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
2.259 + * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
2.260 + * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
2.261 + * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
2.262 + * <tr><td>{@code Double.MAX_VALUE}</td>
2.263 + * <td>{@code 0x1.fffffffffffffp1023}</td>
2.264 + * <tr><td>{@code Minimum Normal Value}</td>
2.265 + * <td>{@code 0x1.0p-1022}</td>
2.266 + * <tr><td>{@code Maximum Subnormal Value}</td>
2.267 + * <td>{@code 0x0.fffffffffffffp-1022}</td>
2.268 + * <tr><td>{@code Double.MIN_VALUE}</td>
2.269 + * <td>{@code 0x0.0000000000001p-1022}</td>
2.270 + * </table>
2.271 + * @param d the {@code double} to be converted.
2.272 + * @return a hex string representation of the argument.
2.273 + * @since 1.5
2.274 + * @author Joseph D. Darcy
2.275 + */
2.276 + public static String toHexString(double d) {
2.277 + /*
2.278 + * Modeled after the "a" conversion specifier in C99, section
2.279 + * 7.19.6.1; however, the output of this method is more
2.280 + * tightly specified.
2.281 + */
2.282 + if (!FpUtils.isFinite(d) )
2.283 + // For infinity and NaN, use the decimal output.
2.284 + return Double.toString(d);
2.285 + else {
2.286 + // Initialized to maximum size of output.
2.287 + StringBuffer answer = new StringBuffer(24);
2.288 +
2.289 + if (FpUtils.rawCopySign(1.0, d) == -1.0) // value is negative,
2.290 + answer.append("-"); // so append sign info
2.291 +
2.292 + answer.append("0x");
2.293 +
2.294 + d = Math.abs(d);
2.295 +
2.296 + if(d == 0.0) {
2.297 + answer.append("0.0p0");
2.298 + }
2.299 + else {
2.300 + boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
2.301 +
2.302 + // Isolate significand bits and OR in a high-order bit
2.303 + // so that the string representation has a known
2.304 + // length.
2.305 + long signifBits = (Double.doubleToLongBits(d)
2.306 + & DoubleConsts.SIGNIF_BIT_MASK) |
2.307 + 0x1000000000000000L;
2.308 +
2.309 + // Subnormal values have a 0 implicit bit; normal
2.310 + // values have a 1 implicit bit.
2.311 + answer.append(subnormal ? "0." : "1.");
2.312 +
2.313 + // Isolate the low-order 13 digits of the hex
2.314 + // representation. If all the digits are zero,
2.315 + // replace with a single 0; otherwise, remove all
2.316 + // trailing zeros.
2.317 + String signif = Long.toHexString(signifBits).substring(3,16);
2.318 + answer.append(signif.equals("0000000000000") ? // 13 zeros
2.319 + "0":
2.320 + signif.replaceFirst("0{1,12}$", ""));
2.321 +
2.322 + // If the value is subnormal, use the E_min exponent
2.323 + // value for double; otherwise, extract and report d's
2.324 + // exponent (the representation of a subnormal uses
2.325 + // E_min -1).
2.326 + answer.append("p" + (subnormal ?
2.327 + DoubleConsts.MIN_EXPONENT:
2.328 + FpUtils.getExponent(d) ));
2.329 + }
2.330 + return answer.toString();
2.331 + }
2.332 + }
2.333 +
2.334 + /**
2.335 + * Returns a {@code Double} object holding the
2.336 + * {@code double} value represented by the argument string
2.337 + * {@code s}.
2.338 + *
2.339 + * <p>If {@code s} is {@code null}, then a
2.340 + * {@code NullPointerException} is thrown.
2.341 + *
2.342 + * <p>Leading and trailing whitespace characters in {@code s}
2.343 + * are ignored. Whitespace is removed as if by the {@link
2.344 + * String#trim} method; that is, both ASCII space and control
2.345 + * characters are removed. The rest of {@code s} should
2.346 + * constitute a <i>FloatValue</i> as described by the lexical
2.347 + * syntax rules:
2.348 + *
2.349 + * <blockquote>
2.350 + * <dl>
2.351 + * <dt><i>FloatValue:</i>
2.352 + * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
2.353 + * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
2.354 + * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
2.355 + * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
2.356 + * <dd><i>SignedInteger</i>
2.357 + * </dl>
2.358 + *
2.359 + * <p>
2.360 + *
2.361 + * <dl>
2.362 + * <dt><i>HexFloatingPointLiteral</i>:
2.363 + * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
2.364 + * </dl>
2.365 + *
2.366 + * <p>
2.367 + *
2.368 + * <dl>
2.369 + * <dt><i>HexSignificand:</i>
2.370 + * <dd><i>HexNumeral</i>
2.371 + * <dd><i>HexNumeral</i> {@code .}
2.372 + * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
2.373 + * </i>{@code .}<i> HexDigits</i>
2.374 + * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
2.375 + * </i>{@code .} <i>HexDigits</i>
2.376 + * </dl>
2.377 + *
2.378 + * <p>
2.379 + *
2.380 + * <dl>
2.381 + * <dt><i>BinaryExponent:</i>
2.382 + * <dd><i>BinaryExponentIndicator SignedInteger</i>
2.383 + * </dl>
2.384 + *
2.385 + * <p>
2.386 + *
2.387 + * <dl>
2.388 + * <dt><i>BinaryExponentIndicator:</i>
2.389 + * <dd>{@code p}
2.390 + * <dd>{@code P}
2.391 + * </dl>
2.392 + *
2.393 + * </blockquote>
2.394 + *
2.395 + * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
2.396 + * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
2.397 + * <i>FloatTypeSuffix</i> are as defined in the lexical structure
2.398 + * sections of
2.399 + * <cite>The Java™ Language Specification</cite>,
2.400 + * except that underscores are not accepted between digits.
2.401 + * If {@code s} does not have the form of
2.402 + * a <i>FloatValue</i>, then a {@code NumberFormatException}
2.403 + * is thrown. Otherwise, {@code s} is regarded as
2.404 + * representing an exact decimal value in the usual
2.405 + * "computerized scientific notation" or as an exact
2.406 + * hexadecimal value; this exact numerical value is then
2.407 + * conceptually converted to an "infinitely precise"
2.408 + * binary value that is then rounded to type {@code double}
2.409 + * by the usual round-to-nearest rule of IEEE 754 floating-point
2.410 + * arithmetic, which includes preserving the sign of a zero
2.411 + * value.
2.412 + *
2.413 + * Note that the round-to-nearest rule also implies overflow and
2.414 + * underflow behaviour; if the exact value of {@code s} is large
2.415 + * enough in magnitude (greater than or equal to ({@link
2.416 + * #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2),
2.417 + * rounding to {@code double} will result in an infinity and if the
2.418 + * exact value of {@code s} is small enough in magnitude (less
2.419 + * than or equal to {@link #MIN_VALUE}/2), rounding to float will
2.420 + * result in a zero.
2.421 + *
2.422 + * Finally, after rounding a {@code Double} object representing
2.423 + * this {@code double} value is returned.
2.424 + *
2.425 + * <p> To interpret localized string representations of a
2.426 + * floating-point value, use subclasses of {@link
2.427 + * java.text.NumberFormat}.
2.428 + *
2.429 + * <p>Note that trailing format specifiers, specifiers that
2.430 + * determine the type of a floating-point literal
2.431 + * ({@code 1.0f} is a {@code float} value;
2.432 + * {@code 1.0d} is a {@code double} value), do
2.433 + * <em>not</em> influence the results of this method. In other
2.434 + * words, the numerical value of the input string is converted
2.435 + * directly to the target floating-point type. The two-step
2.436 + * sequence of conversions, string to {@code float} followed
2.437 + * by {@code float} to {@code double}, is <em>not</em>
2.438 + * equivalent to converting a string directly to
2.439 + * {@code double}. For example, the {@code float}
2.440 + * literal {@code 0.1f} is equal to the {@code double}
2.441 + * value {@code 0.10000000149011612}; the {@code float}
2.442 + * literal {@code 0.1f} represents a different numerical
2.443 + * value than the {@code double} literal
2.444 + * {@code 0.1}. (The numerical value 0.1 cannot be exactly
2.445 + * represented in a binary floating-point number.)
2.446 + *
2.447 + * <p>To avoid calling this method on an invalid string and having
2.448 + * a {@code NumberFormatException} be thrown, the regular
2.449 + * expression below can be used to screen the input string:
2.450 + *
2.451 + * <code>
2.452 + * <pre>
2.453 + * final String Digits = "(\\p{Digit}+)";
2.454 + * final String HexDigits = "(\\p{XDigit}+)";
2.455 + * // an exponent is 'e' or 'E' followed by an optionally
2.456 + * // signed decimal integer.
2.457 + * final String Exp = "[eE][+-]?"+Digits;
2.458 + * final String fpRegex =
2.459 + * ("[\\x00-\\x20]*"+ // Optional leading "whitespace"
2.460 + * "[+-]?(" + // Optional sign character
2.461 + * "NaN|" + // "NaN" string
2.462 + * "Infinity|" + // "Infinity" string
2.463 + *
2.464 + * // A decimal floating-point string representing a finite positive
2.465 + * // number without a leading sign has at most five basic pieces:
2.466 + * // Digits . Digits ExponentPart FloatTypeSuffix
2.467 + * //
2.468 + * // Since this method allows integer-only strings as input
2.469 + * // in addition to strings of floating-point literals, the
2.470 + * // two sub-patterns below are simplifications of the grammar
2.471 + * // productions from section 3.10.2 of
2.472 + * // <cite>The Java™ Language Specification</cite>.
2.473 + *
2.474 + * // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
2.475 + * "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
2.476 + *
2.477 + * // . Digits ExponentPart_opt FloatTypeSuffix_opt
2.478 + * "(\\.("+Digits+")("+Exp+")?)|"+
2.479 + *
2.480 + * // Hexadecimal strings
2.481 + * "((" +
2.482 + * // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
2.483 + * "(0[xX]" + HexDigits + "(\\.)?)|" +
2.484 + *
2.485 + * // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
2.486 + * "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
2.487 + *
2.488 + * ")[pP][+-]?" + Digits + "))" +
2.489 + * "[fFdD]?))" +
2.490 + * "[\\x00-\\x20]*");// Optional trailing "whitespace"
2.491 + *
2.492 + * if (Pattern.matches(fpRegex, myString))
2.493 + * Double.valueOf(myString); // Will not throw NumberFormatException
2.494 + * else {
2.495 + * // Perform suitable alternative action
2.496 + * }
2.497 + * </pre>
2.498 + * </code>
2.499 + *
2.500 + * @param s the string to be parsed.
2.501 + * @return a {@code Double} object holding the value
2.502 + * represented by the {@code String} argument.
2.503 + * @throws NumberFormatException if the string does not contain a
2.504 + * parsable number.
2.505 + */
2.506 + public static Double valueOf(String s) throws NumberFormatException {
2.507 + return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
2.508 + }
2.509 +
2.510 + /**
2.511 + * Returns a {@code Double} instance representing the specified
2.512 + * {@code double} value.
2.513 + * If a new {@code Double} instance is not required, this method
2.514 + * should generally be used in preference to the constructor
2.515 + * {@link #Double(double)}, as this method is likely to yield
2.516 + * significantly better space and time performance by caching
2.517 + * frequently requested values.
2.518 + *
2.519 + * @param d a double value.
2.520 + * @return a {@code Double} instance representing {@code d}.
2.521 + * @since 1.5
2.522 + */
2.523 + public static Double valueOf(double d) {
2.524 + return new Double(d);
2.525 + }
2.526 +
2.527 + /**
2.528 + * Returns a new {@code double} initialized to the value
2.529 + * represented by the specified {@code String}, as performed
2.530 + * by the {@code valueOf} method of class
2.531 + * {@code Double}.
2.532 + *
2.533 + * @param s the string to be parsed.
2.534 + * @return the {@code double} value represented by the string
2.535 + * argument.
2.536 + * @throws NullPointerException if the string is null
2.537 + * @throws NumberFormatException if the string does not contain
2.538 + * a parsable {@code double}.
2.539 + * @see java.lang.Double#valueOf(String)
2.540 + * @since 1.2
2.541 + */
2.542 + public static double parseDouble(String s) throws NumberFormatException {
2.543 + return FloatingDecimal.readJavaFormatString(s).doubleValue();
2.544 + }
2.545 +
2.546 + /**
2.547 + * Returns {@code true} if the specified number is a
2.548 + * Not-a-Number (NaN) value, {@code false} otherwise.
2.549 + *
2.550 + * @param v the value to be tested.
2.551 + * @return {@code true} if the value of the argument is NaN;
2.552 + * {@code false} otherwise.
2.553 + */
2.554 + static public boolean isNaN(double v) {
2.555 + return (v != v);
2.556 + }
2.557 +
2.558 + /**
2.559 + * Returns {@code true} if the specified number is infinitely
2.560 + * large in magnitude, {@code false} otherwise.
2.561 + *
2.562 + * @param v the value to be tested.
2.563 + * @return {@code true} if the value of the argument is positive
2.564 + * infinity or negative infinity; {@code false} otherwise.
2.565 + */
2.566 + static public boolean isInfinite(double v) {
2.567 + return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
2.568 + }
2.569 +
2.570 + /**
2.571 + * The value of the Double.
2.572 + *
2.573 + * @serial
2.574 + */
2.575 + private final double value;
2.576 +
2.577 + /**
2.578 + * Constructs a newly allocated {@code Double} object that
2.579 + * represents the primitive {@code double} argument.
2.580 + *
2.581 + * @param value the value to be represented by the {@code Double}.
2.582 + */
2.583 + public Double(double value) {
2.584 + this.value = value;
2.585 + }
2.586 +
2.587 + /**
2.588 + * Constructs a newly allocated {@code Double} object that
2.589 + * represents the floating-point value of type {@code double}
2.590 + * represented by the string. The string is converted to a
2.591 + * {@code double} value as if by the {@code valueOf} method.
2.592 + *
2.593 + * @param s a string to be converted to a {@code Double}.
2.594 + * @throws NumberFormatException if the string does not contain a
2.595 + * parsable number.
2.596 + * @see java.lang.Double#valueOf(java.lang.String)
2.597 + */
2.598 + public Double(String s) throws NumberFormatException {
2.599 + // REMIND: this is inefficient
2.600 + this(valueOf(s).doubleValue());
2.601 + }
2.602 +
2.603 + /**
2.604 + * Returns {@code true} if this {@code Double} value is
2.605 + * a Not-a-Number (NaN), {@code false} otherwise.
2.606 + *
2.607 + * @return {@code true} if the value represented by this object is
2.608 + * NaN; {@code false} otherwise.
2.609 + */
2.610 + public boolean isNaN() {
2.611 + return isNaN(value);
2.612 + }
2.613 +
2.614 + /**
2.615 + * Returns {@code true} if this {@code Double} value is
2.616 + * infinitely large in magnitude, {@code false} otherwise.
2.617 + *
2.618 + * @return {@code true} if the value represented by this object is
2.619 + * positive infinity or negative infinity;
2.620 + * {@code false} otherwise.
2.621 + */
2.622 + public boolean isInfinite() {
2.623 + return isInfinite(value);
2.624 + }
2.625 +
2.626 + /**
2.627 + * Returns a string representation of this {@code Double} object.
2.628 + * The primitive {@code double} value represented by this
2.629 + * object is converted to a string exactly as if by the method
2.630 + * {@code toString} of one argument.
2.631 + *
2.632 + * @return a {@code String} representation of this object.
2.633 + * @see java.lang.Double#toString(double)
2.634 + */
2.635 + public String toString() {
2.636 + return toString(value);
2.637 + }
2.638 +
2.639 + /**
2.640 + * Returns the value of this {@code Double} as a {@code byte} (by
2.641 + * casting to a {@code byte}).
2.642 + *
2.643 + * @return the {@code double} value represented by this object
2.644 + * converted to type {@code byte}
2.645 + * @since JDK1.1
2.646 + */
2.647 + public byte byteValue() {
2.648 + return (byte)value;
2.649 + }
2.650 +
2.651 + /**
2.652 + * Returns the value of this {@code Double} as a
2.653 + * {@code short} (by casting to a {@code short}).
2.654 + *
2.655 + * @return the {@code double} value represented by this object
2.656 + * converted to type {@code short}
2.657 + * @since JDK1.1
2.658 + */
2.659 + public short shortValue() {
2.660 + return (short)value;
2.661 + }
2.662 +
2.663 + /**
2.664 + * Returns the value of this {@code Double} as an
2.665 + * {@code int} (by casting to type {@code int}).
2.666 + *
2.667 + * @return the {@code double} value represented by this object
2.668 + * converted to type {@code int}
2.669 + */
2.670 + public int intValue() {
2.671 + return (int)value;
2.672 + }
2.673 +
2.674 + /**
2.675 + * Returns the value of this {@code Double} as a
2.676 + * {@code long} (by casting to type {@code long}).
2.677 + *
2.678 + * @return the {@code double} value represented by this object
2.679 + * converted to type {@code long}
2.680 + */
2.681 + public long longValue() {
2.682 + return (long)value;
2.683 + }
2.684 +
2.685 + /**
2.686 + * Returns the {@code float} value of this
2.687 + * {@code Double} object.
2.688 + *
2.689 + * @return the {@code double} value represented by this object
2.690 + * converted to type {@code float}
2.691 + * @since JDK1.0
2.692 + */
2.693 + public float floatValue() {
2.694 + return (float)value;
2.695 + }
2.696 +
2.697 + /**
2.698 + * Returns the {@code double} value of this
2.699 + * {@code Double} object.
2.700 + *
2.701 + * @return the {@code double} value represented by this object
2.702 + */
2.703 + public double doubleValue() {
2.704 + return (double)value;
2.705 + }
2.706 +
2.707 + /**
2.708 + * Returns a hash code for this {@code Double} object. The
2.709 + * result is the exclusive OR of the two halves of the
2.710 + * {@code long} integer bit representation, exactly as
2.711 + * produced by the method {@link #doubleToLongBits(double)}, of
2.712 + * the primitive {@code double} value represented by this
2.713 + * {@code Double} object. That is, the hash code is the value
2.714 + * of the expression:
2.715 + *
2.716 + * <blockquote>
2.717 + * {@code (int)(v^(v>>>32))}
2.718 + * </blockquote>
2.719 + *
2.720 + * where {@code v} is defined by:
2.721 + *
2.722 + * <blockquote>
2.723 + * {@code long v = Double.doubleToLongBits(this.doubleValue());}
2.724 + * </blockquote>
2.725 + *
2.726 + * @return a {@code hash code} value for this object.
2.727 + */
2.728 + public int hashCode() {
2.729 + long bits = doubleToLongBits(value);
2.730 + return (int)(bits ^ (bits >>> 32));
2.731 + }
2.732 +
2.733 + /**
2.734 + * Compares this object against the specified object. The result
2.735 + * is {@code true} if and only if the argument is not
2.736 + * {@code null} and is a {@code Double} object that
2.737 + * represents a {@code double} that has the same value as the
2.738 + * {@code double} represented by this object. For this
2.739 + * purpose, two {@code double} values are considered to be
2.740 + * the same if and only if the method {@link
2.741 + * #doubleToLongBits(double)} returns the identical
2.742 + * {@code long} value when applied to each.
2.743 + *
2.744 + * <p>Note that in most cases, for two instances of class
2.745 + * {@code Double}, {@code d1} and {@code d2}, the
2.746 + * value of {@code d1.equals(d2)} is {@code true} if and
2.747 + * only if
2.748 + *
2.749 + * <blockquote>
2.750 + * {@code d1.doubleValue() == d2.doubleValue()}
2.751 + * </blockquote>
2.752 + *
2.753 + * <p>also has the value {@code true}. However, there are two
2.754 + * exceptions:
2.755 + * <ul>
2.756 + * <li>If {@code d1} and {@code d2} both represent
2.757 + * {@code Double.NaN}, then the {@code equals} method
2.758 + * returns {@code true}, even though
2.759 + * {@code Double.NaN==Double.NaN} has the value
2.760 + * {@code false}.
2.761 + * <li>If {@code d1} represents {@code +0.0} while
2.762 + * {@code d2} represents {@code -0.0}, or vice versa,
2.763 + * the {@code equal} test has the value {@code false},
2.764 + * even though {@code +0.0==-0.0} has the value {@code true}.
2.765 + * </ul>
2.766 + * This definition allows hash tables to operate properly.
2.767 + * @param obj the object to compare with.
2.768 + * @return {@code true} if the objects are the same;
2.769 + * {@code false} otherwise.
2.770 + * @see java.lang.Double#doubleToLongBits(double)
2.771 + */
2.772 + public boolean equals(Object obj) {
2.773 + return (obj instanceof Double)
2.774 + && (doubleToLongBits(((Double)obj).value) ==
2.775 + doubleToLongBits(value));
2.776 + }
2.777 +
2.778 + /**
2.779 + * Returns a representation of the specified floating-point value
2.780 + * according to the IEEE 754 floating-point "double
2.781 + * format" bit layout.
2.782 + *
2.783 + * <p>Bit 63 (the bit that is selected by the mask
2.784 + * {@code 0x8000000000000000L}) represents the sign of the
2.785 + * floating-point number. Bits
2.786 + * 62-52 (the bits that are selected by the mask
2.787 + * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
2.788 + * (the bits that are selected by the mask
2.789 + * {@code 0x000fffffffffffffL}) represent the significand
2.790 + * (sometimes called the mantissa) of the floating-point number.
2.791 + *
2.792 + * <p>If the argument is positive infinity, the result is
2.793 + * {@code 0x7ff0000000000000L}.
2.794 + *
2.795 + * <p>If the argument is negative infinity, the result is
2.796 + * {@code 0xfff0000000000000L}.
2.797 + *
2.798 + * <p>If the argument is NaN, the result is
2.799 + * {@code 0x7ff8000000000000L}.
2.800 + *
2.801 + * <p>In all cases, the result is a {@code long} integer that, when
2.802 + * given to the {@link #longBitsToDouble(long)} method, will produce a
2.803 + * floating-point value the same as the argument to
2.804 + * {@code doubleToLongBits} (except all NaN values are
2.805 + * collapsed to a single "canonical" NaN value).
2.806 + *
2.807 + * @param value a {@code double} precision floating-point number.
2.808 + * @return the bits that represent the floating-point number.
2.809 + */
2.810 + public static long doubleToLongBits(double value) {
2.811 + long result = doubleToRawLongBits(value);
2.812 + // Check for NaN based on values of bit fields, maximum
2.813 + // exponent and nonzero significand.
2.814 + if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
2.815 + DoubleConsts.EXP_BIT_MASK) &&
2.816 + (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
2.817 + result = 0x7ff8000000000000L;
2.818 + return result;
2.819 + }
2.820 +
2.821 + /**
2.822 + * Returns a representation of the specified floating-point value
2.823 + * according to the IEEE 754 floating-point "double
2.824 + * format" bit layout, preserving Not-a-Number (NaN) values.
2.825 + *
2.826 + * <p>Bit 63 (the bit that is selected by the mask
2.827 + * {@code 0x8000000000000000L}) represents the sign of the
2.828 + * floating-point number. Bits
2.829 + * 62-52 (the bits that are selected by the mask
2.830 + * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
2.831 + * (the bits that are selected by the mask
2.832 + * {@code 0x000fffffffffffffL}) represent the significand
2.833 + * (sometimes called the mantissa) of the floating-point number.
2.834 + *
2.835 + * <p>If the argument is positive infinity, the result is
2.836 + * {@code 0x7ff0000000000000L}.
2.837 + *
2.838 + * <p>If the argument is negative infinity, the result is
2.839 + * {@code 0xfff0000000000000L}.
2.840 + *
2.841 + * <p>If the argument is NaN, the result is the {@code long}
2.842 + * integer representing the actual NaN value. Unlike the
2.843 + * {@code doubleToLongBits} method,
2.844 + * {@code doubleToRawLongBits} does not collapse all the bit
2.845 + * patterns encoding a NaN to a single "canonical" NaN
2.846 + * value.
2.847 + *
2.848 + * <p>In all cases, the result is a {@code long} integer that,
2.849 + * when given to the {@link #longBitsToDouble(long)} method, will
2.850 + * produce a floating-point value the same as the argument to
2.851 + * {@code doubleToRawLongBits}.
2.852 + *
2.853 + * @param value a {@code double} precision floating-point number.
2.854 + * @return the bits that represent the floating-point number.
2.855 + * @since 1.3
2.856 + */
2.857 + public static native long doubleToRawLongBits(double value);
2.858 +
2.859 + /**
2.860 + * Returns the {@code double} value corresponding to a given
2.861 + * bit representation.
2.862 + * The argument is considered to be a representation of a
2.863 + * floating-point value according to the IEEE 754 floating-point
2.864 + * "double format" bit layout.
2.865 + *
2.866 + * <p>If the argument is {@code 0x7ff0000000000000L}, the result
2.867 + * is positive infinity.
2.868 + *
2.869 + * <p>If the argument is {@code 0xfff0000000000000L}, the result
2.870 + * is negative infinity.
2.871 + *
2.872 + * <p>If the argument is any value in the range
2.873 + * {@code 0x7ff0000000000001L} through
2.874 + * {@code 0x7fffffffffffffffL} or in the range
2.875 + * {@code 0xfff0000000000001L} through
2.876 + * {@code 0xffffffffffffffffL}, the result is a NaN. No IEEE
2.877 + * 754 floating-point operation provided by Java can distinguish
2.878 + * between two NaN values of the same type with different bit
2.879 + * patterns. Distinct values of NaN are only distinguishable by
2.880 + * use of the {@code Double.doubleToRawLongBits} method.
2.881 + *
2.882 + * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
2.883 + * values that can be computed from the argument:
2.884 + *
2.885 + * <blockquote><pre>
2.886 + * int s = ((bits >> 63) == 0) ? 1 : -1;
2.887 + * int e = (int)((bits >> 52) & 0x7ffL);
2.888 + * long m = (e == 0) ?
2.889 + * (bits & 0xfffffffffffffL) << 1 :
2.890 + * (bits & 0xfffffffffffffL) | 0x10000000000000L;
2.891 + * </pre></blockquote>
2.892 + *
2.893 + * Then the floating-point result equals the value of the mathematical
2.894 + * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-1075</sup>.
2.895 + *
2.896 + * <p>Note that this method may not be able to return a
2.897 + * {@code double} NaN with exactly same bit pattern as the
2.898 + * {@code long} argument. IEEE 754 distinguishes between two
2.899 + * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
2.900 + * differences between the two kinds of NaN are generally not
2.901 + * visible in Java. Arithmetic operations on signaling NaNs turn
2.902 + * them into quiet NaNs with a different, but often similar, bit
2.903 + * pattern. However, on some processors merely copying a
2.904 + * signaling NaN also performs that conversion. In particular,
2.905 + * copying a signaling NaN to return it to the calling method
2.906 + * may perform this conversion. So {@code longBitsToDouble}
2.907 + * may not be able to return a {@code double} with a
2.908 + * signaling NaN bit pattern. Consequently, for some
2.909 + * {@code long} values,
2.910 + * {@code doubleToRawLongBits(longBitsToDouble(start))} may
2.911 + * <i>not</i> equal {@code start}. Moreover, which
2.912 + * particular bit patterns represent signaling NaNs is platform
2.913 + * dependent; although all NaN bit patterns, quiet or signaling,
2.914 + * must be in the NaN range identified above.
2.915 + *
2.916 + * @param bits any {@code long} integer.
2.917 + * @return the {@code double} floating-point value with the same
2.918 + * bit pattern.
2.919 + */
2.920 + public static native double longBitsToDouble(long bits);
2.921 +
2.922 + /**
2.923 + * Compares two {@code Double} objects numerically. There
2.924 + * are two ways in which comparisons performed by this method
2.925 + * differ from those performed by the Java language numerical
2.926 + * comparison operators ({@code <, <=, ==, >=, >})
2.927 + * when applied to primitive {@code double} values:
2.928 + * <ul><li>
2.929 + * {@code Double.NaN} is considered by this method
2.930 + * to be equal to itself and greater than all other
2.931 + * {@code double} values (including
2.932 + * {@code Double.POSITIVE_INFINITY}).
2.933 + * <li>
2.934 + * {@code 0.0d} is considered by this method to be greater
2.935 + * than {@code -0.0d}.
2.936 + * </ul>
2.937 + * This ensures that the <i>natural ordering</i> of
2.938 + * {@code Double} objects imposed by this method is <i>consistent
2.939 + * with equals</i>.
2.940 + *
2.941 + * @param anotherDouble the {@code Double} to be compared.
2.942 + * @return the value {@code 0} if {@code anotherDouble} is
2.943 + * numerically equal to this {@code Double}; a value
2.944 + * less than {@code 0} if this {@code Double}
2.945 + * is numerically less than {@code anotherDouble};
2.946 + * and a value greater than {@code 0} if this
2.947 + * {@code Double} is numerically greater than
2.948 + * {@code anotherDouble}.
2.949 + *
2.950 + * @since 1.2
2.951 + */
2.952 + public int compareTo(Double anotherDouble) {
2.953 + return Double.compare(value, anotherDouble.value);
2.954 + }
2.955 +
2.956 + /**
2.957 + * Compares the two specified {@code double} values. The sign
2.958 + * of the integer value returned is the same as that of the
2.959 + * integer that would be returned by the call:
2.960 + * <pre>
2.961 + * new Double(d1).compareTo(new Double(d2))
2.962 + * </pre>
2.963 + *
2.964 + * @param d1 the first {@code double} to compare
2.965 + * @param d2 the second {@code double} to compare
2.966 + * @return the value {@code 0} if {@code d1} is
2.967 + * numerically equal to {@code d2}; a value less than
2.968 + * {@code 0} if {@code d1} is numerically less than
2.969 + * {@code d2}; and a value greater than {@code 0}
2.970 + * if {@code d1} is numerically greater than
2.971 + * {@code d2}.
2.972 + * @since 1.4
2.973 + */
2.974 + public static int compare(double d1, double d2) {
2.975 + if (d1 < d2)
2.976 + return -1; // Neither val is NaN, thisVal is smaller
2.977 + if (d1 > d2)
2.978 + return 1; // Neither val is NaN, thisVal is larger
2.979 +
2.980 + // Cannot use doubleToRawLongBits because of possibility of NaNs.
2.981 + long thisBits = Double.doubleToLongBits(d1);
2.982 + long anotherBits = Double.doubleToLongBits(d2);
2.983 +
2.984 + return (thisBits == anotherBits ? 0 : // Values are equal
2.985 + (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
2.986 + 1)); // (0.0, -0.0) or (NaN, !NaN)
2.987 + }
2.988 +
2.989 + /** use serialVersionUID from JDK 1.0.2 for interoperability */
2.990 + private static final long serialVersionUID = -9172774392245257468L;
2.991 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/emul/src/main/java/java/lang/Float.java Sat Sep 29 10:56:23 2012 +0200
3.3 @@ -0,0 +1,892 @@
3.4 +/*
3.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3.7 + *
3.8 + * This code is free software; you can redistribute it and/or modify it
3.9 + * under the terms of the GNU General Public License version 2 only, as
3.10 + * published by the Free Software Foundation. Oracle designates this
3.11 + * particular file as subject to the "Classpath" exception as provided
3.12 + * by Oracle in the LICENSE file that accompanied this code.
3.13 + *
3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3.17 + * version 2 for more details (a copy is included in the LICENSE file that
3.18 + * accompanied this code).
3.19 + *
3.20 + * You should have received a copy of the GNU General Public License version
3.21 + * 2 along with this work; if not, write to the Free Software Foundation,
3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3.23 + *
3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3.25 + * or visit www.oracle.com if you need additional information or have any
3.26 + * questions.
3.27 + */
3.28 +
3.29 +package java.lang;
3.30 +
3.31 +import sun.misc.FloatingDecimal;
3.32 +import sun.misc.FpUtils;
3.33 +import sun.misc.FloatConsts;
3.34 +import sun.misc.DoubleConsts;
3.35 +
3.36 +/**
3.37 + * The {@code Float} class wraps a value of primitive type
3.38 + * {@code float} in an object. An object of type
3.39 + * {@code Float} contains a single field whose type is
3.40 + * {@code float}.
3.41 + *
3.42 + * <p>In addition, this class provides several methods for converting a
3.43 + * {@code float} to a {@code String} and a
3.44 + * {@code String} to a {@code float}, as well as other
3.45 + * constants and methods useful when dealing with a
3.46 + * {@code float}.
3.47 + *
3.48 + * @author Lee Boynton
3.49 + * @author Arthur van Hoff
3.50 + * @author Joseph D. Darcy
3.51 + * @since JDK1.0
3.52 + */
3.53 +public final class Float extends Number implements Comparable<Float> {
3.54 + /**
3.55 + * A constant holding the positive infinity of type
3.56 + * {@code float}. It is equal to the value returned by
3.57 + * {@code Float.intBitsToFloat(0x7f800000)}.
3.58 + */
3.59 + public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
3.60 +
3.61 + /**
3.62 + * A constant holding the negative infinity of type
3.63 + * {@code float}. It is equal to the value returned by
3.64 + * {@code Float.intBitsToFloat(0xff800000)}.
3.65 + */
3.66 + public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
3.67 +
3.68 + /**
3.69 + * A constant holding a Not-a-Number (NaN) value of type
3.70 + * {@code float}. It is equivalent to the value returned by
3.71 + * {@code Float.intBitsToFloat(0x7fc00000)}.
3.72 + */
3.73 + public static final float NaN = 0.0f / 0.0f;
3.74 +
3.75 + /**
3.76 + * A constant holding the largest positive finite value of type
3.77 + * {@code float}, (2-2<sup>-23</sup>)·2<sup>127</sup>.
3.78 + * It is equal to the hexadecimal floating-point literal
3.79 + * {@code 0x1.fffffeP+127f} and also equal to
3.80 + * {@code Float.intBitsToFloat(0x7f7fffff)}.
3.81 + */
3.82 + public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f
3.83 +
3.84 + /**
3.85 + * A constant holding the smallest positive normal value of type
3.86 + * {@code float}, 2<sup>-126</sup>. It is equal to the
3.87 + * hexadecimal floating-point literal {@code 0x1.0p-126f} and also
3.88 + * equal to {@code Float.intBitsToFloat(0x00800000)}.
3.89 + *
3.90 + * @since 1.6
3.91 + */
3.92 + public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f
3.93 +
3.94 + /**
3.95 + * A constant holding the smallest positive nonzero value of type
3.96 + * {@code float}, 2<sup>-149</sup>. It is equal to the
3.97 + * hexadecimal floating-point literal {@code 0x0.000002P-126f}
3.98 + * and also equal to {@code Float.intBitsToFloat(0x1)}.
3.99 + */
3.100 + public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f
3.101 +
3.102 + /**
3.103 + * Maximum exponent a finite {@code float} variable may have. It
3.104 + * is equal to the value returned by {@code
3.105 + * Math.getExponent(Float.MAX_VALUE)}.
3.106 + *
3.107 + * @since 1.6
3.108 + */
3.109 + public static final int MAX_EXPONENT = 127;
3.110 +
3.111 + /**
3.112 + * Minimum exponent a normalized {@code float} variable may have.
3.113 + * It is equal to the value returned by {@code
3.114 + * Math.getExponent(Float.MIN_NORMAL)}.
3.115 + *
3.116 + * @since 1.6
3.117 + */
3.118 + public static final int MIN_EXPONENT = -126;
3.119 +
3.120 + /**
3.121 + * The number of bits used to represent a {@code float} value.
3.122 + *
3.123 + * @since 1.5
3.124 + */
3.125 + public static final int SIZE = 32;
3.126 +
3.127 + /**
3.128 + * The {@code Class} instance representing the primitive type
3.129 + * {@code float}.
3.130 + *
3.131 + * @since JDK1.1
3.132 + */
3.133 + public static final Class<Float> TYPE = Class.getPrimitiveClass("float");
3.134 +
3.135 + /**
3.136 + * Returns a string representation of the {@code float}
3.137 + * argument. All characters mentioned below are ASCII characters.
3.138 + * <ul>
3.139 + * <li>If the argument is NaN, the result is the string
3.140 + * "{@code NaN}".
3.141 + * <li>Otherwise, the result is a string that represents the sign and
3.142 + * magnitude (absolute value) of the argument. If the sign is
3.143 + * negative, the first character of the result is
3.144 + * '{@code -}' (<code>'\u002D'</code>); if the sign is
3.145 + * positive, no sign character appears in the result. As for
3.146 + * the magnitude <i>m</i>:
3.147 + * <ul>
3.148 + * <li>If <i>m</i> is infinity, it is represented by the characters
3.149 + * {@code "Infinity"}; thus, positive infinity produces
3.150 + * the result {@code "Infinity"} and negative infinity
3.151 + * produces the result {@code "-Infinity"}.
3.152 + * <li>If <i>m</i> is zero, it is represented by the characters
3.153 + * {@code "0.0"}; thus, negative zero produces the result
3.154 + * {@code "-0.0"} and positive zero produces the result
3.155 + * {@code "0.0"}.
3.156 + * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but
3.157 + * less than 10<sup>7</sup>, then it is represented as the
3.158 + * integer part of <i>m</i>, in decimal form with no leading
3.159 + * zeroes, followed by '{@code .}'
3.160 + * (<code>'\u002E'</code>), followed by one or more
3.161 + * decimal digits representing the fractional part of
3.162 + * <i>m</i>.
3.163 + * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or
3.164 + * equal to 10<sup>7</sup>, then it is represented in
3.165 + * so-called "computerized scientific notation." Let <i>n</i>
3.166 + * be the unique integer such that 10<sup><i>n</i> </sup>≤
3.167 + * <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>
3.168 + * be the mathematically exact quotient of <i>m</i> and
3.169 + * 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10.
3.170 + * The magnitude is then represented as the integer part of
3.171 + * <i>a</i>, as a single decimal digit, followed by
3.172 + * '{@code .}' (<code>'\u002E'</code>), followed by
3.173 + * decimal digits representing the fractional part of
3.174 + * <i>a</i>, followed by the letter '{@code E}'
3.175 + * (<code>'\u0045'</code>), followed by a representation
3.176 + * of <i>n</i> as a decimal integer, as produced by the
3.177 + * method {@link java.lang.Integer#toString(int)}.
3.178 + *
3.179 + * </ul>
3.180 + * </ul>
3.181 + * How many digits must be printed for the fractional part of
3.182 + * <i>m</i> or <i>a</i>? There must be at least one digit
3.183 + * to represent the fractional part, and beyond that as many, but
3.184 + * only as many, more digits as are needed to uniquely distinguish
3.185 + * the argument value from adjacent values of type
3.186 + * {@code float}. That is, suppose that <i>x</i> is the
3.187 + * exact mathematical value represented by the decimal
3.188 + * representation produced by this method for a finite nonzero
3.189 + * argument <i>f</i>. Then <i>f</i> must be the {@code float}
3.190 + * value nearest to <i>x</i>; or, if two {@code float} values are
3.191 + * equally close to <i>x</i>, then <i>f</i> must be one of
3.192 + * them and the least significant bit of the significand of
3.193 + * <i>f</i> must be {@code 0}.
3.194 + *
3.195 + * <p>To create localized string representations of a floating-point
3.196 + * value, use subclasses of {@link java.text.NumberFormat}.
3.197 + *
3.198 + * @param f the float to be converted.
3.199 + * @return a string representation of the argument.
3.200 + */
3.201 + public static String toString(float f) {
3.202 + return new FloatingDecimal(f).toJavaFormatString();
3.203 + }
3.204 +
3.205 + /**
3.206 + * Returns a hexadecimal string representation of the
3.207 + * {@code float} argument. All characters mentioned below are
3.208 + * ASCII characters.
3.209 + *
3.210 + * <ul>
3.211 + * <li>If the argument is NaN, the result is the string
3.212 + * "{@code NaN}".
3.213 + * <li>Otherwise, the result is a string that represents the sign and
3.214 + * magnitude (absolute value) of the argument. If the sign is negative,
3.215 + * the first character of the result is '{@code -}'
3.216 + * (<code>'\u002D'</code>); if the sign is positive, no sign character
3.217 + * appears in the result. As for the magnitude <i>m</i>:
3.218 + *
3.219 + * <ul>
3.220 + * <li>If <i>m</i> is infinity, it is represented by the string
3.221 + * {@code "Infinity"}; thus, positive infinity produces the
3.222 + * result {@code "Infinity"} and negative infinity produces
3.223 + * the result {@code "-Infinity"}.
3.224 + *
3.225 + * <li>If <i>m</i> is zero, it is represented by the string
3.226 + * {@code "0x0.0p0"}; thus, negative zero produces the result
3.227 + * {@code "-0x0.0p0"} and positive zero produces the result
3.228 + * {@code "0x0.0p0"}.
3.229 + *
3.230 + * <li>If <i>m</i> is a {@code float} value with a
3.231 + * normalized representation, substrings are used to represent the
3.232 + * significand and exponent fields. The significand is
3.233 + * represented by the characters {@code "0x1."}
3.234 + * followed by a lowercase hexadecimal representation of the rest
3.235 + * of the significand as a fraction. Trailing zeros in the
3.236 + * hexadecimal representation are removed unless all the digits
3.237 + * are zero, in which case a single zero is used. Next, the
3.238 + * exponent is represented by {@code "p"} followed
3.239 + * by a decimal string of the unbiased exponent as if produced by
3.240 + * a call to {@link Integer#toString(int) Integer.toString} on the
3.241 + * exponent value.
3.242 + *
3.243 + * <li>If <i>m</i> is a {@code float} value with a subnormal
3.244 + * representation, the significand is represented by the
3.245 + * characters {@code "0x0."} followed by a
3.246 + * hexadecimal representation of the rest of the significand as a
3.247 + * fraction. Trailing zeros in the hexadecimal representation are
3.248 + * removed. Next, the exponent is represented by
3.249 + * {@code "p-126"}. Note that there must be at
3.250 + * least one nonzero digit in a subnormal significand.
3.251 + *
3.252 + * </ul>
3.253 + *
3.254 + * </ul>
3.255 + *
3.256 + * <table border>
3.257 + * <caption><h3>Examples</h3></caption>
3.258 + * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
3.259 + * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
3.260 + * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
3.261 + * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
3.262 + * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
3.263 + * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
3.264 + * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
3.265 + * <tr><td>{@code Float.MAX_VALUE}</td>
3.266 + * <td>{@code 0x1.fffffep127}</td>
3.267 + * <tr><td>{@code Minimum Normal Value}</td>
3.268 + * <td>{@code 0x1.0p-126}</td>
3.269 + * <tr><td>{@code Maximum Subnormal Value}</td>
3.270 + * <td>{@code 0x0.fffffep-126}</td>
3.271 + * <tr><td>{@code Float.MIN_VALUE}</td>
3.272 + * <td>{@code 0x0.000002p-126}</td>
3.273 + * </table>
3.274 + * @param f the {@code float} to be converted.
3.275 + * @return a hex string representation of the argument.
3.276 + * @since 1.5
3.277 + * @author Joseph D. Darcy
3.278 + */
3.279 + public static String toHexString(float f) {
3.280 + if (Math.abs(f) < FloatConsts.MIN_NORMAL
3.281 + && f != 0.0f ) {// float subnormal
3.282 + // Adjust exponent to create subnormal double, then
3.283 + // replace subnormal double exponent with subnormal float
3.284 + // exponent
3.285 + String s = Double.toHexString(FpUtils.scalb((double)f,
3.286 + /* -1022+126 */
3.287 + DoubleConsts.MIN_EXPONENT-
3.288 + FloatConsts.MIN_EXPONENT));
3.289 + return s.replaceFirst("p-1022$", "p-126");
3.290 + }
3.291 + else // double string will be the same as float string
3.292 + return Double.toHexString(f);
3.293 + }
3.294 +
3.295 + /**
3.296 + * Returns a {@code Float} object holding the
3.297 + * {@code float} value represented by the argument string
3.298 + * {@code s}.
3.299 + *
3.300 + * <p>If {@code s} is {@code null}, then a
3.301 + * {@code NullPointerException} is thrown.
3.302 + *
3.303 + * <p>Leading and trailing whitespace characters in {@code s}
3.304 + * are ignored. Whitespace is removed as if by the {@link
3.305 + * String#trim} method; that is, both ASCII space and control
3.306 + * characters are removed. The rest of {@code s} should
3.307 + * constitute a <i>FloatValue</i> as described by the lexical
3.308 + * syntax rules:
3.309 + *
3.310 + * <blockquote>
3.311 + * <dl>
3.312 + * <dt><i>FloatValue:</i>
3.313 + * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
3.314 + * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
3.315 + * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
3.316 + * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
3.317 + * <dd><i>SignedInteger</i>
3.318 + * </dl>
3.319 + *
3.320 + * <p>
3.321 + *
3.322 + * <dl>
3.323 + * <dt><i>HexFloatingPointLiteral</i>:
3.324 + * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
3.325 + * </dl>
3.326 + *
3.327 + * <p>
3.328 + *
3.329 + * <dl>
3.330 + * <dt><i>HexSignificand:</i>
3.331 + * <dd><i>HexNumeral</i>
3.332 + * <dd><i>HexNumeral</i> {@code .}
3.333 + * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
3.334 + * </i>{@code .}<i> HexDigits</i>
3.335 + * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
3.336 + * </i>{@code .} <i>HexDigits</i>
3.337 + * </dl>
3.338 + *
3.339 + * <p>
3.340 + *
3.341 + * <dl>
3.342 + * <dt><i>BinaryExponent:</i>
3.343 + * <dd><i>BinaryExponentIndicator SignedInteger</i>
3.344 + * </dl>
3.345 + *
3.346 + * <p>
3.347 + *
3.348 + * <dl>
3.349 + * <dt><i>BinaryExponentIndicator:</i>
3.350 + * <dd>{@code p}
3.351 + * <dd>{@code P}
3.352 + * </dl>
3.353 + *
3.354 + * </blockquote>
3.355 + *
3.356 + * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
3.357 + * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
3.358 + * <i>FloatTypeSuffix</i> are as defined in the lexical structure
3.359 + * sections of
3.360 + * <cite>The Java™ Language Specification</cite>,
3.361 + * except that underscores are not accepted between digits.
3.362 + * If {@code s} does not have the form of
3.363 + * a <i>FloatValue</i>, then a {@code NumberFormatException}
3.364 + * is thrown. Otherwise, {@code s} is regarded as
3.365 + * representing an exact decimal value in the usual
3.366 + * "computerized scientific notation" or as an exact
3.367 + * hexadecimal value; this exact numerical value is then
3.368 + * conceptually converted to an "infinitely precise"
3.369 + * binary value that is then rounded to type {@code float}
3.370 + * by the usual round-to-nearest rule of IEEE 754 floating-point
3.371 + * arithmetic, which includes preserving the sign of a zero
3.372 + * value.
3.373 + *
3.374 + * Note that the round-to-nearest rule also implies overflow and
3.375 + * underflow behaviour; if the exact value of {@code s} is large
3.376 + * enough in magnitude (greater than or equal to ({@link
3.377 + * #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2),
3.378 + * rounding to {@code float} will result in an infinity and if the
3.379 + * exact value of {@code s} is small enough in magnitude (less
3.380 + * than or equal to {@link #MIN_VALUE}/2), rounding to float will
3.381 + * result in a zero.
3.382 + *
3.383 + * Finally, after rounding a {@code Float} object representing
3.384 + * this {@code float} value is returned.
3.385 + *
3.386 + * <p>To interpret localized string representations of a
3.387 + * floating-point value, use subclasses of {@link
3.388 + * java.text.NumberFormat}.
3.389 + *
3.390 + * <p>Note that trailing format specifiers, specifiers that
3.391 + * determine the type of a floating-point literal
3.392 + * ({@code 1.0f} is a {@code float} value;
3.393 + * {@code 1.0d} is a {@code double} value), do
3.394 + * <em>not</em> influence the results of this method. In other
3.395 + * words, the numerical value of the input string is converted
3.396 + * directly to the target floating-point type. In general, the
3.397 + * two-step sequence of conversions, string to {@code double}
3.398 + * followed by {@code double} to {@code float}, is
3.399 + * <em>not</em> equivalent to converting a string directly to
3.400 + * {@code float}. For example, if first converted to an
3.401 + * intermediate {@code double} and then to
3.402 + * {@code float}, the string<br>
3.403 + * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>
3.404 + * results in the {@code float} value
3.405 + * {@code 1.0000002f}; if the string is converted directly to
3.406 + * {@code float}, <code>1.000000<b>1</b>f</code> results.
3.407 + *
3.408 + * <p>To avoid calling this method on an invalid string and having
3.409 + * a {@code NumberFormatException} be thrown, the documentation
3.410 + * for {@link Double#valueOf Double.valueOf} lists a regular
3.411 + * expression which can be used to screen the input.
3.412 + *
3.413 + * @param s the string to be parsed.
3.414 + * @return a {@code Float} object holding the value
3.415 + * represented by the {@code String} argument.
3.416 + * @throws NumberFormatException if the string does not contain a
3.417 + * parsable number.
3.418 + */
3.419 + public static Float valueOf(String s) throws NumberFormatException {
3.420 + return new Float(FloatingDecimal.readJavaFormatString(s).floatValue());
3.421 + }
3.422 +
3.423 + /**
3.424 + * Returns a {@code Float} instance representing the specified
3.425 + * {@code float} value.
3.426 + * If a new {@code Float} instance is not required, this method
3.427 + * should generally be used in preference to the constructor
3.428 + * {@link #Float(float)}, as this method is likely to yield
3.429 + * significantly better space and time performance by caching
3.430 + * frequently requested values.
3.431 + *
3.432 + * @param f a float value.
3.433 + * @return a {@code Float} instance representing {@code f}.
3.434 + * @since 1.5
3.435 + */
3.436 + public static Float valueOf(float f) {
3.437 + return new Float(f);
3.438 + }
3.439 +
3.440 + /**
3.441 + * Returns a new {@code float} initialized to the value
3.442 + * represented by the specified {@code String}, as performed
3.443 + * by the {@code valueOf} method of class {@code Float}.
3.444 + *
3.445 + * @param s the string to be parsed.
3.446 + * @return the {@code float} value represented by the string
3.447 + * argument.
3.448 + * @throws NullPointerException if the string is null
3.449 + * @throws NumberFormatException if the string does not contain a
3.450 + * parsable {@code float}.
3.451 + * @see java.lang.Float#valueOf(String)
3.452 + * @since 1.2
3.453 + */
3.454 + public static float parseFloat(String s) throws NumberFormatException {
3.455 + return FloatingDecimal.readJavaFormatString(s).floatValue();
3.456 + }
3.457 +
3.458 + /**
3.459 + * Returns {@code true} if the specified number is a
3.460 + * Not-a-Number (NaN) value, {@code false} otherwise.
3.461 + *
3.462 + * @param v the value to be tested.
3.463 + * @return {@code true} if the argument is NaN;
3.464 + * {@code false} otherwise.
3.465 + */
3.466 + static public boolean isNaN(float v) {
3.467 + return (v != v);
3.468 + }
3.469 +
3.470 + /**
3.471 + * Returns {@code true} if the specified number is infinitely
3.472 + * large in magnitude, {@code false} otherwise.
3.473 + *
3.474 + * @param v the value to be tested.
3.475 + * @return {@code true} if the argument is positive infinity or
3.476 + * negative infinity; {@code false} otherwise.
3.477 + */
3.478 + static public boolean isInfinite(float v) {
3.479 + return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
3.480 + }
3.481 +
3.482 + /**
3.483 + * The value of the Float.
3.484 + *
3.485 + * @serial
3.486 + */
3.487 + private final float value;
3.488 +
3.489 + /**
3.490 + * Constructs a newly allocated {@code Float} object that
3.491 + * represents the primitive {@code float} argument.
3.492 + *
3.493 + * @param value the value to be represented by the {@code Float}.
3.494 + */
3.495 + public Float(float value) {
3.496 + this.value = value;
3.497 + }
3.498 +
3.499 + /**
3.500 + * Constructs a newly allocated {@code Float} object that
3.501 + * represents the argument converted to type {@code float}.
3.502 + *
3.503 + * @param value the value to be represented by the {@code Float}.
3.504 + */
3.505 + public Float(double value) {
3.506 + this.value = (float)value;
3.507 + }
3.508 +
3.509 + /**
3.510 + * Constructs a newly allocated {@code Float} object that
3.511 + * represents the floating-point value of type {@code float}
3.512 + * represented by the string. The string is converted to a
3.513 + * {@code float} value as if by the {@code valueOf} method.
3.514 + *
3.515 + * @param s a string to be converted to a {@code Float}.
3.516 + * @throws NumberFormatException if the string does not contain a
3.517 + * parsable number.
3.518 + * @see java.lang.Float#valueOf(java.lang.String)
3.519 + */
3.520 + public Float(String s) throws NumberFormatException {
3.521 + // REMIND: this is inefficient
3.522 + this(valueOf(s).floatValue());
3.523 + }
3.524 +
3.525 + /**
3.526 + * Returns {@code true} if this {@code Float} value is a
3.527 + * Not-a-Number (NaN), {@code false} otherwise.
3.528 + *
3.529 + * @return {@code true} if the value represented by this object is
3.530 + * NaN; {@code false} otherwise.
3.531 + */
3.532 + public boolean isNaN() {
3.533 + return isNaN(value);
3.534 + }
3.535 +
3.536 + /**
3.537 + * Returns {@code true} if this {@code Float} value is
3.538 + * infinitely large in magnitude, {@code false} otherwise.
3.539 + *
3.540 + * @return {@code true} if the value represented by this object is
3.541 + * positive infinity or negative infinity;
3.542 + * {@code false} otherwise.
3.543 + */
3.544 + public boolean isInfinite() {
3.545 + return isInfinite(value);
3.546 + }
3.547 +
3.548 + /**
3.549 + * Returns a string representation of this {@code Float} object.
3.550 + * The primitive {@code float} value represented by this object
3.551 + * is converted to a {@code String} exactly as if by the method
3.552 + * {@code toString} of one argument.
3.553 + *
3.554 + * @return a {@code String} representation of this object.
3.555 + * @see java.lang.Float#toString(float)
3.556 + */
3.557 + public String toString() {
3.558 + return Float.toString(value);
3.559 + }
3.560 +
3.561 + /**
3.562 + * Returns the value of this {@code Float} as a {@code byte} (by
3.563 + * casting to a {@code byte}).
3.564 + *
3.565 + * @return the {@code float} value represented by this object
3.566 + * converted to type {@code byte}
3.567 + */
3.568 + public byte byteValue() {
3.569 + return (byte)value;
3.570 + }
3.571 +
3.572 + /**
3.573 + * Returns the value of this {@code Float} as a {@code short} (by
3.574 + * casting to a {@code short}).
3.575 + *
3.576 + * @return the {@code float} value represented by this object
3.577 + * converted to type {@code short}
3.578 + * @since JDK1.1
3.579 + */
3.580 + public short shortValue() {
3.581 + return (short)value;
3.582 + }
3.583 +
3.584 + /**
3.585 + * Returns the value of this {@code Float} as an {@code int} (by
3.586 + * casting to type {@code int}).
3.587 + *
3.588 + * @return the {@code float} value represented by this object
3.589 + * converted to type {@code int}
3.590 + */
3.591 + public int intValue() {
3.592 + return (int)value;
3.593 + }
3.594 +
3.595 + /**
3.596 + * Returns value of this {@code Float} as a {@code long} (by
3.597 + * casting to type {@code long}).
3.598 + *
3.599 + * @return the {@code float} value represented by this object
3.600 + * converted to type {@code long}
3.601 + */
3.602 + public long longValue() {
3.603 + return (long)value;
3.604 + }
3.605 +
3.606 + /**
3.607 + * Returns the {@code float} value of this {@code Float} object.
3.608 + *
3.609 + * @return the {@code float} value represented by this object
3.610 + */
3.611 + public float floatValue() {
3.612 + return value;
3.613 + }
3.614 +
3.615 + /**
3.616 + * Returns the {@code double} value of this {@code Float} object.
3.617 + *
3.618 + * @return the {@code float} value represented by this
3.619 + * object is converted to type {@code double} and the
3.620 + * result of the conversion is returned.
3.621 + */
3.622 + public double doubleValue() {
3.623 + return (double)value;
3.624 + }
3.625 +
3.626 + /**
3.627 + * Returns a hash code for this {@code Float} object. The
3.628 + * result is the integer bit representation, exactly as produced
3.629 + * by the method {@link #floatToIntBits(float)}, of the primitive
3.630 + * {@code float} value represented by this {@code Float}
3.631 + * object.
3.632 + *
3.633 + * @return a hash code value for this object.
3.634 + */
3.635 + public int hashCode() {
3.636 + return floatToIntBits(value);
3.637 + }
3.638 +
3.639 + /**
3.640 +
3.641 + * Compares this object against the specified object. The result
3.642 + * is {@code true} if and only if the argument is not
3.643 + * {@code null} and is a {@code Float} object that
3.644 + * represents a {@code float} with the same value as the
3.645 + * {@code float} represented by this object. For this
3.646 + * purpose, two {@code float} values are considered to be the
3.647 + * same if and only if the method {@link #floatToIntBits(float)}
3.648 + * returns the identical {@code int} value when applied to
3.649 + * each.
3.650 + *
3.651 + * <p>Note that in most cases, for two instances of class
3.652 + * {@code Float}, {@code f1} and {@code f2}, the value
3.653 + * of {@code f1.equals(f2)} is {@code true} if and only if
3.654 + *
3.655 + * <blockquote><pre>
3.656 + * f1.floatValue() == f2.floatValue()
3.657 + * </pre></blockquote>
3.658 + *
3.659 + * <p>also has the value {@code true}. However, there are two exceptions:
3.660 + * <ul>
3.661 + * <li>If {@code f1} and {@code f2} both represent
3.662 + * {@code Float.NaN}, then the {@code equals} method returns
3.663 + * {@code true}, even though {@code Float.NaN==Float.NaN}
3.664 + * has the value {@code false}.
3.665 + * <li>If {@code f1} represents {@code +0.0f} while
3.666 + * {@code f2} represents {@code -0.0f}, or vice
3.667 + * versa, the {@code equal} test has the value
3.668 + * {@code false}, even though {@code 0.0f==-0.0f}
3.669 + * has the value {@code true}.
3.670 + * </ul>
3.671 + *
3.672 + * This definition allows hash tables to operate properly.
3.673 + *
3.674 + * @param obj the object to be compared
3.675 + * @return {@code true} if the objects are the same;
3.676 + * {@code false} otherwise.
3.677 + * @see java.lang.Float#floatToIntBits(float)
3.678 + */
3.679 + public boolean equals(Object obj) {
3.680 + return (obj instanceof Float)
3.681 + && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
3.682 + }
3.683 +
3.684 + /**
3.685 + * Returns a representation of the specified floating-point value
3.686 + * according to the IEEE 754 floating-point "single format" bit
3.687 + * layout.
3.688 + *
3.689 + * <p>Bit 31 (the bit that is selected by the mask
3.690 + * {@code 0x80000000}) represents the sign of the floating-point
3.691 + * number.
3.692 + * Bits 30-23 (the bits that are selected by the mask
3.693 + * {@code 0x7f800000}) represent the exponent.
3.694 + * Bits 22-0 (the bits that are selected by the mask
3.695 + * {@code 0x007fffff}) represent the significand (sometimes called
3.696 + * the mantissa) of the floating-point number.
3.697 + *
3.698 + * <p>If the argument is positive infinity, the result is
3.699 + * {@code 0x7f800000}.
3.700 + *
3.701 + * <p>If the argument is negative infinity, the result is
3.702 + * {@code 0xff800000}.
3.703 + *
3.704 + * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
3.705 + *
3.706 + * <p>In all cases, the result is an integer that, when given to the
3.707 + * {@link #intBitsToFloat(int)} method, will produce a floating-point
3.708 + * value the same as the argument to {@code floatToIntBits}
3.709 + * (except all NaN values are collapsed to a single
3.710 + * "canonical" NaN value).
3.711 + *
3.712 + * @param value a floating-point number.
3.713 + * @return the bits that represent the floating-point number.
3.714 + */
3.715 + public static int floatToIntBits(float value) {
3.716 + int result = floatToRawIntBits(value);
3.717 + // Check for NaN based on values of bit fields, maximum
3.718 + // exponent and nonzero significand.
3.719 + if ( ((result & FloatConsts.EXP_BIT_MASK) ==
3.720 + FloatConsts.EXP_BIT_MASK) &&
3.721 + (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
3.722 + result = 0x7fc00000;
3.723 + return result;
3.724 + }
3.725 +
3.726 + /**
3.727 + * Returns a representation of the specified floating-point value
3.728 + * according to the IEEE 754 floating-point "single format" bit
3.729 + * layout, preserving Not-a-Number (NaN) values.
3.730 + *
3.731 + * <p>Bit 31 (the bit that is selected by the mask
3.732 + * {@code 0x80000000}) represents the sign of the floating-point
3.733 + * number.
3.734 + * Bits 30-23 (the bits that are selected by the mask
3.735 + * {@code 0x7f800000}) represent the exponent.
3.736 + * Bits 22-0 (the bits that are selected by the mask
3.737 + * {@code 0x007fffff}) represent the significand (sometimes called
3.738 + * the mantissa) of the floating-point number.
3.739 + *
3.740 + * <p>If the argument is positive infinity, the result is
3.741 + * {@code 0x7f800000}.
3.742 + *
3.743 + * <p>If the argument is negative infinity, the result is
3.744 + * {@code 0xff800000}.
3.745 + *
3.746 + * <p>If the argument is NaN, the result is the integer representing
3.747 + * the actual NaN value. Unlike the {@code floatToIntBits}
3.748 + * method, {@code floatToRawIntBits} does not collapse all the
3.749 + * bit patterns encoding a NaN to a single "canonical"
3.750 + * NaN value.
3.751 + *
3.752 + * <p>In all cases, the result is an integer that, when given to the
3.753 + * {@link #intBitsToFloat(int)} method, will produce a
3.754 + * floating-point value the same as the argument to
3.755 + * {@code floatToRawIntBits}.
3.756 + *
3.757 + * @param value a floating-point number.
3.758 + * @return the bits that represent the floating-point number.
3.759 + * @since 1.3
3.760 + */
3.761 + public static native int floatToRawIntBits(float value);
3.762 +
3.763 + /**
3.764 + * Returns the {@code float} value corresponding to a given
3.765 + * bit representation.
3.766 + * The argument is considered to be a representation of a
3.767 + * floating-point value according to the IEEE 754 floating-point
3.768 + * "single format" bit layout.
3.769 + *
3.770 + * <p>If the argument is {@code 0x7f800000}, the result is positive
3.771 + * infinity.
3.772 + *
3.773 + * <p>If the argument is {@code 0xff800000}, the result is negative
3.774 + * infinity.
3.775 + *
3.776 + * <p>If the argument is any value in the range
3.777 + * {@code 0x7f800001} through {@code 0x7fffffff} or in
3.778 + * the range {@code 0xff800001} through
3.779 + * {@code 0xffffffff}, the result is a NaN. No IEEE 754
3.780 + * floating-point operation provided by Java can distinguish
3.781 + * between two NaN values of the same type with different bit
3.782 + * patterns. Distinct values of NaN are only distinguishable by
3.783 + * use of the {@code Float.floatToRawIntBits} method.
3.784 + *
3.785 + * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
3.786 + * values that can be computed from the argument:
3.787 + *
3.788 + * <blockquote><pre>
3.789 + * int s = ((bits >> 31) == 0) ? 1 : -1;
3.790 + * int e = ((bits >> 23) & 0xff);
3.791 + * int m = (e == 0) ?
3.792 + * (bits & 0x7fffff) << 1 :
3.793 + * (bits & 0x7fffff) | 0x800000;
3.794 + * </pre></blockquote>
3.795 + *
3.796 + * Then the floating-point result equals the value of the mathematical
3.797 + * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-150</sup>.
3.798 + *
3.799 + * <p>Note that this method may not be able to return a
3.800 + * {@code float} NaN with exactly same bit pattern as the
3.801 + * {@code int} argument. IEEE 754 distinguishes between two
3.802 + * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
3.803 + * differences between the two kinds of NaN are generally not
3.804 + * visible in Java. Arithmetic operations on signaling NaNs turn
3.805 + * them into quiet NaNs with a different, but often similar, bit
3.806 + * pattern. However, on some processors merely copying a
3.807 + * signaling NaN also performs that conversion. In particular,
3.808 + * copying a signaling NaN to return it to the calling method may
3.809 + * perform this conversion. So {@code intBitsToFloat} may
3.810 + * not be able to return a {@code float} with a signaling NaN
3.811 + * bit pattern. Consequently, for some {@code int} values,
3.812 + * {@code floatToRawIntBits(intBitsToFloat(start))} may
3.813 + * <i>not</i> equal {@code start}. Moreover, which
3.814 + * particular bit patterns represent signaling NaNs is platform
3.815 + * dependent; although all NaN bit patterns, quiet or signaling,
3.816 + * must be in the NaN range identified above.
3.817 + *
3.818 + * @param bits an integer.
3.819 + * @return the {@code float} floating-point value with the same bit
3.820 + * pattern.
3.821 + */
3.822 + public static native float intBitsToFloat(int bits);
3.823 +
3.824 + /**
3.825 + * Compares two {@code Float} objects numerically. There are
3.826 + * two ways in which comparisons performed by this method differ
3.827 + * from those performed by the Java language numerical comparison
3.828 + * operators ({@code <, <=, ==, >=, >}) when
3.829 + * applied to primitive {@code float} values:
3.830 + *
3.831 + * <ul><li>
3.832 + * {@code Float.NaN} is considered by this method to
3.833 + * be equal to itself and greater than all other
3.834 + * {@code float} values
3.835 + * (including {@code Float.POSITIVE_INFINITY}).
3.836 + * <li>
3.837 + * {@code 0.0f} is considered by this method to be greater
3.838 + * than {@code -0.0f}.
3.839 + * </ul>
3.840 + *
3.841 + * This ensures that the <i>natural ordering</i> of {@code Float}
3.842 + * objects imposed by this method is <i>consistent with equals</i>.
3.843 + *
3.844 + * @param anotherFloat the {@code Float} to be compared.
3.845 + * @return the value {@code 0} if {@code anotherFloat} is
3.846 + * numerically equal to this {@code Float}; a value
3.847 + * less than {@code 0} if this {@code Float}
3.848 + * is numerically less than {@code anotherFloat};
3.849 + * and a value greater than {@code 0} if this
3.850 + * {@code Float} is numerically greater than
3.851 + * {@code anotherFloat}.
3.852 + *
3.853 + * @since 1.2
3.854 + * @see Comparable#compareTo(Object)
3.855 + */
3.856 + public int compareTo(Float anotherFloat) {
3.857 + return Float.compare(value, anotherFloat.value);
3.858 + }
3.859 +
3.860 + /**
3.861 + * Compares the two specified {@code float} values. The sign
3.862 + * of the integer value returned is the same as that of the
3.863 + * integer that would be returned by the call:
3.864 + * <pre>
3.865 + * new Float(f1).compareTo(new Float(f2))
3.866 + * </pre>
3.867 + *
3.868 + * @param f1 the first {@code float} to compare.
3.869 + * @param f2 the second {@code float} to compare.
3.870 + * @return the value {@code 0} if {@code f1} is
3.871 + * numerically equal to {@code f2}; a value less than
3.872 + * {@code 0} if {@code f1} is numerically less than
3.873 + * {@code f2}; and a value greater than {@code 0}
3.874 + * if {@code f1} is numerically greater than
3.875 + * {@code f2}.
3.876 + * @since 1.4
3.877 + */
3.878 + public static int compare(float f1, float f2) {
3.879 + if (f1 < f2)
3.880 + return -1; // Neither val is NaN, thisVal is smaller
3.881 + if (f1 > f2)
3.882 + return 1; // Neither val is NaN, thisVal is larger
3.883 +
3.884 + // Cannot use floatToRawIntBits because of possibility of NaNs.
3.885 + int thisBits = Float.floatToIntBits(f1);
3.886 + int anotherBits = Float.floatToIntBits(f2);
3.887 +
3.888 + return (thisBits == anotherBits ? 0 : // Values are equal
3.889 + (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
3.890 + 1)); // (0.0, -0.0) or (NaN, !NaN)
3.891 + }
3.892 +
3.893 + /** use serialVersionUID from JDK 1.0.2 for interoperability */
3.894 + private static final long serialVersionUID = -2671257302660747028L;
3.895 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/emul/src/main/java/java/lang/Long.java Sat Sep 29 10:56:23 2012 +0200
4.3 @@ -0,0 +1,1199 @@
4.4 +/*
4.5 + * Copyright (c) 1994, 2009, Oracle and/or its affiliates. All rights reserved.
4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4.7 + *
4.8 + * This code is free software; you can redistribute it and/or modify it
4.9 + * under the terms of the GNU General Public License version 2 only, as
4.10 + * published by the Free Software Foundation. Oracle designates this
4.11 + * particular file as subject to the "Classpath" exception as provided
4.12 + * by Oracle in the LICENSE file that accompanied this code.
4.13 + *
4.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
4.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4.17 + * version 2 for more details (a copy is included in the LICENSE file that
4.18 + * accompanied this code).
4.19 + *
4.20 + * You should have received a copy of the GNU General Public License version
4.21 + * 2 along with this work; if not, write to the Free Software Foundation,
4.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4.23 + *
4.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4.25 + * or visit www.oracle.com if you need additional information or have any
4.26 + * questions.
4.27 + */
4.28 +
4.29 +package java.lang;
4.30 +
4.31 +/**
4.32 + * The {@code Long} class wraps a value of the primitive type {@code
4.33 + * long} in an object. An object of type {@code Long} contains a
4.34 + * single field whose type is {@code long}.
4.35 + *
4.36 + * <p> In addition, this class provides several methods for converting
4.37 + * a {@code long} to a {@code String} and a {@code String} to a {@code
4.38 + * long}, as well as other constants and methods useful when dealing
4.39 + * with a {@code long}.
4.40 + *
4.41 + * <p>Implementation note: The implementations of the "bit twiddling"
4.42 + * methods (such as {@link #highestOneBit(long) highestOneBit} and
4.43 + * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
4.44 + * based on material from Henry S. Warren, Jr.'s <i>Hacker's
4.45 + * Delight</i>, (Addison Wesley, 2002).
4.46 + *
4.47 + * @author Lee Boynton
4.48 + * @author Arthur van Hoff
4.49 + * @author Josh Bloch
4.50 + * @author Joseph D. Darcy
4.51 + * @since JDK1.0
4.52 + */
4.53 +public final class Long extends Number implements Comparable<Long> {
4.54 + /**
4.55 + * A constant holding the minimum value a {@code long} can
4.56 + * have, -2<sup>63</sup>.
4.57 + */
4.58 + public static final long MIN_VALUE = 0x8000000000000000L;
4.59 +
4.60 + /**
4.61 + * A constant holding the maximum value a {@code long} can
4.62 + * have, 2<sup>63</sup>-1.
4.63 + */
4.64 + public static final long MAX_VALUE = 0x7fffffffffffffffL;
4.65 +
4.66 + /**
4.67 + * The {@code Class} instance representing the primitive type
4.68 + * {@code long}.
4.69 + *
4.70 + * @since JDK1.1
4.71 + */
4.72 + public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long");
4.73 +
4.74 + /**
4.75 + * Returns a string representation of the first argument in the
4.76 + * radix specified by the second argument.
4.77 + *
4.78 + * <p>If the radix is smaller than {@code Character.MIN_RADIX}
4.79 + * or larger than {@code Character.MAX_RADIX}, then the radix
4.80 + * {@code 10} is used instead.
4.81 + *
4.82 + * <p>If the first argument is negative, the first element of the
4.83 + * result is the ASCII minus sign {@code '-'}
4.84 + * (<code>'\u002d'</code>). If the first argument is not
4.85 + * negative, no sign character appears in the result.
4.86 + *
4.87 + * <p>The remaining characters of the result represent the magnitude
4.88 + * of the first argument. If the magnitude is zero, it is
4.89 + * represented by a single zero character {@code '0'}
4.90 + * (<code>'\u0030'</code>); otherwise, the first character of
4.91 + * the representation of the magnitude will not be the zero
4.92 + * character. The following ASCII characters are used as digits:
4.93 + *
4.94 + * <blockquote>
4.95 + * {@code 0123456789abcdefghijklmnopqrstuvwxyz}
4.96 + * </blockquote>
4.97 + *
4.98 + * These are <code>'\u0030'</code> through
4.99 + * <code>'\u0039'</code> and <code>'\u0061'</code> through
4.100 + * <code>'\u007a'</code>. If {@code radix} is
4.101 + * <var>N</var>, then the first <var>N</var> of these characters
4.102 + * are used as radix-<var>N</var> digits in the order shown. Thus,
4.103 + * the digits for hexadecimal (radix 16) are
4.104 + * {@code 0123456789abcdef}. If uppercase letters are
4.105 + * desired, the {@link java.lang.String#toUpperCase()} method may
4.106 + * be called on the result:
4.107 + *
4.108 + * <blockquote>
4.109 + * {@code Long.toString(n, 16).toUpperCase()}
4.110 + * </blockquote>
4.111 + *
4.112 + * @param i a {@code long} to be converted to a string.
4.113 + * @param radix the radix to use in the string representation.
4.114 + * @return a string representation of the argument in the specified radix.
4.115 + * @see java.lang.Character#MAX_RADIX
4.116 + * @see java.lang.Character#MIN_RADIX
4.117 + */
4.118 + public static String toString(long i, int radix) {
4.119 + if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
4.120 + radix = 10;
4.121 + if (radix == 10)
4.122 + return toString(i);
4.123 + char[] buf = new char[65];
4.124 + int charPos = 64;
4.125 + boolean negative = (i < 0);
4.126 +
4.127 + if (!negative) {
4.128 + i = -i;
4.129 + }
4.130 +
4.131 + while (i <= -radix) {
4.132 + buf[charPos--] = Integer.digits[(int)(-(i % radix))];
4.133 + i = i / radix;
4.134 + }
4.135 + buf[charPos] = Integer.digits[(int)(-i)];
4.136 +
4.137 + if (negative) {
4.138 + buf[--charPos] = '-';
4.139 + }
4.140 +
4.141 + return new String(buf, charPos, (65 - charPos));
4.142 + }
4.143 +
4.144 + /**
4.145 + * Returns a string representation of the {@code long}
4.146 + * argument as an unsigned integer in base 16.
4.147 + *
4.148 + * <p>The unsigned {@code long} value is the argument plus
4.149 + * 2<sup>64</sup> if the argument is negative; otherwise, it is
4.150 + * equal to the argument. This value is converted to a string of
4.151 + * ASCII digits in hexadecimal (base 16) with no extra
4.152 + * leading {@code 0}s. If the unsigned magnitude is zero, it
4.153 + * is represented by a single zero character {@code '0'}
4.154 + * (<code>'\u0030'</code>); otherwise, the first character of
4.155 + * the representation of the unsigned magnitude will not be the
4.156 + * zero character. The following characters are used as
4.157 + * hexadecimal digits:
4.158 + *
4.159 + * <blockquote>
4.160 + * {@code 0123456789abcdef}
4.161 + * </blockquote>
4.162 + *
4.163 + * These are the characters <code>'\u0030'</code> through
4.164 + * <code>'\u0039'</code> and <code>'\u0061'</code> through
4.165 + * <code>'\u0066'</code>. If uppercase letters are desired,
4.166 + * the {@link java.lang.String#toUpperCase()} method may be called
4.167 + * on the result:
4.168 + *
4.169 + * <blockquote>
4.170 + * {@code Long.toHexString(n).toUpperCase()}
4.171 + * </blockquote>
4.172 + *
4.173 + * @param i a {@code long} to be converted to a string.
4.174 + * @return the string representation of the unsigned {@code long}
4.175 + * value represented by the argument in hexadecimal
4.176 + * (base 16).
4.177 + * @since JDK 1.0.2
4.178 + */
4.179 + public static String toHexString(long i) {
4.180 + return toUnsignedString(i, 4);
4.181 + }
4.182 +
4.183 + /**
4.184 + * Returns a string representation of the {@code long}
4.185 + * argument as an unsigned integer in base 8.
4.186 + *
4.187 + * <p>The unsigned {@code long} value is the argument plus
4.188 + * 2<sup>64</sup> if the argument is negative; otherwise, it is
4.189 + * equal to the argument. This value is converted to a string of
4.190 + * ASCII digits in octal (base 8) with no extra leading
4.191 + * {@code 0}s.
4.192 + *
4.193 + * <p>If the unsigned magnitude is zero, it is represented by a
4.194 + * single zero character {@code '0'}
4.195 + * (<code>'\u0030'</code>); otherwise, the first character of
4.196 + * the representation of the unsigned magnitude will not be the
4.197 + * zero character. The following characters are used as octal
4.198 + * digits:
4.199 + *
4.200 + * <blockquote>
4.201 + * {@code 01234567}
4.202 + * </blockquote>
4.203 + *
4.204 + * These are the characters <code>'\u0030'</code> through
4.205 + * <code>'\u0037'</code>.
4.206 + *
4.207 + * @param i a {@code long} to be converted to a string.
4.208 + * @return the string representation of the unsigned {@code long}
4.209 + * value represented by the argument in octal (base 8).
4.210 + * @since JDK 1.0.2
4.211 + */
4.212 + public static String toOctalString(long i) {
4.213 + return toUnsignedString(i, 3);
4.214 + }
4.215 +
4.216 + /**
4.217 + * Returns a string representation of the {@code long}
4.218 + * argument as an unsigned integer in base 2.
4.219 + *
4.220 + * <p>The unsigned {@code long} value is the argument plus
4.221 + * 2<sup>64</sup> if the argument is negative; otherwise, it is
4.222 + * equal to the argument. This value is converted to a string of
4.223 + * ASCII digits in binary (base 2) with no extra leading
4.224 + * {@code 0}s. If the unsigned magnitude is zero, it is
4.225 + * represented by a single zero character {@code '0'}
4.226 + * (<code>'\u0030'</code>); otherwise, the first character of
4.227 + * the representation of the unsigned magnitude will not be the
4.228 + * zero character. The characters {@code '0'}
4.229 + * (<code>'\u0030'</code>) and {@code '1'}
4.230 + * (<code>'\u0031'</code>) are used as binary digits.
4.231 + *
4.232 + * @param i a {@code long} to be converted to a string.
4.233 + * @return the string representation of the unsigned {@code long}
4.234 + * value represented by the argument in binary (base 2).
4.235 + * @since JDK 1.0.2
4.236 + */
4.237 + public static String toBinaryString(long i) {
4.238 + return toUnsignedString(i, 1);
4.239 + }
4.240 +
4.241 + /**
4.242 + * Convert the integer to an unsigned number.
4.243 + */
4.244 + private static String toUnsignedString(long i, int shift) {
4.245 + char[] buf = new char[64];
4.246 + int charPos = 64;
4.247 + int radix = 1 << shift;
4.248 + long mask = radix - 1;
4.249 + do {
4.250 + buf[--charPos] = Integer.digits[(int)(i & mask)];
4.251 + i >>>= shift;
4.252 + } while (i != 0);
4.253 + return new String(buf, charPos, (64 - charPos));
4.254 + }
4.255 +
4.256 + /**
4.257 + * Returns a {@code String} object representing the specified
4.258 + * {@code long}. The argument is converted to signed decimal
4.259 + * representation and returned as a string, exactly as if the
4.260 + * argument and the radix 10 were given as arguments to the {@link
4.261 + * #toString(long, int)} method.
4.262 + *
4.263 + * @param i a {@code long} to be converted.
4.264 + * @return a string representation of the argument in base 10.
4.265 + */
4.266 + public static String toString(long i) {
4.267 + if (i == Long.MIN_VALUE)
4.268 + return "-9223372036854775808";
4.269 + int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
4.270 + char[] buf = new char[size];
4.271 + getChars(i, size, buf);
4.272 + return new String(0, size, buf);
4.273 + }
4.274 +
4.275 + /**
4.276 + * Places characters representing the integer i into the
4.277 + * character array buf. The characters are placed into
4.278 + * the buffer backwards starting with the least significant
4.279 + * digit at the specified index (exclusive), and working
4.280 + * backwards from there.
4.281 + *
4.282 + * Will fail if i == Long.MIN_VALUE
4.283 + */
4.284 + static void getChars(long i, int index, char[] buf) {
4.285 + long q;
4.286 + int r;
4.287 + int charPos = index;
4.288 + char sign = 0;
4.289 +
4.290 + if (i < 0) {
4.291 + sign = '-';
4.292 + i = -i;
4.293 + }
4.294 +
4.295 + // Get 2 digits/iteration using longs until quotient fits into an int
4.296 + while (i > Integer.MAX_VALUE) {
4.297 + q = i / 100;
4.298 + // really: r = i - (q * 100);
4.299 + r = (int)(i - ((q << 6) + (q << 5) + (q << 2)));
4.300 + i = q;
4.301 + buf[--charPos] = Integer.DigitOnes[r];
4.302 + buf[--charPos] = Integer.DigitTens[r];
4.303 + }
4.304 +
4.305 + // Get 2 digits/iteration using ints
4.306 + int q2;
4.307 + int i2 = (int)i;
4.308 + while (i2 >= 65536) {
4.309 + q2 = i2 / 100;
4.310 + // really: r = i2 - (q * 100);
4.311 + r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
4.312 + i2 = q2;
4.313 + buf[--charPos] = Integer.DigitOnes[r];
4.314 + buf[--charPos] = Integer.DigitTens[r];
4.315 + }
4.316 +
4.317 + // Fall thru to fast mode for smaller numbers
4.318 + // assert(i2 <= 65536, i2);
4.319 + for (;;) {
4.320 + q2 = (i2 * 52429) >>> (16+3);
4.321 + r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
4.322 + buf[--charPos] = Integer.digits[r];
4.323 + i2 = q2;
4.324 + if (i2 == 0) break;
4.325 + }
4.326 + if (sign != 0) {
4.327 + buf[--charPos] = sign;
4.328 + }
4.329 + }
4.330 +
4.331 + // Requires positive x
4.332 + static int stringSize(long x) {
4.333 + long p = 10;
4.334 + for (int i=1; i<19; i++) {
4.335 + if (x < p)
4.336 + return i;
4.337 + p = 10*p;
4.338 + }
4.339 + return 19;
4.340 + }
4.341 +
4.342 + /**
4.343 + * Parses the string argument as a signed {@code long} in the
4.344 + * radix specified by the second argument. The characters in the
4.345 + * string must all be digits of the specified radix (as determined
4.346 + * by whether {@link java.lang.Character#digit(char, int)} returns
4.347 + * a nonnegative value), except that the first character may be an
4.348 + * ASCII minus sign {@code '-'} (<code>'\u002D'</code>) to
4.349 + * indicate a negative value or an ASCII plus sign {@code '+'}
4.350 + * (<code>'\u002B'</code>) to indicate a positive value. The
4.351 + * resulting {@code long} value is returned.
4.352 + *
4.353 + * <p>Note that neither the character {@code L}
4.354 + * (<code>'\u004C'</code>) nor {@code l}
4.355 + * (<code>'\u006C'</code>) is permitted to appear at the end
4.356 + * of the string as a type indicator, as would be permitted in
4.357 + * Java programming language source code - except that either
4.358 + * {@code L} or {@code l} may appear as a digit for a
4.359 + * radix greater than 22.
4.360 + *
4.361 + * <p>An exception of type {@code NumberFormatException} is
4.362 + * thrown if any of the following situations occurs:
4.363 + * <ul>
4.364 + *
4.365 + * <li>The first argument is {@code null} or is a string of
4.366 + * length zero.
4.367 + *
4.368 + * <li>The {@code radix} is either smaller than {@link
4.369 + * java.lang.Character#MIN_RADIX} or larger than {@link
4.370 + * java.lang.Character#MAX_RADIX}.
4.371 + *
4.372 + * <li>Any character of the string is not a digit of the specified
4.373 + * radix, except that the first character may be a minus sign
4.374 + * {@code '-'} (<code>'\u002d'</code>) or plus sign {@code
4.375 + * '+'} (<code>'\u002B'</code>) provided that the string is
4.376 + * longer than length 1.
4.377 + *
4.378 + * <li>The value represented by the string is not a value of type
4.379 + * {@code long}.
4.380 + * </ul>
4.381 + *
4.382 + * <p>Examples:
4.383 + * <blockquote><pre>
4.384 + * parseLong("0", 10) returns 0L
4.385 + * parseLong("473", 10) returns 473L
4.386 + * parseLong("+42", 10) returns 42L
4.387 + * parseLong("-0", 10) returns 0L
4.388 + * parseLong("-FF", 16) returns -255L
4.389 + * parseLong("1100110", 2) returns 102L
4.390 + * parseLong("99", 8) throws a NumberFormatException
4.391 + * parseLong("Hazelnut", 10) throws a NumberFormatException
4.392 + * parseLong("Hazelnut", 36) returns 1356099454469L
4.393 + * </pre></blockquote>
4.394 + *
4.395 + * @param s the {@code String} containing the
4.396 + * {@code long} representation to be parsed.
4.397 + * @param radix the radix to be used while parsing {@code s}.
4.398 + * @return the {@code long} represented by the string argument in
4.399 + * the specified radix.
4.400 + * @throws NumberFormatException if the string does not contain a
4.401 + * parsable {@code long}.
4.402 + */
4.403 + public static long parseLong(String s, int radix)
4.404 + throws NumberFormatException
4.405 + {
4.406 + if (s == null) {
4.407 + throw new NumberFormatException("null");
4.408 + }
4.409 +
4.410 + if (radix < Character.MIN_RADIX) {
4.411 + throw new NumberFormatException("radix " + radix +
4.412 + " less than Character.MIN_RADIX");
4.413 + }
4.414 + if (radix > Character.MAX_RADIX) {
4.415 + throw new NumberFormatException("radix " + radix +
4.416 + " greater than Character.MAX_RADIX");
4.417 + }
4.418 +
4.419 + long result = 0;
4.420 + boolean negative = false;
4.421 + int i = 0, len = s.length();
4.422 + long limit = -Long.MAX_VALUE;
4.423 + long multmin;
4.424 + int digit;
4.425 +
4.426 + if (len > 0) {
4.427 + char firstChar = s.charAt(0);
4.428 + if (firstChar < '0') { // Possible leading "+" or "-"
4.429 + if (firstChar == '-') {
4.430 + negative = true;
4.431 + limit = Long.MIN_VALUE;
4.432 + } else if (firstChar != '+')
4.433 + throw NumberFormatException.forInputString(s);
4.434 +
4.435 + if (len == 1) // Cannot have lone "+" or "-"
4.436 + throw NumberFormatException.forInputString(s);
4.437 + i++;
4.438 + }
4.439 + multmin = limit / radix;
4.440 + while (i < len) {
4.441 + // Accumulating negatively avoids surprises near MAX_VALUE
4.442 + digit = Character.digit(s.charAt(i++),radix);
4.443 + if (digit < 0) {
4.444 + throw NumberFormatException.forInputString(s);
4.445 + }
4.446 + if (result < multmin) {
4.447 + throw NumberFormatException.forInputString(s);
4.448 + }
4.449 + result *= radix;
4.450 + if (result < limit + digit) {
4.451 + throw NumberFormatException.forInputString(s);
4.452 + }
4.453 + result -= digit;
4.454 + }
4.455 + } else {
4.456 + throw NumberFormatException.forInputString(s);
4.457 + }
4.458 + return negative ? result : -result;
4.459 + }
4.460 +
4.461 + /**
4.462 + * Parses the string argument as a signed decimal {@code long}.
4.463 + * The characters in the string must all be decimal digits, except
4.464 + * that the first character may be an ASCII minus sign {@code '-'}
4.465 + * (<code>\u002D'</code>) to indicate a negative value or an
4.466 + * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
4.467 + * indicate a positive value. The resulting {@code long} value is
4.468 + * returned, exactly as if the argument and the radix {@code 10}
4.469 + * were given as arguments to the {@link
4.470 + * #parseLong(java.lang.String, int)} method.
4.471 + *
4.472 + * <p>Note that neither the character {@code L}
4.473 + * (<code>'\u004C'</code>) nor {@code l}
4.474 + * (<code>'\u006C'</code>) is permitted to appear at the end
4.475 + * of the string as a type indicator, as would be permitted in
4.476 + * Java programming language source code.
4.477 + *
4.478 + * @param s a {@code String} containing the {@code long}
4.479 + * representation to be parsed
4.480 + * @return the {@code long} represented by the argument in
4.481 + * decimal.
4.482 + * @throws NumberFormatException if the string does not contain a
4.483 + * parsable {@code long}.
4.484 + */
4.485 + public static long parseLong(String s) throws NumberFormatException {
4.486 + return parseLong(s, 10);
4.487 + }
4.488 +
4.489 + /**
4.490 + * Returns a {@code Long} object holding the value
4.491 + * extracted from the specified {@code String} when parsed
4.492 + * with the radix given by the second argument. The first
4.493 + * argument is interpreted as representing a signed
4.494 + * {@code long} in the radix specified by the second
4.495 + * argument, exactly as if the arguments were given to the {@link
4.496 + * #parseLong(java.lang.String, int)} method. The result is a
4.497 + * {@code Long} object that represents the {@code long}
4.498 + * value specified by the string.
4.499 + *
4.500 + * <p>In other words, this method returns a {@code Long} object equal
4.501 + * to the value of:
4.502 + *
4.503 + * <blockquote>
4.504 + * {@code new Long(Long.parseLong(s, radix))}
4.505 + * </blockquote>
4.506 + *
4.507 + * @param s the string to be parsed
4.508 + * @param radix the radix to be used in interpreting {@code s}
4.509 + * @return a {@code Long} object holding the value
4.510 + * represented by the string argument in the specified
4.511 + * radix.
4.512 + * @throws NumberFormatException If the {@code String} does not
4.513 + * contain a parsable {@code long}.
4.514 + */
4.515 + public static Long valueOf(String s, int radix) throws NumberFormatException {
4.516 + return Long.valueOf(parseLong(s, radix));
4.517 + }
4.518 +
4.519 + /**
4.520 + * Returns a {@code Long} object holding the value
4.521 + * of the specified {@code String}. The argument is
4.522 + * interpreted as representing a signed decimal {@code long},
4.523 + * exactly as if the argument were given to the {@link
4.524 + * #parseLong(java.lang.String)} method. The result is a
4.525 + * {@code Long} object that represents the integer value
4.526 + * specified by the string.
4.527 + *
4.528 + * <p>In other words, this method returns a {@code Long} object
4.529 + * equal to the value of:
4.530 + *
4.531 + * <blockquote>
4.532 + * {@code new Long(Long.parseLong(s))}
4.533 + * </blockquote>
4.534 + *
4.535 + * @param s the string to be parsed.
4.536 + * @return a {@code Long} object holding the value
4.537 + * represented by the string argument.
4.538 + * @throws NumberFormatException If the string cannot be parsed
4.539 + * as a {@code long}.
4.540 + */
4.541 + public static Long valueOf(String s) throws NumberFormatException
4.542 + {
4.543 + return Long.valueOf(parseLong(s, 10));
4.544 + }
4.545 +
4.546 + private static class LongCache {
4.547 + private LongCache(){}
4.548 +
4.549 + static final Long cache[] = new Long[-(-128) + 127 + 1];
4.550 +
4.551 + static {
4.552 + for(int i = 0; i < cache.length; i++)
4.553 + cache[i] = new Long(i - 128);
4.554 + }
4.555 + }
4.556 +
4.557 + /**
4.558 + * Returns a {@code Long} instance representing the specified
4.559 + * {@code long} value.
4.560 + * If a new {@code Long} instance is not required, this method
4.561 + * should generally be used in preference to the constructor
4.562 + * {@link #Long(long)}, as this method is likely to yield
4.563 + * significantly better space and time performance by caching
4.564 + * frequently requested values.
4.565 + *
4.566 + * Note that unlike the {@linkplain Integer#valueOf(int)
4.567 + * corresponding method} in the {@code Integer} class, this method
4.568 + * is <em>not</em> required to cache values within a particular
4.569 + * range.
4.570 + *
4.571 + * @param l a long value.
4.572 + * @return a {@code Long} instance representing {@code l}.
4.573 + * @since 1.5
4.574 + */
4.575 + public static Long valueOf(long l) {
4.576 + final int offset = 128;
4.577 + if (l >= -128 && l <= 127) { // will cache
4.578 + return LongCache.cache[(int)l + offset];
4.579 + }
4.580 + return new Long(l);
4.581 + }
4.582 +
4.583 + /**
4.584 + * Decodes a {@code String} into a {@code Long}.
4.585 + * Accepts decimal, hexadecimal, and octal numbers given by the
4.586 + * following grammar:
4.587 + *
4.588 + * <blockquote>
4.589 + * <dl>
4.590 + * <dt><i>DecodableString:</i>
4.591 + * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
4.592 + * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
4.593 + * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
4.594 + * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
4.595 + * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
4.596 + * <p>
4.597 + * <dt><i>Sign:</i>
4.598 + * <dd>{@code -}
4.599 + * <dd>{@code +}
4.600 + * </dl>
4.601 + * </blockquote>
4.602 + *
4.603 + * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
4.604 + * are as defined in section 3.10.1 of
4.605 + * <cite>The Java™ Language Specification</cite>,
4.606 + * except that underscores are not accepted between digits.
4.607 + *
4.608 + * <p>The sequence of characters following an optional
4.609 + * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
4.610 + * "{@code #}", or leading zero) is parsed as by the {@code
4.611 + * Long.parseLong} method with the indicated radix (10, 16, or 8).
4.612 + * This sequence of characters must represent a positive value or
4.613 + * a {@link NumberFormatException} will be thrown. The result is
4.614 + * negated if first character of the specified {@code String} is
4.615 + * the minus sign. No whitespace characters are permitted in the
4.616 + * {@code String}.
4.617 + *
4.618 + * @param nm the {@code String} to decode.
4.619 + * @return a {@code Long} object holding the {@code long}
4.620 + * value represented by {@code nm}
4.621 + * @throws NumberFormatException if the {@code String} does not
4.622 + * contain a parsable {@code long}.
4.623 + * @see java.lang.Long#parseLong(String, int)
4.624 + * @since 1.2
4.625 + */
4.626 + public static Long decode(String nm) throws NumberFormatException {
4.627 + int radix = 10;
4.628 + int index = 0;
4.629 + boolean negative = false;
4.630 + Long result;
4.631 +
4.632 + if (nm.length() == 0)
4.633 + throw new NumberFormatException("Zero length string");
4.634 + char firstChar = nm.charAt(0);
4.635 + // Handle sign, if present
4.636 + if (firstChar == '-') {
4.637 + negative = true;
4.638 + index++;
4.639 + } else if (firstChar == '+')
4.640 + index++;
4.641 +
4.642 + // Handle radix specifier, if present
4.643 + if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
4.644 + index += 2;
4.645 + radix = 16;
4.646 + }
4.647 + else if (nm.startsWith("#", index)) {
4.648 + index ++;
4.649 + radix = 16;
4.650 + }
4.651 + else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
4.652 + index ++;
4.653 + radix = 8;
4.654 + }
4.655 +
4.656 + if (nm.startsWith("-", index) || nm.startsWith("+", index))
4.657 + throw new NumberFormatException("Sign character in wrong position");
4.658 +
4.659 + try {
4.660 + result = Long.valueOf(nm.substring(index), radix);
4.661 + result = negative ? Long.valueOf(-result.longValue()) : result;
4.662 + } catch (NumberFormatException e) {
4.663 + // If number is Long.MIN_VALUE, we'll end up here. The next line
4.664 + // handles this case, and causes any genuine format error to be
4.665 + // rethrown.
4.666 + String constant = negative ? ("-" + nm.substring(index))
4.667 + : nm.substring(index);
4.668 + result = Long.valueOf(constant, radix);
4.669 + }
4.670 + return result;
4.671 + }
4.672 +
4.673 + /**
4.674 + * The value of the {@code Long}.
4.675 + *
4.676 + * @serial
4.677 + */
4.678 + private final long value;
4.679 +
4.680 + /**
4.681 + * Constructs a newly allocated {@code Long} object that
4.682 + * represents the specified {@code long} argument.
4.683 + *
4.684 + * @param value the value to be represented by the
4.685 + * {@code Long} object.
4.686 + */
4.687 + public Long(long value) {
4.688 + this.value = value;
4.689 + }
4.690 +
4.691 + /**
4.692 + * Constructs a newly allocated {@code Long} object that
4.693 + * represents the {@code long} value indicated by the
4.694 + * {@code String} parameter. The string is converted to a
4.695 + * {@code long} value in exactly the manner used by the
4.696 + * {@code parseLong} method for radix 10.
4.697 + *
4.698 + * @param s the {@code String} to be converted to a
4.699 + * {@code Long}.
4.700 + * @throws NumberFormatException if the {@code String} does not
4.701 + * contain a parsable {@code long}.
4.702 + * @see java.lang.Long#parseLong(java.lang.String, int)
4.703 + */
4.704 + public Long(String s) throws NumberFormatException {
4.705 + this.value = parseLong(s, 10);
4.706 + }
4.707 +
4.708 + /**
4.709 + * Returns the value of this {@code Long} as a
4.710 + * {@code byte}.
4.711 + */
4.712 + public byte byteValue() {
4.713 + return (byte)value;
4.714 + }
4.715 +
4.716 + /**
4.717 + * Returns the value of this {@code Long} as a
4.718 + * {@code short}.
4.719 + */
4.720 + public short shortValue() {
4.721 + return (short)value;
4.722 + }
4.723 +
4.724 + /**
4.725 + * Returns the value of this {@code Long} as an
4.726 + * {@code int}.
4.727 + */
4.728 + public int intValue() {
4.729 + return (int)value;
4.730 + }
4.731 +
4.732 + /**
4.733 + * Returns the value of this {@code Long} as a
4.734 + * {@code long} value.
4.735 + */
4.736 + public long longValue() {
4.737 + return (long)value;
4.738 + }
4.739 +
4.740 + /**
4.741 + * Returns the value of this {@code Long} as a
4.742 + * {@code float}.
4.743 + */
4.744 + public float floatValue() {
4.745 + return (float)value;
4.746 + }
4.747 +
4.748 + /**
4.749 + * Returns the value of this {@code Long} as a
4.750 + * {@code double}.
4.751 + */
4.752 + public double doubleValue() {
4.753 + return (double)value;
4.754 + }
4.755 +
4.756 + /**
4.757 + * Returns a {@code String} object representing this
4.758 + * {@code Long}'s value. The value is converted to signed
4.759 + * decimal representation and returned as a string, exactly as if
4.760 + * the {@code long} value were given as an argument to the
4.761 + * {@link java.lang.Long#toString(long)} method.
4.762 + *
4.763 + * @return a string representation of the value of this object in
4.764 + * base 10.
4.765 + */
4.766 + public String toString() {
4.767 + return toString(value);
4.768 + }
4.769 +
4.770 + /**
4.771 + * Returns a hash code for this {@code Long}. The result is
4.772 + * the exclusive OR of the two halves of the primitive
4.773 + * {@code long} value held by this {@code Long}
4.774 + * object. That is, the hashcode is the value of the expression:
4.775 + *
4.776 + * <blockquote>
4.777 + * {@code (int)(this.longValue()^(this.longValue()>>>32))}
4.778 + * </blockquote>
4.779 + *
4.780 + * @return a hash code value for this object.
4.781 + */
4.782 + public int hashCode() {
4.783 + return (int)(value ^ (value >>> 32));
4.784 + }
4.785 +
4.786 + /**
4.787 + * Compares this object to the specified object. The result is
4.788 + * {@code true} if and only if the argument is not
4.789 + * {@code null} and is a {@code Long} object that
4.790 + * contains the same {@code long} value as this object.
4.791 + *
4.792 + * @param obj the object to compare with.
4.793 + * @return {@code true} if the objects are the same;
4.794 + * {@code false} otherwise.
4.795 + */
4.796 + public boolean equals(Object obj) {
4.797 + if (obj instanceof Long) {
4.798 + return value == ((Long)obj).longValue();
4.799 + }
4.800 + return false;
4.801 + }
4.802 +
4.803 + /**
4.804 + * Determines the {@code long} value of the system property
4.805 + * with the specified name.
4.806 + *
4.807 + * <p>The first argument is treated as the name of a system property.
4.808 + * System properties are accessible through the {@link
4.809 + * java.lang.System#getProperty(java.lang.String)} method. The
4.810 + * string value of this property is then interpreted as a
4.811 + * {@code long} value and a {@code Long} object
4.812 + * representing this value is returned. Details of possible
4.813 + * numeric formats can be found with the definition of
4.814 + * {@code getProperty}.
4.815 + *
4.816 + * <p>If there is no property with the specified name, if the
4.817 + * specified name is empty or {@code null}, or if the
4.818 + * property does not have the correct numeric format, then
4.819 + * {@code null} is returned.
4.820 + *
4.821 + * <p>In other words, this method returns a {@code Long} object equal to
4.822 + * the value of:
4.823 + *
4.824 + * <blockquote>
4.825 + * {@code getLong(nm, null)}
4.826 + * </blockquote>
4.827 + *
4.828 + * @param nm property name.
4.829 + * @return the {@code Long} value of the property.
4.830 + * @see java.lang.System#getProperty(java.lang.String)
4.831 + * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
4.832 + */
4.833 + public static Long getLong(String nm) {
4.834 + return getLong(nm, null);
4.835 + }
4.836 +
4.837 + /**
4.838 + * Determines the {@code long} value of the system property
4.839 + * with the specified name.
4.840 + *
4.841 + * <p>The first argument is treated as the name of a system property.
4.842 + * System properties are accessible through the {@link
4.843 + * java.lang.System#getProperty(java.lang.String)} method. The
4.844 + * string value of this property is then interpreted as a
4.845 + * {@code long} value and a {@code Long} object
4.846 + * representing this value is returned. Details of possible
4.847 + * numeric formats can be found with the definition of
4.848 + * {@code getProperty}.
4.849 + *
4.850 + * <p>The second argument is the default value. A {@code Long} object
4.851 + * that represents the value of the second argument is returned if there
4.852 + * is no property of the specified name, if the property does not have
4.853 + * the correct numeric format, or if the specified name is empty or null.
4.854 + *
4.855 + * <p>In other words, this method returns a {@code Long} object equal
4.856 + * to the value of:
4.857 + *
4.858 + * <blockquote>
4.859 + * {@code getLong(nm, new Long(val))}
4.860 + * </blockquote>
4.861 + *
4.862 + * but in practice it may be implemented in a manner such as:
4.863 + *
4.864 + * <blockquote><pre>
4.865 + * Long result = getLong(nm, null);
4.866 + * return (result == null) ? new Long(val) : result;
4.867 + * </pre></blockquote>
4.868 + *
4.869 + * to avoid the unnecessary allocation of a {@code Long} object when
4.870 + * the default value is not needed.
4.871 + *
4.872 + * @param nm property name.
4.873 + * @param val default value.
4.874 + * @return the {@code Long} value of the property.
4.875 + * @see java.lang.System#getProperty(java.lang.String)
4.876 + * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
4.877 + */
4.878 + public static Long getLong(String nm, long val) {
4.879 + Long result = Long.getLong(nm, null);
4.880 + return (result == null) ? Long.valueOf(val) : result;
4.881 + }
4.882 +
4.883 + /**
4.884 + * Returns the {@code long} value of the system property with
4.885 + * the specified name. The first argument is treated as the name
4.886 + * of a system property. System properties are accessible through
4.887 + * the {@link java.lang.System#getProperty(java.lang.String)}
4.888 + * method. The string value of this property is then interpreted
4.889 + * as a {@code long} value, as per the
4.890 + * {@code Long.decode} method, and a {@code Long} object
4.891 + * representing this value is returned.
4.892 + *
4.893 + * <ul>
4.894 + * <li>If the property value begins with the two ASCII characters
4.895 + * {@code 0x} or the ASCII character {@code #}, not followed by
4.896 + * a minus sign, then the rest of it is parsed as a hexadecimal integer
4.897 + * exactly as for the method {@link #valueOf(java.lang.String, int)}
4.898 + * with radix 16.
4.899 + * <li>If the property value begins with the ASCII character
4.900 + * {@code 0} followed by another character, it is parsed as
4.901 + * an octal integer exactly as by the method {@link
4.902 + * #valueOf(java.lang.String, int)} with radix 8.
4.903 + * <li>Otherwise the property value is parsed as a decimal
4.904 + * integer exactly as by the method
4.905 + * {@link #valueOf(java.lang.String, int)} with radix 10.
4.906 + * </ul>
4.907 + *
4.908 + * <p>Note that, in every case, neither {@code L}
4.909 + * (<code>'\u004C'</code>) nor {@code l}
4.910 + * (<code>'\u006C'</code>) is permitted to appear at the end
4.911 + * of the property value as a type indicator, as would be
4.912 + * permitted in Java programming language source code.
4.913 + *
4.914 + * <p>The second argument is the default value. The default value is
4.915 + * returned if there is no property of the specified name, if the
4.916 + * property does not have the correct numeric format, or if the
4.917 + * specified name is empty or {@code null}.
4.918 + *
4.919 + * @param nm property name.
4.920 + * @param val default value.
4.921 + * @return the {@code Long} value of the property.
4.922 + * @see java.lang.System#getProperty(java.lang.String)
4.923 + * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
4.924 + * @see java.lang.Long#decode
4.925 + */
4.926 + public static Long getLong(String nm, Long val) {
4.927 + String v = null;
4.928 + try {
4.929 + v = System.getProperty(nm);
4.930 + } catch (IllegalArgumentException e) {
4.931 + } catch (NullPointerException e) {
4.932 + }
4.933 + if (v != null) {
4.934 + try {
4.935 + return Long.decode(v);
4.936 + } catch (NumberFormatException e) {
4.937 + }
4.938 + }
4.939 + return val;
4.940 + }
4.941 +
4.942 + /**
4.943 + * Compares two {@code Long} objects numerically.
4.944 + *
4.945 + * @param anotherLong the {@code Long} to be compared.
4.946 + * @return the value {@code 0} if this {@code Long} is
4.947 + * equal to the argument {@code Long}; a value less than
4.948 + * {@code 0} if this {@code Long} is numerically less
4.949 + * than the argument {@code Long}; and a value greater
4.950 + * than {@code 0} if this {@code Long} is numerically
4.951 + * greater than the argument {@code Long} (signed
4.952 + * comparison).
4.953 + * @since 1.2
4.954 + */
4.955 + public int compareTo(Long anotherLong) {
4.956 + return compare(this.value, anotherLong.value);
4.957 + }
4.958 +
4.959 + /**
4.960 + * Compares two {@code long} values numerically.
4.961 + * The value returned is identical to what would be returned by:
4.962 + * <pre>
4.963 + * Long.valueOf(x).compareTo(Long.valueOf(y))
4.964 + * </pre>
4.965 + *
4.966 + * @param x the first {@code long} to compare
4.967 + * @param y the second {@code long} to compare
4.968 + * @return the value {@code 0} if {@code x == y};
4.969 + * a value less than {@code 0} if {@code x < y}; and
4.970 + * a value greater than {@code 0} if {@code x > y}
4.971 + * @since 1.7
4.972 + */
4.973 + public static int compare(long x, long y) {
4.974 + return (x < y) ? -1 : ((x == y) ? 0 : 1);
4.975 + }
4.976 +
4.977 +
4.978 + // Bit Twiddling
4.979 +
4.980 + /**
4.981 + * The number of bits used to represent a {@code long} value in two's
4.982 + * complement binary form.
4.983 + *
4.984 + * @since 1.5
4.985 + */
4.986 + public static final int SIZE = 64;
4.987 +
4.988 + /**
4.989 + * Returns a {@code long} value with at most a single one-bit, in the
4.990 + * position of the highest-order ("leftmost") one-bit in the specified
4.991 + * {@code long} value. Returns zero if the specified value has no
4.992 + * one-bits in its two's complement binary representation, that is, if it
4.993 + * is equal to zero.
4.994 + *
4.995 + * @return a {@code long} value with a single one-bit, in the position
4.996 + * of the highest-order one-bit in the specified value, or zero if
4.997 + * the specified value is itself equal to zero.
4.998 + * @since 1.5
4.999 + */
4.1000 + public static long highestOneBit(long i) {
4.1001 + // HD, Figure 3-1
4.1002 + i |= (i >> 1);
4.1003 + i |= (i >> 2);
4.1004 + i |= (i >> 4);
4.1005 + i |= (i >> 8);
4.1006 + i |= (i >> 16);
4.1007 + i |= (i >> 32);
4.1008 + return i - (i >>> 1);
4.1009 + }
4.1010 +
4.1011 + /**
4.1012 + * Returns a {@code long} value with at most a single one-bit, in the
4.1013 + * position of the lowest-order ("rightmost") one-bit in the specified
4.1014 + * {@code long} value. Returns zero if the specified value has no
4.1015 + * one-bits in its two's complement binary representation, that is, if it
4.1016 + * is equal to zero.
4.1017 + *
4.1018 + * @return a {@code long} value with a single one-bit, in the position
4.1019 + * of the lowest-order one-bit in the specified value, or zero if
4.1020 + * the specified value is itself equal to zero.
4.1021 + * @since 1.5
4.1022 + */
4.1023 + public static long lowestOneBit(long i) {
4.1024 + // HD, Section 2-1
4.1025 + return i & -i;
4.1026 + }
4.1027 +
4.1028 + /**
4.1029 + * Returns the number of zero bits preceding the highest-order
4.1030 + * ("leftmost") one-bit in the two's complement binary representation
4.1031 + * of the specified {@code long} value. Returns 64 if the
4.1032 + * specified value has no one-bits in its two's complement representation,
4.1033 + * in other words if it is equal to zero.
4.1034 + *
4.1035 + * <p>Note that this method is closely related to the logarithm base 2.
4.1036 + * For all positive {@code long} values x:
4.1037 + * <ul>
4.1038 + * <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)}
4.1039 + * <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)}
4.1040 + * </ul>
4.1041 + *
4.1042 + * @return the number of zero bits preceding the highest-order
4.1043 + * ("leftmost") one-bit in the two's complement binary representation
4.1044 + * of the specified {@code long} value, or 64 if the value
4.1045 + * is equal to zero.
4.1046 + * @since 1.5
4.1047 + */
4.1048 + public static int numberOfLeadingZeros(long i) {
4.1049 + // HD, Figure 5-6
4.1050 + if (i == 0)
4.1051 + return 64;
4.1052 + int n = 1;
4.1053 + int x = (int)(i >>> 32);
4.1054 + if (x == 0) { n += 32; x = (int)i; }
4.1055 + if (x >>> 16 == 0) { n += 16; x <<= 16; }
4.1056 + if (x >>> 24 == 0) { n += 8; x <<= 8; }
4.1057 + if (x >>> 28 == 0) { n += 4; x <<= 4; }
4.1058 + if (x >>> 30 == 0) { n += 2; x <<= 2; }
4.1059 + n -= x >>> 31;
4.1060 + return n;
4.1061 + }
4.1062 +
4.1063 + /**
4.1064 + * Returns the number of zero bits following the lowest-order ("rightmost")
4.1065 + * one-bit in the two's complement binary representation of the specified
4.1066 + * {@code long} value. Returns 64 if the specified value has no
4.1067 + * one-bits in its two's complement representation, in other words if it is
4.1068 + * equal to zero.
4.1069 + *
4.1070 + * @return the number of zero bits following the lowest-order ("rightmost")
4.1071 + * one-bit in the two's complement binary representation of the
4.1072 + * specified {@code long} value, or 64 if the value is equal
4.1073 + * to zero.
4.1074 + * @since 1.5
4.1075 + */
4.1076 + public static int numberOfTrailingZeros(long i) {
4.1077 + // HD, Figure 5-14
4.1078 + int x, y;
4.1079 + if (i == 0) return 64;
4.1080 + int n = 63;
4.1081 + y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32);
4.1082 + y = x <<16; if (y != 0) { n = n -16; x = y; }
4.1083 + y = x << 8; if (y != 0) { n = n - 8; x = y; }
4.1084 + y = x << 4; if (y != 0) { n = n - 4; x = y; }
4.1085 + y = x << 2; if (y != 0) { n = n - 2; x = y; }
4.1086 + return n - ((x << 1) >>> 31);
4.1087 + }
4.1088 +
4.1089 + /**
4.1090 + * Returns the number of one-bits in the two's complement binary
4.1091 + * representation of the specified {@code long} value. This function is
4.1092 + * sometimes referred to as the <i>population count</i>.
4.1093 + *
4.1094 + * @return the number of one-bits in the two's complement binary
4.1095 + * representation of the specified {@code long} value.
4.1096 + * @since 1.5
4.1097 + */
4.1098 + public static int bitCount(long i) {
4.1099 + // HD, Figure 5-14
4.1100 + i = i - ((i >>> 1) & 0x5555555555555555L);
4.1101 + i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L);
4.1102 + i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
4.1103 + i = i + (i >>> 8);
4.1104 + i = i + (i >>> 16);
4.1105 + i = i + (i >>> 32);
4.1106 + return (int)i & 0x7f;
4.1107 + }
4.1108 +
4.1109 + /**
4.1110 + * Returns the value obtained by rotating the two's complement binary
4.1111 + * representation of the specified {@code long} value left by the
4.1112 + * specified number of bits. (Bits shifted out of the left hand, or
4.1113 + * high-order, side reenter on the right, or low-order.)
4.1114 + *
4.1115 + * <p>Note that left rotation with a negative distance is equivalent to
4.1116 + * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
4.1117 + * distance)}. Note also that rotation by any multiple of 64 is a
4.1118 + * no-op, so all but the last six bits of the rotation distance can be
4.1119 + * ignored, even if the distance is negative: {@code rotateLeft(val,
4.1120 + * distance) == rotateLeft(val, distance & 0x3F)}.
4.1121 + *
4.1122 + * @return the value obtained by rotating the two's complement binary
4.1123 + * representation of the specified {@code long} value left by the
4.1124 + * specified number of bits.
4.1125 + * @since 1.5
4.1126 + */
4.1127 + public static long rotateLeft(long i, int distance) {
4.1128 + return (i << distance) | (i >>> -distance);
4.1129 + }
4.1130 +
4.1131 + /**
4.1132 + * Returns the value obtained by rotating the two's complement binary
4.1133 + * representation of the specified {@code long} value right by the
4.1134 + * specified number of bits. (Bits shifted out of the right hand, or
4.1135 + * low-order, side reenter on the left, or high-order.)
4.1136 + *
4.1137 + * <p>Note that right rotation with a negative distance is equivalent to
4.1138 + * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
4.1139 + * distance)}. Note also that rotation by any multiple of 64 is a
4.1140 + * no-op, so all but the last six bits of the rotation distance can be
4.1141 + * ignored, even if the distance is negative: {@code rotateRight(val,
4.1142 + * distance) == rotateRight(val, distance & 0x3F)}.
4.1143 + *
4.1144 + * @return the value obtained by rotating the two's complement binary
4.1145 + * representation of the specified {@code long} value right by the
4.1146 + * specified number of bits.
4.1147 + * @since 1.5
4.1148 + */
4.1149 + public static long rotateRight(long i, int distance) {
4.1150 + return (i >>> distance) | (i << -distance);
4.1151 + }
4.1152 +
4.1153 + /**
4.1154 + * Returns the value obtained by reversing the order of the bits in the
4.1155 + * two's complement binary representation of the specified {@code long}
4.1156 + * value.
4.1157 + *
4.1158 + * @return the value obtained by reversing order of the bits in the
4.1159 + * specified {@code long} value.
4.1160 + * @since 1.5
4.1161 + */
4.1162 + public static long reverse(long i) {
4.1163 + // HD, Figure 7-1
4.1164 + i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L;
4.1165 + i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L;
4.1166 + i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL;
4.1167 + i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
4.1168 + i = (i << 48) | ((i & 0xffff0000L) << 16) |
4.1169 + ((i >>> 16) & 0xffff0000L) | (i >>> 48);
4.1170 + return i;
4.1171 + }
4.1172 +
4.1173 + /**
4.1174 + * Returns the signum function of the specified {@code long} value. (The
4.1175 + * return value is -1 if the specified value is negative; 0 if the
4.1176 + * specified value is zero; and 1 if the specified value is positive.)
4.1177 + *
4.1178 + * @return the signum function of the specified {@code long} value.
4.1179 + * @since 1.5
4.1180 + */
4.1181 + public static int signum(long i) {
4.1182 + // HD, Section 2-7
4.1183 + return (int) ((i >> 63) | (-i >>> 63));
4.1184 + }
4.1185 +
4.1186 + /**
4.1187 + * Returns the value obtained by reversing the order of the bytes in the
4.1188 + * two's complement representation of the specified {@code long} value.
4.1189 + *
4.1190 + * @return the value obtained by reversing the bytes in the specified
4.1191 + * {@code long} value.
4.1192 + * @since 1.5
4.1193 + */
4.1194 + public static long reverseBytes(long i) {
4.1195 + i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
4.1196 + return (i << 48) | ((i & 0xffff0000L) << 16) |
4.1197 + ((i >>> 16) & 0xffff0000L) | (i >>> 48);
4.1198 + }
4.1199 +
4.1200 + /** use serialVersionUID from JDK 1.0.2 for interoperability */
4.1201 + private static final long serialVersionUID = 4290774380558885855L;
4.1202 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/emul/src/main/java/java/lang/Math.java Sat Sep 29 10:56:23 2012 +0200
5.3 @@ -0,0 +1,1526 @@
5.4 +/*
5.5 + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5.7 + *
5.8 + * This code is free software; you can redistribute it and/or modify it
5.9 + * under the terms of the GNU General Public License version 2 only, as
5.10 + * published by the Free Software Foundation. Oracle designates this
5.11 + * particular file as subject to the "Classpath" exception as provided
5.12 + * by Oracle in the LICENSE file that accompanied this code.
5.13 + *
5.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
5.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5.17 + * version 2 for more details (a copy is included in the LICENSE file that
5.18 + * accompanied this code).
5.19 + *
5.20 + * You should have received a copy of the GNU General Public License version
5.21 + * 2 along with this work; if not, write to the Free Software Foundation,
5.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5.23 + *
5.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5.25 + * or visit www.oracle.com if you need additional information or have any
5.26 + * questions.
5.27 + */
5.28 +
5.29 +package java.lang;
5.30 +import java.util.Random;
5.31 +
5.32 +
5.33 +/**
5.34 + * The class {@code Math} contains methods for performing basic
5.35 + * numeric operations such as the elementary exponential, logarithm,
5.36 + * square root, and trigonometric functions.
5.37 + *
5.38 + * <p>Unlike some of the numeric methods of class
5.39 + * {@code StrictMath}, all implementations of the equivalent
5.40 + * functions of class {@code Math} are not defined to return the
5.41 + * bit-for-bit same results. This relaxation permits
5.42 + * better-performing implementations where strict reproducibility is
5.43 + * not required.
5.44 + *
5.45 + * <p>By default many of the {@code Math} methods simply call
5.46 + * the equivalent method in {@code StrictMath} for their
5.47 + * implementation. Code generators are encouraged to use
5.48 + * platform-specific native libraries or microprocessor instructions,
5.49 + * where available, to provide higher-performance implementations of
5.50 + * {@code Math} methods. Such higher-performance
5.51 + * implementations still must conform to the specification for
5.52 + * {@code Math}.
5.53 + *
5.54 + * <p>The quality of implementation specifications concern two
5.55 + * properties, accuracy of the returned result and monotonicity of the
5.56 + * method. Accuracy of the floating-point {@code Math} methods
5.57 + * is measured in terms of <i>ulps</i>, units in the last place. For
5.58 + * a given floating-point format, an ulp of a specific real number
5.59 + * value is the distance between the two floating-point values
5.60 + * bracketing that numerical value. When discussing the accuracy of a
5.61 + * method as a whole rather than at a specific argument, the number of
5.62 + * ulps cited is for the worst-case error at any argument. If a
5.63 + * method always has an error less than 0.5 ulps, the method always
5.64 + * returns the floating-point number nearest the exact result; such a
5.65 + * method is <i>correctly rounded</i>. A correctly rounded method is
5.66 + * generally the best a floating-point approximation can be; however,
5.67 + * it is impractical for many floating-point methods to be correctly
5.68 + * rounded. Instead, for the {@code Math} class, a larger error
5.69 + * bound of 1 or 2 ulps is allowed for certain methods. Informally,
5.70 + * with a 1 ulp error bound, when the exact result is a representable
5.71 + * number, the exact result should be returned as the computed result;
5.72 + * otherwise, either of the two floating-point values which bracket
5.73 + * the exact result may be returned. For exact results large in
5.74 + * magnitude, one of the endpoints of the bracket may be infinite.
5.75 + * Besides accuracy at individual arguments, maintaining proper
5.76 + * relations between the method at different arguments is also
5.77 + * important. Therefore, most methods with more than 0.5 ulp errors
5.78 + * are required to be <i>semi-monotonic</i>: whenever the mathematical
5.79 + * function is non-decreasing, so is the floating-point approximation,
5.80 + * likewise, whenever the mathematical function is non-increasing, so
5.81 + * is the floating-point approximation. Not all approximations that
5.82 + * have 1 ulp accuracy will automatically meet the monotonicity
5.83 + * requirements.
5.84 + *
5.85 + * @author unascribed
5.86 + * @author Joseph D. Darcy
5.87 + * @since JDK1.0
5.88 + */
5.89 +
5.90 +public final class Math {
5.91 +
5.92 + /**
5.93 + * Don't let anyone instantiate this class.
5.94 + */
5.95 + private Math() {}
5.96 +
5.97 + /**
5.98 + * The {@code double} value that is closer than any other to
5.99 + * <i>e</i>, the base of the natural logarithms.
5.100 + */
5.101 + public static final double E = 2.7182818284590452354;
5.102 +
5.103 + /**
5.104 + * The {@code double} value that is closer than any other to
5.105 + * <i>pi</i>, the ratio of the circumference of a circle to its
5.106 + * diameter.
5.107 + */
5.108 + public static final double PI = 3.14159265358979323846;
5.109 +
5.110 + /**
5.111 + * Returns the trigonometric sine of an angle. Special cases:
5.112 + * <ul><li>If the argument is NaN or an infinity, then the
5.113 + * result is NaN.
5.114 + * <li>If the argument is zero, then the result is a zero with the
5.115 + * same sign as the argument.</ul>
5.116 + *
5.117 + * <p>The computed result must be within 1 ulp of the exact result.
5.118 + * Results must be semi-monotonic.
5.119 + *
5.120 + * @param a an angle, in radians.
5.121 + * @return the sine of the argument.
5.122 + */
5.123 + public static double sin(double a) {
5.124 + return StrictMath.sin(a); // default impl. delegates to StrictMath
5.125 + }
5.126 +
5.127 + /**
5.128 + * Returns the trigonometric cosine of an angle. Special cases:
5.129 + * <ul><li>If the argument is NaN or an infinity, then the
5.130 + * result is NaN.</ul>
5.131 + *
5.132 + * <p>The computed result must be within 1 ulp of the exact result.
5.133 + * Results must be semi-monotonic.
5.134 + *
5.135 + * @param a an angle, in radians.
5.136 + * @return the cosine of the argument.
5.137 + */
5.138 + public static double cos(double a) {
5.139 + return StrictMath.cos(a); // default impl. delegates to StrictMath
5.140 + }
5.141 +
5.142 + /**
5.143 + * Returns the trigonometric tangent of an angle. Special cases:
5.144 + * <ul><li>If the argument is NaN or an infinity, then the result
5.145 + * is NaN.
5.146 + * <li>If the argument is zero, then the result is a zero with the
5.147 + * same sign as the argument.</ul>
5.148 + *
5.149 + * <p>The computed result must be within 1 ulp of the exact result.
5.150 + * Results must be semi-monotonic.
5.151 + *
5.152 + * @param a an angle, in radians.
5.153 + * @return the tangent of the argument.
5.154 + */
5.155 + public static double tan(double a) {
5.156 + return StrictMath.tan(a); // default impl. delegates to StrictMath
5.157 + }
5.158 +
5.159 + /**
5.160 + * Returns the arc sine of a value; the returned angle is in the
5.161 + * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
5.162 + * <ul><li>If the argument is NaN or its absolute value is greater
5.163 + * than 1, then the result is NaN.
5.164 + * <li>If the argument is zero, then the result is a zero with the
5.165 + * same sign as the argument.</ul>
5.166 + *
5.167 + * <p>The computed result must be within 1 ulp of the exact result.
5.168 + * Results must be semi-monotonic.
5.169 + *
5.170 + * @param a the value whose arc sine is to be returned.
5.171 + * @return the arc sine of the argument.
5.172 + */
5.173 + public static double asin(double a) {
5.174 + return StrictMath.asin(a); // default impl. delegates to StrictMath
5.175 + }
5.176 +
5.177 + /**
5.178 + * Returns the arc cosine of a value; the returned angle is in the
5.179 + * range 0.0 through <i>pi</i>. Special case:
5.180 + * <ul><li>If the argument is NaN or its absolute value is greater
5.181 + * than 1, then the result is NaN.</ul>
5.182 + *
5.183 + * <p>The computed result must be within 1 ulp of the exact result.
5.184 + * Results must be semi-monotonic.
5.185 + *
5.186 + * @param a the value whose arc cosine is to be returned.
5.187 + * @return the arc cosine of the argument.
5.188 + */
5.189 + public static double acos(double a) {
5.190 + return StrictMath.acos(a); // default impl. delegates to StrictMath
5.191 + }
5.192 +
5.193 + /**
5.194 + * Returns the arc tangent of a value; the returned angle is in the
5.195 + * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
5.196 + * <ul><li>If the argument is NaN, then the result is NaN.
5.197 + * <li>If the argument is zero, then the result is a zero with the
5.198 + * same sign as the argument.</ul>
5.199 + *
5.200 + * <p>The computed result must be within 1 ulp of the exact result.
5.201 + * Results must be semi-monotonic.
5.202 + *
5.203 + * @param a the value whose arc tangent is to be returned.
5.204 + * @return the arc tangent of the argument.
5.205 + */
5.206 + public static double atan(double a) {
5.207 + return StrictMath.atan(a); // default impl. delegates to StrictMath
5.208 + }
5.209 +
5.210 + /**
5.211 + * Converts an angle measured in degrees to an approximately
5.212 + * equivalent angle measured in radians. The conversion from
5.213 + * degrees to radians is generally inexact.
5.214 + *
5.215 + * @param angdeg an angle, in degrees
5.216 + * @return the measurement of the angle {@code angdeg}
5.217 + * in radians.
5.218 + * @since 1.2
5.219 + */
5.220 + public static double toRadians(double angdeg) {
5.221 + return angdeg / 180.0 * PI;
5.222 + }
5.223 +
5.224 + /**
5.225 + * Converts an angle measured in radians to an approximately
5.226 + * equivalent angle measured in degrees. The conversion from
5.227 + * radians to degrees is generally inexact; users should
5.228 + * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
5.229 + * equal {@code 0.0}.
5.230 + *
5.231 + * @param angrad an angle, in radians
5.232 + * @return the measurement of the angle {@code angrad}
5.233 + * in degrees.
5.234 + * @since 1.2
5.235 + */
5.236 + public static double toDegrees(double angrad) {
5.237 + return angrad * 180.0 / PI;
5.238 + }
5.239 +
5.240 + /**
5.241 + * Returns Euler's number <i>e</i> raised to the power of a
5.242 + * {@code double} value. Special cases:
5.243 + * <ul><li>If the argument is NaN, the result is NaN.
5.244 + * <li>If the argument is positive infinity, then the result is
5.245 + * positive infinity.
5.246 + * <li>If the argument is negative infinity, then the result is
5.247 + * positive zero.</ul>
5.248 + *
5.249 + * <p>The computed result must be within 1 ulp of the exact result.
5.250 + * Results must be semi-monotonic.
5.251 + *
5.252 + * @param a the exponent to raise <i>e</i> to.
5.253 + * @return the value <i>e</i><sup>{@code a}</sup>,
5.254 + * where <i>e</i> is the base of the natural logarithms.
5.255 + */
5.256 + public static double exp(double a) {
5.257 + return StrictMath.exp(a); // default impl. delegates to StrictMath
5.258 + }
5.259 +
5.260 + /**
5.261 + * Returns the natural logarithm (base <i>e</i>) of a {@code double}
5.262 + * value. Special cases:
5.263 + * <ul><li>If the argument is NaN or less than zero, then the result
5.264 + * is NaN.
5.265 + * <li>If the argument is positive infinity, then the result is
5.266 + * positive infinity.
5.267 + * <li>If the argument is positive zero or negative zero, then the
5.268 + * result is negative infinity.</ul>
5.269 + *
5.270 + * <p>The computed result must be within 1 ulp of the exact result.
5.271 + * Results must be semi-monotonic.
5.272 + *
5.273 + * @param a a value
5.274 + * @return the value ln {@code a}, the natural logarithm of
5.275 + * {@code a}.
5.276 + */
5.277 + public static double log(double a) {
5.278 + return StrictMath.log(a); // default impl. delegates to StrictMath
5.279 + }
5.280 +
5.281 + /**
5.282 + * Returns the base 10 logarithm of a {@code double} value.
5.283 + * Special cases:
5.284 + *
5.285 + * <ul><li>If the argument is NaN or less than zero, then the result
5.286 + * is NaN.
5.287 + * <li>If the argument is positive infinity, then the result is
5.288 + * positive infinity.
5.289 + * <li>If the argument is positive zero or negative zero, then the
5.290 + * result is negative infinity.
5.291 + * <li> If the argument is equal to 10<sup><i>n</i></sup> for
5.292 + * integer <i>n</i>, then the result is <i>n</i>.
5.293 + * </ul>
5.294 + *
5.295 + * <p>The computed result must be within 1 ulp of the exact result.
5.296 + * Results must be semi-monotonic.
5.297 + *
5.298 + * @param a a value
5.299 + * @return the base 10 logarithm of {@code a}.
5.300 + * @since 1.5
5.301 + */
5.302 + public static double log10(double a) {
5.303 + return StrictMath.log10(a); // default impl. delegates to StrictMath
5.304 + }
5.305 +
5.306 + /**
5.307 + * Returns the correctly rounded positive square root of a
5.308 + * {@code double} value.
5.309 + * Special cases:
5.310 + * <ul><li>If the argument is NaN or less than zero, then the result
5.311 + * is NaN.
5.312 + * <li>If the argument is positive infinity, then the result is positive
5.313 + * infinity.
5.314 + * <li>If the argument is positive zero or negative zero, then the
5.315 + * result is the same as the argument.</ul>
5.316 + * Otherwise, the result is the {@code double} value closest to
5.317 + * the true mathematical square root of the argument value.
5.318 + *
5.319 + * @param a a value.
5.320 + * @return the positive square root of {@code a}.
5.321 + * If the argument is NaN or less than zero, the result is NaN.
5.322 + */
5.323 + public static double sqrt(double a) {
5.324 + return StrictMath.sqrt(a); // default impl. delegates to StrictMath
5.325 + // Note that hardware sqrt instructions
5.326 + // frequently can be directly used by JITs
5.327 + // and should be much faster than doing
5.328 + // Math.sqrt in software.
5.329 + }
5.330 +
5.331 +
5.332 + /**
5.333 + * Returns the cube root of a {@code double} value. For
5.334 + * positive finite {@code x}, {@code cbrt(-x) ==
5.335 + * -cbrt(x)}; that is, the cube root of a negative value is
5.336 + * the negative of the cube root of that value's magnitude.
5.337 + *
5.338 + * Special cases:
5.339 + *
5.340 + * <ul>
5.341 + *
5.342 + * <li>If the argument is NaN, then the result is NaN.
5.343 + *
5.344 + * <li>If the argument is infinite, then the result is an infinity
5.345 + * with the same sign as the argument.
5.346 + *
5.347 + * <li>If the argument is zero, then the result is a zero with the
5.348 + * same sign as the argument.
5.349 + *
5.350 + * </ul>
5.351 + *
5.352 + * <p>The computed result must be within 1 ulp of the exact result.
5.353 + *
5.354 + * @param a a value.
5.355 + * @return the cube root of {@code a}.
5.356 + * @since 1.5
5.357 + */
5.358 + public static double cbrt(double a) {
5.359 + return StrictMath.cbrt(a);
5.360 + }
5.361 +
5.362 + /**
5.363 + * Computes the remainder operation on two arguments as prescribed
5.364 + * by the IEEE 754 standard.
5.365 + * The remainder value is mathematically equal to
5.366 + * <code>f1 - f2</code> × <i>n</i>,
5.367 + * where <i>n</i> is the mathematical integer closest to the exact
5.368 + * mathematical value of the quotient {@code f1/f2}, and if two
5.369 + * mathematical integers are equally close to {@code f1/f2},
5.370 + * then <i>n</i> is the integer that is even. If the remainder is
5.371 + * zero, its sign is the same as the sign of the first argument.
5.372 + * Special cases:
5.373 + * <ul><li>If either argument is NaN, or the first argument is infinite,
5.374 + * or the second argument is positive zero or negative zero, then the
5.375 + * result is NaN.
5.376 + * <li>If the first argument is finite and the second argument is
5.377 + * infinite, then the result is the same as the first argument.</ul>
5.378 + *
5.379 + * @param f1 the dividend.
5.380 + * @param f2 the divisor.
5.381 + * @return the remainder when {@code f1} is divided by
5.382 + * {@code f2}.
5.383 + */
5.384 + public static double IEEEremainder(double f1, double f2) {
5.385 + return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
5.386 + }
5.387 +
5.388 + /**
5.389 + * Returns the smallest (closest to negative infinity)
5.390 + * {@code double} value that is greater than or equal to the
5.391 + * argument and is equal to a mathematical integer. Special cases:
5.392 + * <ul><li>If the argument value is already equal to a
5.393 + * mathematical integer, then the result is the same as the
5.394 + * argument. <li>If the argument is NaN or an infinity or
5.395 + * positive zero or negative zero, then the result is the same as
5.396 + * the argument. <li>If the argument value is less than zero but
5.397 + * greater than -1.0, then the result is negative zero.</ul> Note
5.398 + * that the value of {@code Math.ceil(x)} is exactly the
5.399 + * value of {@code -Math.floor(-x)}.
5.400 + *
5.401 + *
5.402 + * @param a a value.
5.403 + * @return the smallest (closest to negative infinity)
5.404 + * floating-point value that is greater than or equal to
5.405 + * the argument and is equal to a mathematical integer.
5.406 + */
5.407 + public static double ceil(double a) {
5.408 + return StrictMath.ceil(a); // default impl. delegates to StrictMath
5.409 + }
5.410 +
5.411 + /**
5.412 + * Returns the largest (closest to positive infinity)
5.413 + * {@code double} value that is less than or equal to the
5.414 + * argument and is equal to a mathematical integer. Special cases:
5.415 + * <ul><li>If the argument value is already equal to a
5.416 + * mathematical integer, then the result is the same as the
5.417 + * argument. <li>If the argument is NaN or an infinity or
5.418 + * positive zero or negative zero, then the result is the same as
5.419 + * the argument.</ul>
5.420 + *
5.421 + * @param a a value.
5.422 + * @return the largest (closest to positive infinity)
5.423 + * floating-point value that less than or equal to the argument
5.424 + * and is equal to a mathematical integer.
5.425 + */
5.426 + public static double floor(double a) {
5.427 + return StrictMath.floor(a); // default impl. delegates to StrictMath
5.428 + }
5.429 +
5.430 + /**
5.431 + * Returns the {@code double} value that is closest in value
5.432 + * to the argument and is equal to a mathematical integer. If two
5.433 + * {@code double} values that are mathematical integers are
5.434 + * equally close, the result is the integer value that is
5.435 + * even. Special cases:
5.436 + * <ul><li>If the argument value is already equal to a mathematical
5.437 + * integer, then the result is the same as the argument.
5.438 + * <li>If the argument is NaN or an infinity or positive zero or negative
5.439 + * zero, then the result is the same as the argument.</ul>
5.440 + *
5.441 + * @param a a {@code double} value.
5.442 + * @return the closest floating-point value to {@code a} that is
5.443 + * equal to a mathematical integer.
5.444 + */
5.445 + public static double rint(double a) {
5.446 + return StrictMath.rint(a); // default impl. delegates to StrictMath
5.447 + }
5.448 +
5.449 + /**
5.450 + * Returns the angle <i>theta</i> from the conversion of rectangular
5.451 + * coordinates ({@code x}, {@code y}) to polar
5.452 + * coordinates (r, <i>theta</i>).
5.453 + * This method computes the phase <i>theta</i> by computing an arc tangent
5.454 + * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
5.455 + * cases:
5.456 + * <ul><li>If either argument is NaN, then the result is NaN.
5.457 + * <li>If the first argument is positive zero and the second argument
5.458 + * is positive, or the first argument is positive and finite and the
5.459 + * second argument is positive infinity, then the result is positive
5.460 + * zero.
5.461 + * <li>If the first argument is negative zero and the second argument
5.462 + * is positive, or the first argument is negative and finite and the
5.463 + * second argument is positive infinity, then the result is negative zero.
5.464 + * <li>If the first argument is positive zero and the second argument
5.465 + * is negative, or the first argument is positive and finite and the
5.466 + * second argument is negative infinity, then the result is the
5.467 + * {@code double} value closest to <i>pi</i>.
5.468 + * <li>If the first argument is negative zero and the second argument
5.469 + * is negative, or the first argument is negative and finite and the
5.470 + * second argument is negative infinity, then the result is the
5.471 + * {@code double} value closest to -<i>pi</i>.
5.472 + * <li>If the first argument is positive and the second argument is
5.473 + * positive zero or negative zero, or the first argument is positive
5.474 + * infinity and the second argument is finite, then the result is the
5.475 + * {@code double} value closest to <i>pi</i>/2.
5.476 + * <li>If the first argument is negative and the second argument is
5.477 + * positive zero or negative zero, or the first argument is negative
5.478 + * infinity and the second argument is finite, then the result is the
5.479 + * {@code double} value closest to -<i>pi</i>/2.
5.480 + * <li>If both arguments are positive infinity, then the result is the
5.481 + * {@code double} value closest to <i>pi</i>/4.
5.482 + * <li>If the first argument is positive infinity and the second argument
5.483 + * is negative infinity, then the result is the {@code double}
5.484 + * value closest to 3*<i>pi</i>/4.
5.485 + * <li>If the first argument is negative infinity and the second argument
5.486 + * is positive infinity, then the result is the {@code double} value
5.487 + * closest to -<i>pi</i>/4.
5.488 + * <li>If both arguments are negative infinity, then the result is the
5.489 + * {@code double} value closest to -3*<i>pi</i>/4.</ul>
5.490 + *
5.491 + * <p>The computed result must be within 2 ulps of the exact result.
5.492 + * Results must be semi-monotonic.
5.493 + *
5.494 + * @param y the ordinate coordinate
5.495 + * @param x the abscissa coordinate
5.496 + * @return the <i>theta</i> component of the point
5.497 + * (<i>r</i>, <i>theta</i>)
5.498 + * in polar coordinates that corresponds to the point
5.499 + * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
5.500 + */
5.501 + public static double atan2(double y, double x) {
5.502 + return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
5.503 + }
5.504 +
5.505 + /**
5.506 + * Returns the value of the first argument raised to the power of the
5.507 + * second argument. Special cases:
5.508 + *
5.509 + * <ul><li>If the second argument is positive or negative zero, then the
5.510 + * result is 1.0.
5.511 + * <li>If the second argument is 1.0, then the result is the same as the
5.512 + * first argument.
5.513 + * <li>If the second argument is NaN, then the result is NaN.
5.514 + * <li>If the first argument is NaN and the second argument is nonzero,
5.515 + * then the result is NaN.
5.516 + *
5.517 + * <li>If
5.518 + * <ul>
5.519 + * <li>the absolute value of the first argument is greater than 1
5.520 + * and the second argument is positive infinity, or
5.521 + * <li>the absolute value of the first argument is less than 1 and
5.522 + * the second argument is negative infinity,
5.523 + * </ul>
5.524 + * then the result is positive infinity.
5.525 + *
5.526 + * <li>If
5.527 + * <ul>
5.528 + * <li>the absolute value of the first argument is greater than 1 and
5.529 + * the second argument is negative infinity, or
5.530 + * <li>the absolute value of the
5.531 + * first argument is less than 1 and the second argument is positive
5.532 + * infinity,
5.533 + * </ul>
5.534 + * then the result is positive zero.
5.535 + *
5.536 + * <li>If the absolute value of the first argument equals 1 and the
5.537 + * second argument is infinite, then the result is NaN.
5.538 + *
5.539 + * <li>If
5.540 + * <ul>
5.541 + * <li>the first argument is positive zero and the second argument
5.542 + * is greater than zero, or
5.543 + * <li>the first argument is positive infinity and the second
5.544 + * argument is less than zero,
5.545 + * </ul>
5.546 + * then the result is positive zero.
5.547 + *
5.548 + * <li>If
5.549 + * <ul>
5.550 + * <li>the first argument is positive zero and the second argument
5.551 + * is less than zero, or
5.552 + * <li>the first argument is positive infinity and the second
5.553 + * argument is greater than zero,
5.554 + * </ul>
5.555 + * then the result is positive infinity.
5.556 + *
5.557 + * <li>If
5.558 + * <ul>
5.559 + * <li>the first argument is negative zero and the second argument
5.560 + * is greater than zero but not a finite odd integer, or
5.561 + * <li>the first argument is negative infinity and the second
5.562 + * argument is less than zero but not a finite odd integer,
5.563 + * </ul>
5.564 + * then the result is positive zero.
5.565 + *
5.566 + * <li>If
5.567 + * <ul>
5.568 + * <li>the first argument is negative zero and the second argument
5.569 + * is a positive finite odd integer, or
5.570 + * <li>the first argument is negative infinity and the second
5.571 + * argument is a negative finite odd integer,
5.572 + * </ul>
5.573 + * then the result is negative zero.
5.574 + *
5.575 + * <li>If
5.576 + * <ul>
5.577 + * <li>the first argument is negative zero and the second argument
5.578 + * is less than zero but not a finite odd integer, or
5.579 + * <li>the first argument is negative infinity and the second
5.580 + * argument is greater than zero but not a finite odd integer,
5.581 + * </ul>
5.582 + * then the result is positive infinity.
5.583 + *
5.584 + * <li>If
5.585 + * <ul>
5.586 + * <li>the first argument is negative zero and the second argument
5.587 + * is a negative finite odd integer, or
5.588 + * <li>the first argument is negative infinity and the second
5.589 + * argument is a positive finite odd integer,
5.590 + * </ul>
5.591 + * then the result is negative infinity.
5.592 + *
5.593 + * <li>If the first argument is finite and less than zero
5.594 + * <ul>
5.595 + * <li> if the second argument is a finite even integer, the
5.596 + * result is equal to the result of raising the absolute value of
5.597 + * the first argument to the power of the second argument
5.598 + *
5.599 + * <li>if the second argument is a finite odd integer, the result
5.600 + * is equal to the negative of the result of raising the absolute
5.601 + * value of the first argument to the power of the second
5.602 + * argument
5.603 + *
5.604 + * <li>if the second argument is finite and not an integer, then
5.605 + * the result is NaN.
5.606 + * </ul>
5.607 + *
5.608 + * <li>If both arguments are integers, then the result is exactly equal
5.609 + * to the mathematical result of raising the first argument to the power
5.610 + * of the second argument if that result can in fact be represented
5.611 + * exactly as a {@code double} value.</ul>
5.612 + *
5.613 + * <p>(In the foregoing descriptions, a floating-point value is
5.614 + * considered to be an integer if and only if it is finite and a
5.615 + * fixed point of the method {@link #ceil ceil} or,
5.616 + * equivalently, a fixed point of the method {@link #floor
5.617 + * floor}. A value is a fixed point of a one-argument
5.618 + * method if and only if the result of applying the method to the
5.619 + * value is equal to the value.)
5.620 + *
5.621 + * <p>The computed result must be within 1 ulp of the exact result.
5.622 + * Results must be semi-monotonic.
5.623 + *
5.624 + * @param a the base.
5.625 + * @param b the exponent.
5.626 + * @return the value {@code a}<sup>{@code b}</sup>.
5.627 + */
5.628 + public static double pow(double a, double b) {
5.629 + return StrictMath.pow(a, b); // default impl. delegates to StrictMath
5.630 + }
5.631 +
5.632 + /**
5.633 + * Returns the closest {@code int} to the argument, with ties
5.634 + * rounding up.
5.635 + *
5.636 + * <p>
5.637 + * Special cases:
5.638 + * <ul><li>If the argument is NaN, the result is 0.
5.639 + * <li>If the argument is negative infinity or any value less than or
5.640 + * equal to the value of {@code Integer.MIN_VALUE}, the result is
5.641 + * equal to the value of {@code Integer.MIN_VALUE}.
5.642 + * <li>If the argument is positive infinity or any value greater than or
5.643 + * equal to the value of {@code Integer.MAX_VALUE}, the result is
5.644 + * equal to the value of {@code Integer.MAX_VALUE}.</ul>
5.645 + *
5.646 + * @param a a floating-point value to be rounded to an integer.
5.647 + * @return the value of the argument rounded to the nearest
5.648 + * {@code int} value.
5.649 + * @see java.lang.Integer#MAX_VALUE
5.650 + * @see java.lang.Integer#MIN_VALUE
5.651 + */
5.652 + public static int round(float a) {
5.653 + if (a != 0x1.fffffep-2f) // greatest float value less than 0.5
5.654 + return (int)floor(a + 0.5f);
5.655 + else
5.656 + return 0;
5.657 + }
5.658 +
5.659 + /**
5.660 + * Returns the closest {@code long} to the argument, with ties
5.661 + * rounding up.
5.662 + *
5.663 + * <p>Special cases:
5.664 + * <ul><li>If the argument is NaN, the result is 0.
5.665 + * <li>If the argument is negative infinity or any value less than or
5.666 + * equal to the value of {@code Long.MIN_VALUE}, the result is
5.667 + * equal to the value of {@code Long.MIN_VALUE}.
5.668 + * <li>If the argument is positive infinity or any value greater than or
5.669 + * equal to the value of {@code Long.MAX_VALUE}, the result is
5.670 + * equal to the value of {@code Long.MAX_VALUE}.</ul>
5.671 + *
5.672 + * @param a a floating-point value to be rounded to a
5.673 + * {@code long}.
5.674 + * @return the value of the argument rounded to the nearest
5.675 + * {@code long} value.
5.676 + * @see java.lang.Long#MAX_VALUE
5.677 + * @see java.lang.Long#MIN_VALUE
5.678 + */
5.679 + public static long round(double a) {
5.680 + if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
5.681 + return (long)floor(a + 0.5d);
5.682 + else
5.683 + return 0;
5.684 + }
5.685 +
5.686 + private static Random randomNumberGenerator;
5.687 +
5.688 + private static synchronized Random initRNG() {
5.689 + Random rnd = randomNumberGenerator;
5.690 + return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
5.691 + }
5.692 +
5.693 + /**
5.694 + * Returns a {@code double} value with a positive sign, greater
5.695 + * than or equal to {@code 0.0} and less than {@code 1.0}.
5.696 + * Returned values are chosen pseudorandomly with (approximately)
5.697 + * uniform distribution from that range.
5.698 + *
5.699 + * <p>When this method is first called, it creates a single new
5.700 + * pseudorandom-number generator, exactly as if by the expression
5.701 + *
5.702 + * <blockquote>{@code new java.util.Random()}</blockquote>
5.703 + *
5.704 + * This new pseudorandom-number generator is used thereafter for
5.705 + * all calls to this method and is used nowhere else.
5.706 + *
5.707 + * <p>This method is properly synchronized to allow correct use by
5.708 + * more than one thread. However, if many threads need to generate
5.709 + * pseudorandom numbers at a great rate, it may reduce contention
5.710 + * for each thread to have its own pseudorandom-number generator.
5.711 + *
5.712 + * @return a pseudorandom {@code double} greater than or equal
5.713 + * to {@code 0.0} and less than {@code 1.0}.
5.714 + * @see Random#nextDouble()
5.715 + */
5.716 + public static double random() {
5.717 + Random rnd = randomNumberGenerator;
5.718 + if (rnd == null) rnd = initRNG();
5.719 + return rnd.nextDouble();
5.720 + }
5.721 +
5.722 + /**
5.723 + * Returns the absolute value of an {@code int} value.
5.724 + * If the argument is not negative, the argument is returned.
5.725 + * If the argument is negative, the negation of the argument is returned.
5.726 + *
5.727 + * <p>Note that if the argument is equal to the value of
5.728 + * {@link Integer#MIN_VALUE}, the most negative representable
5.729 + * {@code int} value, the result is that same value, which is
5.730 + * negative.
5.731 + *
5.732 + * @param a the argument whose absolute value is to be determined
5.733 + * @return the absolute value of the argument.
5.734 + */
5.735 + public static int abs(int a) {
5.736 + return (a < 0) ? -a : a;
5.737 + }
5.738 +
5.739 + /**
5.740 + * Returns the absolute value of a {@code long} value.
5.741 + * If the argument is not negative, the argument is returned.
5.742 + * If the argument is negative, the negation of the argument is returned.
5.743 + *
5.744 + * <p>Note that if the argument is equal to the value of
5.745 + * {@link Long#MIN_VALUE}, the most negative representable
5.746 + * {@code long} value, the result is that same value, which
5.747 + * is negative.
5.748 + *
5.749 + * @param a the argument whose absolute value is to be determined
5.750 + * @return the absolute value of the argument.
5.751 + */
5.752 + public static long abs(long a) {
5.753 + return (a < 0) ? -a : a;
5.754 + }
5.755 +
5.756 + /**
5.757 + * Returns the absolute value of a {@code float} value.
5.758 + * If the argument is not negative, the argument is returned.
5.759 + * If the argument is negative, the negation of the argument is returned.
5.760 + * Special cases:
5.761 + * <ul><li>If the argument is positive zero or negative zero, the
5.762 + * result is positive zero.
5.763 + * <li>If the argument is infinite, the result is positive infinity.
5.764 + * <li>If the argument is NaN, the result is NaN.</ul>
5.765 + * In other words, the result is the same as the value of the expression:
5.766 + * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
5.767 + *
5.768 + * @param a the argument whose absolute value is to be determined
5.769 + * @return the absolute value of the argument.
5.770 + */
5.771 + public static float abs(float a) {
5.772 + return (a <= 0.0F) ? 0.0F - a : a;
5.773 + }
5.774 +
5.775 + /**
5.776 + * Returns the absolute value of a {@code double} value.
5.777 + * If the argument is not negative, the argument is returned.
5.778 + * If the argument is negative, the negation of the argument is returned.
5.779 + * Special cases:
5.780 + * <ul><li>If the argument is positive zero or negative zero, the result
5.781 + * is positive zero.
5.782 + * <li>If the argument is infinite, the result is positive infinity.
5.783 + * <li>If the argument is NaN, the result is NaN.</ul>
5.784 + * In other words, the result is the same as the value of the expression:
5.785 + * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
5.786 + *
5.787 + * @param a the argument whose absolute value is to be determined
5.788 + * @return the absolute value of the argument.
5.789 + */
5.790 + public static double abs(double a) {
5.791 + return (a <= 0.0D) ? 0.0D - a : a;
5.792 + }
5.793 +
5.794 + /**
5.795 + * Returns the greater of two {@code int} values. That is, the
5.796 + * result is the argument closer to the value of
5.797 + * {@link Integer#MAX_VALUE}. If the arguments have the same value,
5.798 + * the result is that same value.
5.799 + *
5.800 + * @param a an argument.
5.801 + * @param b another argument.
5.802 + * @return the larger of {@code a} and {@code b}.
5.803 + */
5.804 + public static int max(int a, int b) {
5.805 + return (a >= b) ? a : b;
5.806 + }
5.807 +
5.808 + /**
5.809 + * Returns the greater of two {@code long} values. That is, the
5.810 + * result is the argument closer to the value of
5.811 + * {@link Long#MAX_VALUE}. If the arguments have the same value,
5.812 + * the result is that same value.
5.813 + *
5.814 + * @param a an argument.
5.815 + * @param b another argument.
5.816 + * @return the larger of {@code a} and {@code b}.
5.817 + */
5.818 + public static long max(long a, long b) {
5.819 + return (a >= b) ? a : b;
5.820 + }
5.821 +
5.822 + private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
5.823 + private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);
5.824 +
5.825 + /**
5.826 + * Returns the greater of two {@code float} values. That is,
5.827 + * the result is the argument closer to positive infinity. If the
5.828 + * arguments have the same value, the result is that same
5.829 + * value. If either value is NaN, then the result is NaN. Unlike
5.830 + * the numerical comparison operators, this method considers
5.831 + * negative zero to be strictly smaller than positive zero. If one
5.832 + * argument is positive zero and the other negative zero, the
5.833 + * result is positive zero.
5.834 + *
5.835 + * @param a an argument.
5.836 + * @param b another argument.
5.837 + * @return the larger of {@code a} and {@code b}.
5.838 + */
5.839 + public static float max(float a, float b) {
5.840 + if (a != a) return a; // a is NaN
5.841 + if ((a == 0.0f) && (b == 0.0f)
5.842 + && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
5.843 + return b;
5.844 + }
5.845 + return (a >= b) ? a : b;
5.846 + }
5.847 +
5.848 + /**
5.849 + * Returns the greater of two {@code double} values. That
5.850 + * is, the result is the argument closer to positive infinity. If
5.851 + * the arguments have the same value, the result is that same
5.852 + * value. If either value is NaN, then the result is NaN. Unlike
5.853 + * the numerical comparison operators, this method considers
5.854 + * negative zero to be strictly smaller than positive zero. If one
5.855 + * argument is positive zero and the other negative zero, the
5.856 + * result is positive zero.
5.857 + *
5.858 + * @param a an argument.
5.859 + * @param b another argument.
5.860 + * @return the larger of {@code a} and {@code b}.
5.861 + */
5.862 + public static double max(double a, double b) {
5.863 + if (a != a) return a; // a is NaN
5.864 + if ((a == 0.0d) && (b == 0.0d)
5.865 + && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
5.866 + return b;
5.867 + }
5.868 + return (a >= b) ? a : b;
5.869 + }
5.870 +
5.871 + /**
5.872 + * Returns the smaller of two {@code int} values. That is,
5.873 + * the result the argument closer to the value of
5.874 + * {@link Integer#MIN_VALUE}. If the arguments have the same
5.875 + * value, the result is that same value.
5.876 + *
5.877 + * @param a an argument.
5.878 + * @param b another argument.
5.879 + * @return the smaller of {@code a} and {@code b}.
5.880 + */
5.881 + public static int min(int a, int b) {
5.882 + return (a <= b) ? a : b;
5.883 + }
5.884 +
5.885 + /**
5.886 + * Returns the smaller of two {@code long} values. That is,
5.887 + * the result is the argument closer to the value of
5.888 + * {@link Long#MIN_VALUE}. If the arguments have the same
5.889 + * value, the result is that same value.
5.890 + *
5.891 + * @param a an argument.
5.892 + * @param b another argument.
5.893 + * @return the smaller of {@code a} and {@code b}.
5.894 + */
5.895 + public static long min(long a, long b) {
5.896 + return (a <= b) ? a : b;
5.897 + }
5.898 +
5.899 + /**
5.900 + * Returns the smaller of two {@code float} values. That is,
5.901 + * the result is the value closer to negative infinity. If the
5.902 + * arguments have the same value, the result is that same
5.903 + * value. If either value is NaN, then the result is NaN. Unlike
5.904 + * the numerical comparison operators, this method considers
5.905 + * negative zero to be strictly smaller than positive zero. If
5.906 + * one argument is positive zero and the other is negative zero,
5.907 + * the result is negative zero.
5.908 + *
5.909 + * @param a an argument.
5.910 + * @param b another argument.
5.911 + * @return the smaller of {@code a} and {@code b}.
5.912 + */
5.913 + public static float min(float a, float b) {
5.914 + if (a != a) return a; // a is NaN
5.915 + if ((a == 0.0f) && (b == 0.0f)
5.916 + && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
5.917 + return b;
5.918 + }
5.919 + return (a <= b) ? a : b;
5.920 + }
5.921 +
5.922 + /**
5.923 + * Returns the smaller of two {@code double} values. That
5.924 + * is, the result is the value closer to negative infinity. If the
5.925 + * arguments have the same value, the result is that same
5.926 + * value. If either value is NaN, then the result is NaN. Unlike
5.927 + * the numerical comparison operators, this method considers
5.928 + * negative zero to be strictly smaller than positive zero. If one
5.929 + * argument is positive zero and the other is negative zero, the
5.930 + * result is negative zero.
5.931 + *
5.932 + * @param a an argument.
5.933 + * @param b another argument.
5.934 + * @return the smaller of {@code a} and {@code b}.
5.935 + */
5.936 + public static double min(double a, double b) {
5.937 + if (a != a) return a; // a is NaN
5.938 + if ((a == 0.0d) && (b == 0.0d)
5.939 + && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
5.940 + return b;
5.941 + }
5.942 + return (a <= b) ? a : b;
5.943 + }
5.944 +
5.945 + /**
5.946 + * Returns the size of an ulp of the argument. An ulp of a
5.947 + * {@code double} value is the positive distance between this
5.948 + * floating-point value and the {@code double} value next
5.949 + * larger in magnitude. Note that for non-NaN <i>x</i>,
5.950 + * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
5.951 + *
5.952 + * <p>Special Cases:
5.953 + * <ul>
5.954 + * <li> If the argument is NaN, then the result is NaN.
5.955 + * <li> If the argument is positive or negative infinity, then the
5.956 + * result is positive infinity.
5.957 + * <li> If the argument is positive or negative zero, then the result is
5.958 + * {@code Double.MIN_VALUE}.
5.959 + * <li> If the argument is ±{@code Double.MAX_VALUE}, then
5.960 + * the result is equal to 2<sup>971</sup>.
5.961 + * </ul>
5.962 + *
5.963 + * @param d the floating-point value whose ulp is to be returned
5.964 + * @return the size of an ulp of the argument
5.965 + * @author Joseph D. Darcy
5.966 + * @since 1.5
5.967 + */
5.968 + public static double ulp(double d) {
5.969 + return sun.misc.FpUtils.ulp(d);
5.970 + }
5.971 +
5.972 + /**
5.973 + * Returns the size of an ulp of the argument. An ulp of a
5.974 + * {@code float} value is the positive distance between this
5.975 + * floating-point value and the {@code float} value next
5.976 + * larger in magnitude. Note that for non-NaN <i>x</i>,
5.977 + * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
5.978 + *
5.979 + * <p>Special Cases:
5.980 + * <ul>
5.981 + * <li> If the argument is NaN, then the result is NaN.
5.982 + * <li> If the argument is positive or negative infinity, then the
5.983 + * result is positive infinity.
5.984 + * <li> If the argument is positive or negative zero, then the result is
5.985 + * {@code Float.MIN_VALUE}.
5.986 + * <li> If the argument is ±{@code Float.MAX_VALUE}, then
5.987 + * the result is equal to 2<sup>104</sup>.
5.988 + * </ul>
5.989 + *
5.990 + * @param f the floating-point value whose ulp is to be returned
5.991 + * @return the size of an ulp of the argument
5.992 + * @author Joseph D. Darcy
5.993 + * @since 1.5
5.994 + */
5.995 + public static float ulp(float f) {
5.996 + return sun.misc.FpUtils.ulp(f);
5.997 + }
5.998 +
5.999 + /**
5.1000 + * Returns the signum function of the argument; zero if the argument
5.1001 + * is zero, 1.0 if the argument is greater than zero, -1.0 if the
5.1002 + * argument is less than zero.
5.1003 + *
5.1004 + * <p>Special Cases:
5.1005 + * <ul>
5.1006 + * <li> If the argument is NaN, then the result is NaN.
5.1007 + * <li> If the argument is positive zero or negative zero, then the
5.1008 + * result is the same as the argument.
5.1009 + * </ul>
5.1010 + *
5.1011 + * @param d the floating-point value whose signum is to be returned
5.1012 + * @return the signum function of the argument
5.1013 + * @author Joseph D. Darcy
5.1014 + * @since 1.5
5.1015 + */
5.1016 + public static double signum(double d) {
5.1017 + return sun.misc.FpUtils.signum(d);
5.1018 + }
5.1019 +
5.1020 + /**
5.1021 + * Returns the signum function of the argument; zero if the argument
5.1022 + * is zero, 1.0f if the argument is greater than zero, -1.0f if the
5.1023 + * argument is less than zero.
5.1024 + *
5.1025 + * <p>Special Cases:
5.1026 + * <ul>
5.1027 + * <li> If the argument is NaN, then the result is NaN.
5.1028 + * <li> If the argument is positive zero or negative zero, then the
5.1029 + * result is the same as the argument.
5.1030 + * </ul>
5.1031 + *
5.1032 + * @param f the floating-point value whose signum is to be returned
5.1033 + * @return the signum function of the argument
5.1034 + * @author Joseph D. Darcy
5.1035 + * @since 1.5
5.1036 + */
5.1037 + public static float signum(float f) {
5.1038 + return sun.misc.FpUtils.signum(f);
5.1039 + }
5.1040 +
5.1041 + /**
5.1042 + * Returns the hyperbolic sine of a {@code double} value.
5.1043 + * The hyperbolic sine of <i>x</i> is defined to be
5.1044 + * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2
5.1045 + * where <i>e</i> is {@linkplain Math#E Euler's number}.
5.1046 + *
5.1047 + * <p>Special cases:
5.1048 + * <ul>
5.1049 + *
5.1050 + * <li>If the argument is NaN, then the result is NaN.
5.1051 + *
5.1052 + * <li>If the argument is infinite, then the result is an infinity
5.1053 + * with the same sign as the argument.
5.1054 + *
5.1055 + * <li>If the argument is zero, then the result is a zero with the
5.1056 + * same sign as the argument.
5.1057 + *
5.1058 + * </ul>
5.1059 + *
5.1060 + * <p>The computed result must be within 2.5 ulps of the exact result.
5.1061 + *
5.1062 + * @param x The number whose hyperbolic sine is to be returned.
5.1063 + * @return The hyperbolic sine of {@code x}.
5.1064 + * @since 1.5
5.1065 + */
5.1066 + public static double sinh(double x) {
5.1067 + return StrictMath.sinh(x);
5.1068 + }
5.1069 +
5.1070 + /**
5.1071 + * Returns the hyperbolic cosine of a {@code double} value.
5.1072 + * The hyperbolic cosine of <i>x</i> is defined to be
5.1073 + * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2
5.1074 + * where <i>e</i> is {@linkplain Math#E Euler's number}.
5.1075 + *
5.1076 + * <p>Special cases:
5.1077 + * <ul>
5.1078 + *
5.1079 + * <li>If the argument is NaN, then the result is NaN.
5.1080 + *
5.1081 + * <li>If the argument is infinite, then the result is positive
5.1082 + * infinity.
5.1083 + *
5.1084 + * <li>If the argument is zero, then the result is {@code 1.0}.
5.1085 + *
5.1086 + * </ul>
5.1087 + *
5.1088 + * <p>The computed result must be within 2.5 ulps of the exact result.
5.1089 + *
5.1090 + * @param x The number whose hyperbolic cosine is to be returned.
5.1091 + * @return The hyperbolic cosine of {@code x}.
5.1092 + * @since 1.5
5.1093 + */
5.1094 + public static double cosh(double x) {
5.1095 + return StrictMath.cosh(x);
5.1096 + }
5.1097 +
5.1098 + /**
5.1099 + * Returns the hyperbolic tangent of a {@code double} value.
5.1100 + * The hyperbolic tangent of <i>x</i> is defined to be
5.1101 + * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>),
5.1102 + * in other words, {@linkplain Math#sinh
5.1103 + * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note
5.1104 + * that the absolute value of the exact tanh is always less than
5.1105 + * 1.
5.1106 + *
5.1107 + * <p>Special cases:
5.1108 + * <ul>
5.1109 + *
5.1110 + * <li>If the argument is NaN, then the result is NaN.
5.1111 + *
5.1112 + * <li>If the argument is zero, then the result is a zero with the
5.1113 + * same sign as the argument.
5.1114 + *
5.1115 + * <li>If the argument is positive infinity, then the result is
5.1116 + * {@code +1.0}.
5.1117 + *
5.1118 + * <li>If the argument is negative infinity, then the result is
5.1119 + * {@code -1.0}.
5.1120 + *
5.1121 + * </ul>
5.1122 + *
5.1123 + * <p>The computed result must be within 2.5 ulps of the exact result.
5.1124 + * The result of {@code tanh} for any finite input must have
5.1125 + * an absolute value less than or equal to 1. Note that once the
5.1126 + * exact result of tanh is within 1/2 of an ulp of the limit value
5.1127 + * of ±1, correctly signed ±{@code 1.0} should
5.1128 + * be returned.
5.1129 + *
5.1130 + * @param x The number whose hyperbolic tangent is to be returned.
5.1131 + * @return The hyperbolic tangent of {@code x}.
5.1132 + * @since 1.5
5.1133 + */
5.1134 + public static double tanh(double x) {
5.1135 + return StrictMath.tanh(x);
5.1136 + }
5.1137 +
5.1138 + /**
5.1139 + * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
5.1140 + * without intermediate overflow or underflow.
5.1141 + *
5.1142 + * <p>Special cases:
5.1143 + * <ul>
5.1144 + *
5.1145 + * <li> If either argument is infinite, then the result
5.1146 + * is positive infinity.
5.1147 + *
5.1148 + * <li> If either argument is NaN and neither argument is infinite,
5.1149 + * then the result is NaN.
5.1150 + *
5.1151 + * </ul>
5.1152 + *
5.1153 + * <p>The computed result must be within 1 ulp of the exact
5.1154 + * result. If one parameter is held constant, the results must be
5.1155 + * semi-monotonic in the other parameter.
5.1156 + *
5.1157 + * @param x a value
5.1158 + * @param y a value
5.1159 + * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
5.1160 + * without intermediate overflow or underflow
5.1161 + * @since 1.5
5.1162 + */
5.1163 + public static double hypot(double x, double y) {
5.1164 + return StrictMath.hypot(x, y);
5.1165 + }
5.1166 +
5.1167 + /**
5.1168 + * Returns <i>e</i><sup>x</sup> -1. Note that for values of
5.1169 + * <i>x</i> near 0, the exact sum of
5.1170 + * {@code expm1(x)} + 1 is much closer to the true
5.1171 + * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
5.1172 + *
5.1173 + * <p>Special cases:
5.1174 + * <ul>
5.1175 + * <li>If the argument is NaN, the result is NaN.
5.1176 + *
5.1177 + * <li>If the argument is positive infinity, then the result is
5.1178 + * positive infinity.
5.1179 + *
5.1180 + * <li>If the argument is negative infinity, then the result is
5.1181 + * -1.0.
5.1182 + *
5.1183 + * <li>If the argument is zero, then the result is a zero with the
5.1184 + * same sign as the argument.
5.1185 + *
5.1186 + * </ul>
5.1187 + *
5.1188 + * <p>The computed result must be within 1 ulp of the exact result.
5.1189 + * Results must be semi-monotonic. The result of
5.1190 + * {@code expm1} for any finite input must be greater than or
5.1191 + * equal to {@code -1.0}. Note that once the exact result of
5.1192 + * <i>e</i><sup>{@code x}</sup> - 1 is within 1/2
5.1193 + * ulp of the limit value -1, {@code -1.0} should be
5.1194 + * returned.
5.1195 + *
5.1196 + * @param x the exponent to raise <i>e</i> to in the computation of
5.1197 + * <i>e</i><sup>{@code x}</sup> -1.
5.1198 + * @return the value <i>e</i><sup>{@code x}</sup> - 1.
5.1199 + * @since 1.5
5.1200 + */
5.1201 + public static double expm1(double x) {
5.1202 + return StrictMath.expm1(x);
5.1203 + }
5.1204 +
5.1205 + /**
5.1206 + * Returns the natural logarithm of the sum of the argument and 1.
5.1207 + * Note that for small values {@code x}, the result of
5.1208 + * {@code log1p(x)} is much closer to the true result of ln(1
5.1209 + * + {@code x}) than the floating-point evaluation of
5.1210 + * {@code log(1.0+x)}.
5.1211 + *
5.1212 + * <p>Special cases:
5.1213 + *
5.1214 + * <ul>
5.1215 + *
5.1216 + * <li>If the argument is NaN or less than -1, then the result is
5.1217 + * NaN.
5.1218 + *
5.1219 + * <li>If the argument is positive infinity, then the result is
5.1220 + * positive infinity.
5.1221 + *
5.1222 + * <li>If the argument is negative one, then the result is
5.1223 + * negative infinity.
5.1224 + *
5.1225 + * <li>If the argument is zero, then the result is a zero with the
5.1226 + * same sign as the argument.
5.1227 + *
5.1228 + * </ul>
5.1229 + *
5.1230 + * <p>The computed result must be within 1 ulp of the exact result.
5.1231 + * Results must be semi-monotonic.
5.1232 + *
5.1233 + * @param x a value
5.1234 + * @return the value ln({@code x} + 1), the natural
5.1235 + * log of {@code x} + 1
5.1236 + * @since 1.5
5.1237 + */
5.1238 + public static double log1p(double x) {
5.1239 + return StrictMath.log1p(x);
5.1240 + }
5.1241 +
5.1242 + /**
5.1243 + * Returns the first floating-point argument with the sign of the
5.1244 + * second floating-point argument. Note that unlike the {@link
5.1245 + * StrictMath#copySign(double, double) StrictMath.copySign}
5.1246 + * method, this method does not require NaN {@code sign}
5.1247 + * arguments to be treated as positive values; implementations are
5.1248 + * permitted to treat some NaN arguments as positive and other NaN
5.1249 + * arguments as negative to allow greater performance.
5.1250 + *
5.1251 + * @param magnitude the parameter providing the magnitude of the result
5.1252 + * @param sign the parameter providing the sign of the result
5.1253 + * @return a value with the magnitude of {@code magnitude}
5.1254 + * and the sign of {@code sign}.
5.1255 + * @since 1.6
5.1256 + */
5.1257 + public static double copySign(double magnitude, double sign) {
5.1258 + return sun.misc.FpUtils.rawCopySign(magnitude, sign);
5.1259 + }
5.1260 +
5.1261 + /**
5.1262 + * Returns the first floating-point argument with the sign of the
5.1263 + * second floating-point argument. Note that unlike the {@link
5.1264 + * StrictMath#copySign(float, float) StrictMath.copySign}
5.1265 + * method, this method does not require NaN {@code sign}
5.1266 + * arguments to be treated as positive values; implementations are
5.1267 + * permitted to treat some NaN arguments as positive and other NaN
5.1268 + * arguments as negative to allow greater performance.
5.1269 + *
5.1270 + * @param magnitude the parameter providing the magnitude of the result
5.1271 + * @param sign the parameter providing the sign of the result
5.1272 + * @return a value with the magnitude of {@code magnitude}
5.1273 + * and the sign of {@code sign}.
5.1274 + * @since 1.6
5.1275 + */
5.1276 + public static float copySign(float magnitude, float sign) {
5.1277 + return sun.misc.FpUtils.rawCopySign(magnitude, sign);
5.1278 + }
5.1279 +
5.1280 + /**
5.1281 + * Returns the unbiased exponent used in the representation of a
5.1282 + * {@code float}. Special cases:
5.1283 + *
5.1284 + * <ul>
5.1285 + * <li>If the argument is NaN or infinite, then the result is
5.1286 + * {@link Float#MAX_EXPONENT} + 1.
5.1287 + * <li>If the argument is zero or subnormal, then the result is
5.1288 + * {@link Float#MIN_EXPONENT} -1.
5.1289 + * </ul>
5.1290 + * @param f a {@code float} value
5.1291 + * @return the unbiased exponent of the argument
5.1292 + * @since 1.6
5.1293 + */
5.1294 + public static int getExponent(float f) {
5.1295 + return sun.misc.FpUtils.getExponent(f);
5.1296 + }
5.1297 +
5.1298 + /**
5.1299 + * Returns the unbiased exponent used in the representation of a
5.1300 + * {@code double}. Special cases:
5.1301 + *
5.1302 + * <ul>
5.1303 + * <li>If the argument is NaN or infinite, then the result is
5.1304 + * {@link Double#MAX_EXPONENT} + 1.
5.1305 + * <li>If the argument is zero or subnormal, then the result is
5.1306 + * {@link Double#MIN_EXPONENT} -1.
5.1307 + * </ul>
5.1308 + * @param d a {@code double} value
5.1309 + * @return the unbiased exponent of the argument
5.1310 + * @since 1.6
5.1311 + */
5.1312 + public static int getExponent(double d) {
5.1313 + return sun.misc.FpUtils.getExponent(d);
5.1314 + }
5.1315 +
5.1316 + /**
5.1317 + * Returns the floating-point number adjacent to the first
5.1318 + * argument in the direction of the second argument. If both
5.1319 + * arguments compare as equal the second argument is returned.
5.1320 + *
5.1321 + * <p>
5.1322 + * Special cases:
5.1323 + * <ul>
5.1324 + * <li> If either argument is a NaN, then NaN is returned.
5.1325 + *
5.1326 + * <li> If both arguments are signed zeros, {@code direction}
5.1327 + * is returned unchanged (as implied by the requirement of
5.1328 + * returning the second argument if the arguments compare as
5.1329 + * equal).
5.1330 + *
5.1331 + * <li> If {@code start} is
5.1332 + * ±{@link Double#MIN_VALUE} and {@code direction}
5.1333 + * has a value such that the result should have a smaller
5.1334 + * magnitude, then a zero with the same sign as {@code start}
5.1335 + * is returned.
5.1336 + *
5.1337 + * <li> If {@code start} is infinite and
5.1338 + * {@code direction} has a value such that the result should
5.1339 + * have a smaller magnitude, {@link Double#MAX_VALUE} with the
5.1340 + * same sign as {@code start} is returned.
5.1341 + *
5.1342 + * <li> If {@code start} is equal to ±
5.1343 + * {@link Double#MAX_VALUE} and {@code direction} has a
5.1344 + * value such that the result should have a larger magnitude, an
5.1345 + * infinity with same sign as {@code start} is returned.
5.1346 + * </ul>
5.1347 + *
5.1348 + * @param start starting floating-point value
5.1349 + * @param direction value indicating which of
5.1350 + * {@code start}'s neighbors or {@code start} should
5.1351 + * be returned
5.1352 + * @return The floating-point number adjacent to {@code start} in the
5.1353 + * direction of {@code direction}.
5.1354 + * @since 1.6
5.1355 + */
5.1356 + public static double nextAfter(double start, double direction) {
5.1357 + return sun.misc.FpUtils.nextAfter(start, direction);
5.1358 + }
5.1359 +
5.1360 + /**
5.1361 + * Returns the floating-point number adjacent to the first
5.1362 + * argument in the direction of the second argument. If both
5.1363 + * arguments compare as equal a value equivalent to the second argument
5.1364 + * is returned.
5.1365 + *
5.1366 + * <p>
5.1367 + * Special cases:
5.1368 + * <ul>
5.1369 + * <li> If either argument is a NaN, then NaN is returned.
5.1370 + *
5.1371 + * <li> If both arguments are signed zeros, a value equivalent
5.1372 + * to {@code direction} is returned.
5.1373 + *
5.1374 + * <li> If {@code start} is
5.1375 + * ±{@link Float#MIN_VALUE} and {@code direction}
5.1376 + * has a value such that the result should have a smaller
5.1377 + * magnitude, then a zero with the same sign as {@code start}
5.1378 + * is returned.
5.1379 + *
5.1380 + * <li> If {@code start} is infinite and
5.1381 + * {@code direction} has a value such that the result should
5.1382 + * have a smaller magnitude, {@link Float#MAX_VALUE} with the
5.1383 + * same sign as {@code start} is returned.
5.1384 + *
5.1385 + * <li> If {@code start} is equal to ±
5.1386 + * {@link Float#MAX_VALUE} and {@code direction} has a
5.1387 + * value such that the result should have a larger magnitude, an
5.1388 + * infinity with same sign as {@code start} is returned.
5.1389 + * </ul>
5.1390 + *
5.1391 + * @param start starting floating-point value
5.1392 + * @param direction value indicating which of
5.1393 + * {@code start}'s neighbors or {@code start} should
5.1394 + * be returned
5.1395 + * @return The floating-point number adjacent to {@code start} in the
5.1396 + * direction of {@code direction}.
5.1397 + * @since 1.6
5.1398 + */
5.1399 + public static float nextAfter(float start, double direction) {
5.1400 + return sun.misc.FpUtils.nextAfter(start, direction);
5.1401 + }
5.1402 +
5.1403 + /**
5.1404 + * Returns the floating-point value adjacent to {@code d} in
5.1405 + * the direction of positive infinity. This method is
5.1406 + * semantically equivalent to {@code nextAfter(d,
5.1407 + * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
5.1408 + * implementation may run faster than its equivalent
5.1409 + * {@code nextAfter} call.
5.1410 + *
5.1411 + * <p>Special Cases:
5.1412 + * <ul>
5.1413 + * <li> If the argument is NaN, the result is NaN.
5.1414 + *
5.1415 + * <li> If the argument is positive infinity, the result is
5.1416 + * positive infinity.
5.1417 + *
5.1418 + * <li> If the argument is zero, the result is
5.1419 + * {@link Double#MIN_VALUE}
5.1420 + *
5.1421 + * </ul>
5.1422 + *
5.1423 + * @param d starting floating-point value
5.1424 + * @return The adjacent floating-point value closer to positive
5.1425 + * infinity.
5.1426 + * @since 1.6
5.1427 + */
5.1428 + public static double nextUp(double d) {
5.1429 + return sun.misc.FpUtils.nextUp(d);
5.1430 + }
5.1431 +
5.1432 + /**
5.1433 + * Returns the floating-point value adjacent to {@code f} in
5.1434 + * the direction of positive infinity. This method is
5.1435 + * semantically equivalent to {@code nextAfter(f,
5.1436 + * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
5.1437 + * implementation may run faster than its equivalent
5.1438 + * {@code nextAfter} call.
5.1439 + *
5.1440 + * <p>Special Cases:
5.1441 + * <ul>
5.1442 + * <li> If the argument is NaN, the result is NaN.
5.1443 + *
5.1444 + * <li> If the argument is positive infinity, the result is
5.1445 + * positive infinity.
5.1446 + *
5.1447 + * <li> If the argument is zero, the result is
5.1448 + * {@link Float#MIN_VALUE}
5.1449 + *
5.1450 + * </ul>
5.1451 + *
5.1452 + * @param f starting floating-point value
5.1453 + * @return The adjacent floating-point value closer to positive
5.1454 + * infinity.
5.1455 + * @since 1.6
5.1456 + */
5.1457 + public static float nextUp(float f) {
5.1458 + return sun.misc.FpUtils.nextUp(f);
5.1459 + }
5.1460 +
5.1461 +
5.1462 + /**
5.1463 + * Return {@code d} ×
5.1464 + * 2<sup>{@code scaleFactor}</sup> rounded as if performed
5.1465 + * by a single correctly rounded floating-point multiply to a
5.1466 + * member of the double value set. See the Java
5.1467 + * Language Specification for a discussion of floating-point
5.1468 + * value sets. If the exponent of the result is between {@link
5.1469 + * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
5.1470 + * answer is calculated exactly. If the exponent of the result
5.1471 + * would be larger than {@code Double.MAX_EXPONENT}, an
5.1472 + * infinity is returned. Note that if the result is subnormal,
5.1473 + * precision may be lost; that is, when {@code scalb(x, n)}
5.1474 + * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
5.1475 + * <i>x</i>. When the result is non-NaN, the result has the same
5.1476 + * sign as {@code d}.
5.1477 + *
5.1478 + * <p>Special cases:
5.1479 + * <ul>
5.1480 + * <li> If the first argument is NaN, NaN is returned.
5.1481 + * <li> If the first argument is infinite, then an infinity of the
5.1482 + * same sign is returned.
5.1483 + * <li> If the first argument is zero, then a zero of the same
5.1484 + * sign is returned.
5.1485 + * </ul>
5.1486 + *
5.1487 + * @param d number to be scaled by a power of two.
5.1488 + * @param scaleFactor power of 2 used to scale {@code d}
5.1489 + * @return {@code d} × 2<sup>{@code scaleFactor}</sup>
5.1490 + * @since 1.6
5.1491 + */
5.1492 + public static double scalb(double d, int scaleFactor) {
5.1493 + return sun.misc.FpUtils.scalb(d, scaleFactor);
5.1494 + }
5.1495 +
5.1496 + /**
5.1497 + * Return {@code f} ×
5.1498 + * 2<sup>{@code scaleFactor}</sup> rounded as if performed
5.1499 + * by a single correctly rounded floating-point multiply to a
5.1500 + * member of the float value set. See the Java
5.1501 + * Language Specification for a discussion of floating-point
5.1502 + * value sets. If the exponent of the result is between {@link
5.1503 + * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
5.1504 + * answer is calculated exactly. If the exponent of the result
5.1505 + * would be larger than {@code Float.MAX_EXPONENT}, an
5.1506 + * infinity is returned. Note that if the result is subnormal,
5.1507 + * precision may be lost; that is, when {@code scalb(x, n)}
5.1508 + * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
5.1509 + * <i>x</i>. When the result is non-NaN, the result has the same
5.1510 + * sign as {@code f}.
5.1511 + *
5.1512 + * <p>Special cases:
5.1513 + * <ul>
5.1514 + * <li> If the first argument is NaN, NaN is returned.
5.1515 + * <li> If the first argument is infinite, then an infinity of the
5.1516 + * same sign is returned.
5.1517 + * <li> If the first argument is zero, then a zero of the same
5.1518 + * sign is returned.
5.1519 + * </ul>
5.1520 + *
5.1521 + * @param f number to be scaled by a power of two.
5.1522 + * @param scaleFactor power of 2 used to scale {@code f}
5.1523 + * @return {@code f} × 2<sup>{@code scaleFactor}</sup>
5.1524 + * @since 1.6
5.1525 + */
5.1526 + public static float scalb(float f, int scaleFactor) {
5.1527 + return sun.misc.FpUtils.scalb(f, scaleFactor);
5.1528 + }
5.1529 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/emul/src/main/java/java/lang/Short.java Sat Sep 29 10:56:23 2012 +0200
6.3 @@ -0,0 +1,468 @@
6.4 +/*
6.5 + * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6.7 + *
6.8 + * This code is free software; you can redistribute it and/or modify it
6.9 + * under the terms of the GNU General Public License version 2 only, as
6.10 + * published by the Free Software Foundation. Oracle designates this
6.11 + * particular file as subject to the "Classpath" exception as provided
6.12 + * by Oracle in the LICENSE file that accompanied this code.
6.13 + *
6.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
6.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6.17 + * version 2 for more details (a copy is included in the LICENSE file that
6.18 + * accompanied this code).
6.19 + *
6.20 + * You should have received a copy of the GNU General Public License version
6.21 + * 2 along with this work; if not, write to the Free Software Foundation,
6.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6.23 + *
6.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6.25 + * or visit www.oracle.com if you need additional information or have any
6.26 + * questions.
6.27 + */
6.28 +
6.29 +package java.lang;
6.30 +
6.31 +/**
6.32 + * The {@code Short} class wraps a value of primitive type {@code
6.33 + * short} in an object. An object of type {@code Short} contains a
6.34 + * single field whose type is {@code short}.
6.35 + *
6.36 + * <p>In addition, this class provides several methods for converting
6.37 + * a {@code short} to a {@code String} and a {@code String} to a
6.38 + * {@code short}, as well as other constants and methods useful when
6.39 + * dealing with a {@code short}.
6.40 + *
6.41 + * @author Nakul Saraiya
6.42 + * @author Joseph D. Darcy
6.43 + * @see java.lang.Number
6.44 + * @since JDK1.1
6.45 + */
6.46 +public final class Short extends Number implements Comparable<Short> {
6.47 +
6.48 + /**
6.49 + * A constant holding the minimum value a {@code short} can
6.50 + * have, -2<sup>15</sup>.
6.51 + */
6.52 + public static final short MIN_VALUE = -32768;
6.53 +
6.54 + /**
6.55 + * A constant holding the maximum value a {@code short} can
6.56 + * have, 2<sup>15</sup>-1.
6.57 + */
6.58 + public static final short MAX_VALUE = 32767;
6.59 +
6.60 + /**
6.61 + * The {@code Class} instance representing the primitive type
6.62 + * {@code short}.
6.63 + */
6.64 + public static final Class<Short> TYPE = (Class<Short>) Class.getPrimitiveClass("short");
6.65 +
6.66 + /**
6.67 + * Returns a new {@code String} object representing the
6.68 + * specified {@code short}. The radix is assumed to be 10.
6.69 + *
6.70 + * @param s the {@code short} to be converted
6.71 + * @return the string representation of the specified {@code short}
6.72 + * @see java.lang.Integer#toString(int)
6.73 + */
6.74 + public static String toString(short s) {
6.75 + return Integer.toString((int)s, 10);
6.76 + }
6.77 +
6.78 + /**
6.79 + * Parses the string argument as a signed {@code short} in the
6.80 + * radix specified by the second argument. The characters in the
6.81 + * string must all be digits, of the specified radix (as
6.82 + * determined by whether {@link java.lang.Character#digit(char,
6.83 + * int)} returns a nonnegative value) except that the first
6.84 + * character may be an ASCII minus sign {@code '-'}
6.85 + * (<code>'\u002D'</code>) to indicate a negative value or an
6.86 + * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
6.87 + * indicate a positive value. The resulting {@code short} value
6.88 + * is returned.
6.89 + *
6.90 + * <p>An exception of type {@code NumberFormatException} is
6.91 + * thrown if any of the following situations occurs:
6.92 + * <ul>
6.93 + * <li> The first argument is {@code null} or is a string of
6.94 + * length zero.
6.95 + *
6.96 + * <li> The radix is either smaller than {@link
6.97 + * java.lang.Character#MIN_RADIX} or larger than {@link
6.98 + * java.lang.Character#MAX_RADIX}.
6.99 + *
6.100 + * <li> Any character of the string is not a digit of the
6.101 + * specified radix, except that the first character may be a minus
6.102 + * sign {@code '-'} (<code>'\u002D'</code>) or plus sign
6.103 + * {@code '+'} (<code>'\u002B'</code>) provided that the
6.104 + * string is longer than length 1.
6.105 + *
6.106 + * <li> The value represented by the string is not a value of type
6.107 + * {@code short}.
6.108 + * </ul>
6.109 + *
6.110 + * @param s the {@code String} containing the
6.111 + * {@code short} representation to be parsed
6.112 + * @param radix the radix to be used while parsing {@code s}
6.113 + * @return the {@code short} represented by the string
6.114 + * argument in the specified radix.
6.115 + * @throws NumberFormatException If the {@code String}
6.116 + * does not contain a parsable {@code short}.
6.117 + */
6.118 + public static short parseShort(String s, int radix)
6.119 + throws NumberFormatException {
6.120 + int i = Integer.parseInt(s, radix);
6.121 + if (i < MIN_VALUE || i > MAX_VALUE)
6.122 + throw new NumberFormatException(
6.123 + "Value out of range. Value:\"" + s + "\" Radix:" + radix);
6.124 + return (short)i;
6.125 + }
6.126 +
6.127 + /**
6.128 + * Parses the string argument as a signed decimal {@code
6.129 + * short}. The characters in the string must all be decimal
6.130 + * digits, except that the first character may be an ASCII minus
6.131 + * sign {@code '-'} (<code>'\u002D'</code>) to indicate a
6.132 + * negative value or an ASCII plus sign {@code '+'}
6.133 + * (<code>'\u002B'</code>) to indicate a positive value. The
6.134 + * resulting {@code short} value is returned, exactly as if the
6.135 + * argument and the radix 10 were given as arguments to the {@link
6.136 + * #parseShort(java.lang.String, int)} method.
6.137 + *
6.138 + * @param s a {@code String} containing the {@code short}
6.139 + * representation to be parsed
6.140 + * @return the {@code short} value represented by the
6.141 + * argument in decimal.
6.142 + * @throws NumberFormatException If the string does not
6.143 + * contain a parsable {@code short}.
6.144 + */
6.145 + public static short parseShort(String s) throws NumberFormatException {
6.146 + return parseShort(s, 10);
6.147 + }
6.148 +
6.149 + /**
6.150 + * Returns a {@code Short} object holding the value
6.151 + * extracted from the specified {@code String} when parsed
6.152 + * with the radix given by the second argument. The first argument
6.153 + * is interpreted as representing a signed {@code short} in
6.154 + * the radix specified by the second argument, exactly as if the
6.155 + * argument were given to the {@link #parseShort(java.lang.String,
6.156 + * int)} method. The result is a {@code Short} object that
6.157 + * represents the {@code short} value specified by the string.
6.158 + *
6.159 + * <p>In other words, this method returns a {@code Short} object
6.160 + * equal to the value of:
6.161 + *
6.162 + * <blockquote>
6.163 + * {@code new Short(Short.parseShort(s, radix))}
6.164 + * </blockquote>
6.165 + *
6.166 + * @param s the string to be parsed
6.167 + * @param radix the radix to be used in interpreting {@code s}
6.168 + * @return a {@code Short} object holding the value
6.169 + * represented by the string argument in the
6.170 + * specified radix.
6.171 + * @throws NumberFormatException If the {@code String} does
6.172 + * not contain a parsable {@code short}.
6.173 + */
6.174 + public static Short valueOf(String s, int radix)
6.175 + throws NumberFormatException {
6.176 + return valueOf(parseShort(s, radix));
6.177 + }
6.178 +
6.179 + /**
6.180 + * Returns a {@code Short} object holding the
6.181 + * value given by the specified {@code String}. The argument
6.182 + * is interpreted as representing a signed decimal
6.183 + * {@code short}, exactly as if the argument were given to
6.184 + * the {@link #parseShort(java.lang.String)} method. The result is
6.185 + * a {@code Short} object that represents the
6.186 + * {@code short} value specified by the string.
6.187 + *
6.188 + * <p>In other words, this method returns a {@code Short} object
6.189 + * equal to the value of:
6.190 + *
6.191 + * <blockquote>
6.192 + * {@code new Short(Short.parseShort(s))}
6.193 + * </blockquote>
6.194 + *
6.195 + * @param s the string to be parsed
6.196 + * @return a {@code Short} object holding the value
6.197 + * represented by the string argument
6.198 + * @throws NumberFormatException If the {@code String} does
6.199 + * not contain a parsable {@code short}.
6.200 + */
6.201 + public static Short valueOf(String s) throws NumberFormatException {
6.202 + return valueOf(s, 10);
6.203 + }
6.204 +
6.205 + private static class ShortCache {
6.206 + private ShortCache(){}
6.207 +
6.208 + static final Short cache[] = new Short[-(-128) + 127 + 1];
6.209 +
6.210 + static {
6.211 + for(int i = 0; i < cache.length; i++)
6.212 + cache[i] = new Short((short)(i - 128));
6.213 + }
6.214 + }
6.215 +
6.216 + /**
6.217 + * Returns a {@code Short} instance representing the specified
6.218 + * {@code short} value.
6.219 + * If a new {@code Short} instance is not required, this method
6.220 + * should generally be used in preference to the constructor
6.221 + * {@link #Short(short)}, as this method is likely to yield
6.222 + * significantly better space and time performance by caching
6.223 + * frequently requested values.
6.224 + *
6.225 + * This method will always cache values in the range -128 to 127,
6.226 + * inclusive, and may cache other values outside of this range.
6.227 + *
6.228 + * @param s a short value.
6.229 + * @return a {@code Short} instance representing {@code s}.
6.230 + * @since 1.5
6.231 + */
6.232 + public static Short valueOf(short s) {
6.233 + final int offset = 128;
6.234 + int sAsInt = s;
6.235 + if (sAsInt >= -128 && sAsInt <= 127) { // must cache
6.236 + return ShortCache.cache[sAsInt + offset];
6.237 + }
6.238 + return new Short(s);
6.239 + }
6.240 +
6.241 + /**
6.242 + * Decodes a {@code String} into a {@code Short}.
6.243 + * Accepts decimal, hexadecimal, and octal numbers given by
6.244 + * the following grammar:
6.245 + *
6.246 + * <blockquote>
6.247 + * <dl>
6.248 + * <dt><i>DecodableString:</i>
6.249 + * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
6.250 + * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
6.251 + * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
6.252 + * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
6.253 + * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
6.254 + * <p>
6.255 + * <dt><i>Sign:</i>
6.256 + * <dd>{@code -}
6.257 + * <dd>{@code +}
6.258 + * </dl>
6.259 + * </blockquote>
6.260 + *
6.261 + * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
6.262 + * are as defined in section 3.10.1 of
6.263 + * <cite>The Java™ Language Specification</cite>,
6.264 + * except that underscores are not accepted between digits.
6.265 + *
6.266 + * <p>The sequence of characters following an optional
6.267 + * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
6.268 + * "{@code #}", or leading zero) is parsed as by the {@code
6.269 + * Short.parseShort} method with the indicated radix (10, 16, or
6.270 + * 8). This sequence of characters must represent a positive
6.271 + * value or a {@link NumberFormatException} will be thrown. The
6.272 + * result is negated if first character of the specified {@code
6.273 + * String} is the minus sign. No whitespace characters are
6.274 + * permitted in the {@code String}.
6.275 + *
6.276 + * @param nm the {@code String} to decode.
6.277 + * @return a {@code Short} object holding the {@code short}
6.278 + * value represented by {@code nm}
6.279 + * @throws NumberFormatException if the {@code String} does not
6.280 + * contain a parsable {@code short}.
6.281 + * @see java.lang.Short#parseShort(java.lang.String, int)
6.282 + */
6.283 + public static Short decode(String nm) throws NumberFormatException {
6.284 + int i = Integer.decode(nm);
6.285 + if (i < MIN_VALUE || i > MAX_VALUE)
6.286 + throw new NumberFormatException(
6.287 + "Value " + i + " out of range from input " + nm);
6.288 + return valueOf((short)i);
6.289 + }
6.290 +
6.291 + /**
6.292 + * The value of the {@code Short}.
6.293 + *
6.294 + * @serial
6.295 + */
6.296 + private final short value;
6.297 +
6.298 + /**
6.299 + * Constructs a newly allocated {@code Short} object that
6.300 + * represents the specified {@code short} value.
6.301 + *
6.302 + * @param value the value to be represented by the
6.303 + * {@code Short}.
6.304 + */
6.305 + public Short(short value) {
6.306 + this.value = value;
6.307 + }
6.308 +
6.309 + /**
6.310 + * Constructs a newly allocated {@code Short} object that
6.311 + * represents the {@code short} value indicated by the
6.312 + * {@code String} parameter. The string is converted to a
6.313 + * {@code short} value in exactly the manner used by the
6.314 + * {@code parseShort} method for radix 10.
6.315 + *
6.316 + * @param s the {@code String} to be converted to a
6.317 + * {@code Short}
6.318 + * @throws NumberFormatException If the {@code String}
6.319 + * does not contain a parsable {@code short}.
6.320 + * @see java.lang.Short#parseShort(java.lang.String, int)
6.321 + */
6.322 + public Short(String s) throws NumberFormatException {
6.323 + this.value = parseShort(s, 10);
6.324 + }
6.325 +
6.326 + /**
6.327 + * Returns the value of this {@code Short} as a
6.328 + * {@code byte}.
6.329 + */
6.330 + public byte byteValue() {
6.331 + return (byte)value;
6.332 + }
6.333 +
6.334 + /**
6.335 + * Returns the value of this {@code Short} as a
6.336 + * {@code short}.
6.337 + */
6.338 + public short shortValue() {
6.339 + return value;
6.340 + }
6.341 +
6.342 + /**
6.343 + * Returns the value of this {@code Short} as an
6.344 + * {@code int}.
6.345 + */
6.346 + public int intValue() {
6.347 + return (int)value;
6.348 + }
6.349 +
6.350 + /**
6.351 + * Returns the value of this {@code Short} as a
6.352 + * {@code long}.
6.353 + */
6.354 + public long longValue() {
6.355 + return (long)value;
6.356 + }
6.357 +
6.358 + /**
6.359 + * Returns the value of this {@code Short} as a
6.360 + * {@code float}.
6.361 + */
6.362 + public float floatValue() {
6.363 + return (float)value;
6.364 + }
6.365 +
6.366 + /**
6.367 + * Returns the value of this {@code Short} as a
6.368 + * {@code double}.
6.369 + */
6.370 + public double doubleValue() {
6.371 + return (double)value;
6.372 + }
6.373 +
6.374 + /**
6.375 + * Returns a {@code String} object representing this
6.376 + * {@code Short}'s value. The value is converted to signed
6.377 + * decimal representation and returned as a string, exactly as if
6.378 + * the {@code short} value were given as an argument to the
6.379 + * {@link java.lang.Short#toString(short)} method.
6.380 + *
6.381 + * @return a string representation of the value of this object in
6.382 + * base 10.
6.383 + */
6.384 + public String toString() {
6.385 + return Integer.toString((int)value);
6.386 + }
6.387 +
6.388 + /**
6.389 + * Returns a hash code for this {@code Short}; equal to the result
6.390 + * of invoking {@code intValue()}.
6.391 + *
6.392 + * @return a hash code value for this {@code Short}
6.393 + */
6.394 + public int hashCode() {
6.395 + return (int)value;
6.396 + }
6.397 +
6.398 + /**
6.399 + * Compares this object to the specified object. The result is
6.400 + * {@code true} if and only if the argument is not
6.401 + * {@code null} and is a {@code Short} object that
6.402 + * contains the same {@code short} value as this object.
6.403 + *
6.404 + * @param obj the object to compare with
6.405 + * @return {@code true} if the objects are the same;
6.406 + * {@code false} otherwise.
6.407 + */
6.408 + public boolean equals(Object obj) {
6.409 + if (obj instanceof Short) {
6.410 + return value == ((Short)obj).shortValue();
6.411 + }
6.412 + return false;
6.413 + }
6.414 +
6.415 + /**
6.416 + * Compares two {@code Short} objects numerically.
6.417 + *
6.418 + * @param anotherShort the {@code Short} to be compared.
6.419 + * @return the value {@code 0} if this {@code Short} is
6.420 + * equal to the argument {@code Short}; a value less than
6.421 + * {@code 0} if this {@code Short} is numerically less
6.422 + * than the argument {@code Short}; and a value greater than
6.423 + * {@code 0} if this {@code Short} is numerically
6.424 + * greater than the argument {@code Short} (signed
6.425 + * comparison).
6.426 + * @since 1.2
6.427 + */
6.428 + public int compareTo(Short anotherShort) {
6.429 + return compare(this.value, anotherShort.value);
6.430 + }
6.431 +
6.432 + /**
6.433 + * Compares two {@code short} values numerically.
6.434 + * The value returned is identical to what would be returned by:
6.435 + * <pre>
6.436 + * Short.valueOf(x).compareTo(Short.valueOf(y))
6.437 + * </pre>
6.438 + *
6.439 + * @param x the first {@code short} to compare
6.440 + * @param y the second {@code short} to compare
6.441 + * @return the value {@code 0} if {@code x == y};
6.442 + * a value less than {@code 0} if {@code x < y}; and
6.443 + * a value greater than {@code 0} if {@code x > y}
6.444 + * @since 1.7
6.445 + */
6.446 + public static int compare(short x, short y) {
6.447 + return x - y;
6.448 + }
6.449 +
6.450 + /**
6.451 + * The number of bits used to represent a {@code short} value in two's
6.452 + * complement binary form.
6.453 + * @since 1.5
6.454 + */
6.455 + public static final int SIZE = 16;
6.456 +
6.457 + /**
6.458 + * Returns the value obtained by reversing the order of the bytes in the
6.459 + * two's complement representation of the specified {@code short} value.
6.460 + *
6.461 + * @return the value obtained by reversing (or, equivalently, swapping)
6.462 + * the bytes in the specified {@code short} value.
6.463 + * @since 1.5
6.464 + */
6.465 + public static short reverseBytes(short i) {
6.466 + return (short) (((i & 0xFF00) >> 8) | (i << 8));
6.467 + }
6.468 +
6.469 + /** use serialVersionUID from JDK 1.1. for interoperability */
6.470 + private static final long serialVersionUID = 7515723908773894738L;
6.471 +}