| 1 | /* Double.java -- object wrapper for double
|
|---|
| 2 | Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.lang;
|
|---|
| 40 |
|
|---|
| 41 | import gnu.classpath.Configuration;
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Instances of class <code>Double</code> represent primitive
|
|---|
| 45 | * <code>double</code> values.
|
|---|
| 46 | *
|
|---|
| 47 | * Additionally, this class provides various helper functions and variables
|
|---|
| 48 | * related to doubles.
|
|---|
| 49 | *
|
|---|
| 50 | * @author Paul Fisher
|
|---|
| 51 | * @author Andrew Haley <[email protected]>
|
|---|
| 52 | * @author Eric Blake <[email protected]>
|
|---|
| 53 | * @since 1.0
|
|---|
| 54 | * @status updated to 1.4
|
|---|
| 55 | */
|
|---|
| 56 | public final class Double extends Number implements Comparable
|
|---|
| 57 | {
|
|---|
| 58 | /**
|
|---|
| 59 | * Compatible with JDK 1.0+.
|
|---|
| 60 | */
|
|---|
| 61 | private static final long serialVersionUID = -9172774392245257468L;
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * The maximum positive value a <code>double</code> may represent
|
|---|
| 65 | * is 1.7976931348623157e+308.
|
|---|
| 66 | */
|
|---|
| 67 | public static final double MAX_VALUE = 1.7976931348623157e+308;
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * The minimum positive value a <code>double</code> may represent
|
|---|
| 71 | * is 5e-324.
|
|---|
| 72 | */
|
|---|
| 73 | public static final double MIN_VALUE = 5e-324;
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * The value of a double representation -1.0/0.0, negative
|
|---|
| 77 | * infinity.
|
|---|
| 78 | */
|
|---|
| 79 | public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * The value of a double representing 1.0/0.0, positive infinity.
|
|---|
| 83 | */
|
|---|
| 84 | public static final double POSITIVE_INFINITY = 1.0 / 0.0;
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * All IEEE 754 values of NaN have the same value in Java.
|
|---|
| 88 | */
|
|---|
| 89 | public static final double NaN = 0.0 / 0.0;
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * The primitive type <code>double</code> is represented by this
|
|---|
| 93 | * <code>Class</code> object.
|
|---|
| 94 | * @since 1.1
|
|---|
| 95 | */
|
|---|
| 96 | public static final Class TYPE = VMClassLoader.getPrimitiveClass('D');
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * The immutable value of this Double.
|
|---|
| 100 | *
|
|---|
| 101 | * @serial the wrapped double
|
|---|
| 102 | */
|
|---|
| 103 | private final double value;
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Load native routines necessary for this class.
|
|---|
| 107 | */
|
|---|
| 108 | static
|
|---|
| 109 | {
|
|---|
| 110 | if (Configuration.INIT_LOAD_LIBRARY)
|
|---|
| 111 | {
|
|---|
| 112 | System.loadLibrary("javalang");
|
|---|
| 113 | initIDs();
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | * Create a <code>Double</code> from the primitive <code>double</code>
|
|---|
| 119 | * specified.
|
|---|
| 120 | *
|
|---|
| 121 | * @param value the <code>double</code> argument
|
|---|
| 122 | */
|
|---|
| 123 | public Double(double value)
|
|---|
| 124 | {
|
|---|
| 125 | this.value = value;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Create a <code>Double</code> from the specified <code>String</code>.
|
|---|
| 130 | * This method calls <code>Double.parseDouble()</code>.
|
|---|
| 131 | *
|
|---|
| 132 | * @param s the <code>String</code> to convert
|
|---|
| 133 | * @throws NumberFormatException if <code>s</code> cannot be parsed as a
|
|---|
| 134 | * <code>double</code>
|
|---|
| 135 | * @throws NullPointerException if <code>s</code> is null
|
|---|
| 136 | * @see #parseDouble(String)
|
|---|
| 137 | */
|
|---|
| 138 | public Double(String s)
|
|---|
| 139 | {
|
|---|
| 140 | value = parseDouble(s);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Convert the <code>double</code> to a <code>String</code>.
|
|---|
| 145 | * Floating-point string representation is fairly complex: here is a
|
|---|
| 146 | * rundown of the possible values. "<code>[-]</code>" indicates that a
|
|---|
| 147 | * negative sign will be printed if the value (or exponent) is negative.
|
|---|
| 148 | * "<code><number></code>" means a string of digits ('0' to '9').
|
|---|
| 149 | * "<code><digit></code>" means a single digit ('0' to '9').<br>
|
|---|
| 150 | *
|
|---|
| 151 | * <table border=1>
|
|---|
| 152 | * <tr><th>Value of Double</th><th>String Representation</th></tr>
|
|---|
| 153 | * <tr><td>[+-] 0</td> <td><code>[-]0.0</code></td></tr>
|
|---|
| 154 | * <tr><td>Between [+-] 10<sup>-3</sup> and 10<sup>7</sup>, exclusive</td>
|
|---|
| 155 | * <td><code>[-]number.number</code></td></tr>
|
|---|
| 156 | * <tr><td>Other numeric value</td>
|
|---|
| 157 | * <td><code>[-]<digit>.<number>
|
|---|
| 158 | * E[-]<number></code></td></tr>
|
|---|
| 159 | * <tr><td>[+-] infinity</td> <td><code>[-]Infinity</code></td></tr>
|
|---|
| 160 | * <tr><td>NaN</td> <td><code>NaN</code></td></tr>
|
|---|
| 161 | * </table>
|
|---|
| 162 | *
|
|---|
| 163 | * Yes, negative zero <em>is</em> a possible value. Note that there is
|
|---|
| 164 | * <em>always</em> a <code>.</code> and at least one digit printed after
|
|---|
| 165 | * it: even if the number is 3, it will be printed as <code>3.0</code>.
|
|---|
| 166 | * After the ".", all digits will be printed except trailing zeros. The
|
|---|
| 167 | * result is rounded to the shortest decimal number which will parse back
|
|---|
| 168 | * to the same double.
|
|---|
| 169 | *
|
|---|
| 170 | * <p>To create other output formats, use {@link java.text.NumberFormat}.
|
|---|
| 171 | *
|
|---|
| 172 | * @XXX specify where we are not in accord with the spec.
|
|---|
| 173 | *
|
|---|
| 174 | * @param d the <code>double</code> to convert
|
|---|
| 175 | * @return the <code>String</code> representing the <code>double</code>
|
|---|
| 176 | */
|
|---|
| 177 | public static String toString(double d)
|
|---|
| 178 | {
|
|---|
| 179 | return toString(d, false);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * Create a new <code>Double</code> object using the <code>String</code>.
|
|---|
| 184 | *
|
|---|
| 185 | * @param s the <code>String</code> to convert
|
|---|
| 186 | * @return the new <code>Double</code>
|
|---|
| 187 | * @throws NumberFormatException if <code>s</code> cannot be parsed as a
|
|---|
| 188 | * <code>double</code>
|
|---|
| 189 | * @throws NullPointerException if <code>s</code> is null.
|
|---|
| 190 | * @see #parseDouble(String)
|
|---|
| 191 | */
|
|---|
| 192 | public static Double valueOf(String s)
|
|---|
| 193 | {
|
|---|
| 194 | return new Double(parseDouble(s));
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /**
|
|---|
| 198 | * Parse the specified <code>String</code> as a <code>double</code>. The
|
|---|
| 199 | * extended BNF grammar is as follows:<br>
|
|---|
| 200 | * <pre>
|
|---|
| 201 | * <em>DecodableString</em>:
|
|---|
| 202 | * ( [ <code>-</code> | <code>+</code> ] <code>NaN</code> )
|
|---|
| 203 | * | ( [ <code>-</code> | <code>+</code> ] <code>Infinity</code> )
|
|---|
| 204 | * | ( [ <code>-</code> | <code>+</code> ] <em>FloatingPoint</em>
|
|---|
| 205 | * [ <code>f</code> | <code>F</code> | <code>d</code>
|
|---|
| 206 | * | <code>D</code>] )
|
|---|
| 207 | * <em>FloatingPoint</em>:
|
|---|
| 208 | * ( { <em>Digit</em> }+ [ <code>.</code> { <em>Digit</em> } ]
|
|---|
| 209 | * [ <em>Exponent</em> ] )
|
|---|
| 210 | * | ( <code>.</code> { <em>Digit</em> }+ [ <em>Exponent</em> ] )
|
|---|
| 211 | * <em>Exponent</em>:
|
|---|
| 212 | * ( ( <code>e</code> | <code>E</code> )
|
|---|
| 213 | * [ <code>-</code> | <code>+</code> ] { <em>Digit</em> }+ )
|
|---|
| 214 | * <em>Digit</em>: <em><code>'0'</code> through <code>'9'</code></em>
|
|---|
| 215 | * </pre>
|
|---|
| 216 | *
|
|---|
| 217 | * <p>NaN and infinity are special cases, to allow parsing of the output
|
|---|
| 218 | * of toString. Otherwise, the result is determined by calculating
|
|---|
| 219 | * <em>n * 10<sup>exponent</sup></em> to infinite precision, then rounding
|
|---|
| 220 | * to the nearest double. Remember that many numbers cannot be precisely
|
|---|
| 221 | * represented in floating point. In case of overflow, infinity is used,
|
|---|
| 222 | * and in case of underflow, signed zero is used. Unlike Integer.parseInt,
|
|---|
| 223 | * this does not accept Unicode digits outside the ASCII range.
|
|---|
| 224 | *
|
|---|
| 225 | * <p>If an unexpected character is found in the <code>String</code>, a
|
|---|
| 226 | * <code>NumberFormatException</code> will be thrown. Leading and trailing
|
|---|
| 227 | * 'whitespace' is ignored via <code>String.trim()</code>, but spaces
|
|---|
| 228 | * internal to the actual number are not allowed.
|
|---|
| 229 | *
|
|---|
| 230 | * <p>To parse numbers according to another format, consider using
|
|---|
| 231 | * {@link java.text.NumberFormat}.
|
|---|
| 232 | *
|
|---|
| 233 | * @XXX specify where/how we are not in accord with the spec.
|
|---|
| 234 | *
|
|---|
| 235 | * @param str the <code>String</code> to convert
|
|---|
| 236 | * @return the <code>double</code> value of <code>s</code>
|
|---|
| 237 | * @throws NumberFormatException if <code>s</code> cannot be parsed as a
|
|---|
| 238 | * <code>double</code>
|
|---|
| 239 | * @throws NullPointerException if <code>s</code> is null
|
|---|
| 240 | * @see #MIN_VALUE
|
|---|
| 241 | * @see #MAX_VALUE
|
|---|
| 242 | * @see #POSITIVE_INFINITY
|
|---|
| 243 | * @see #NEGATIVE_INFINITY
|
|---|
| 244 | * @since 1.2
|
|---|
| 245 | */
|
|---|
| 246 | public static native double parseDouble(String s);
|
|---|
| 247 |
|
|---|
| 248 | /**
|
|---|
| 249 | * Return <code>true</code> if the <code>double</code> has the same
|
|---|
| 250 | * value as <code>NaN</code>, otherwise return <code>false</code>.
|
|---|
| 251 | *
|
|---|
| 252 | * @param v the <code>double</code> to compare
|
|---|
| 253 | * @return whether the argument is <code>NaN</code>.
|
|---|
| 254 | */
|
|---|
| 255 | public static boolean isNaN(double v)
|
|---|
| 256 | {
|
|---|
| 257 | // This works since NaN != NaN is the only reflexive inequality
|
|---|
| 258 | // comparison which returns true.
|
|---|
| 259 | return v != v;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /**
|
|---|
| 263 | * Return <code>true</code> if the <code>double</code> has a value
|
|---|
| 264 | * equal to either <code>NEGATIVE_INFINITY</code> or
|
|---|
| 265 | * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
|
|---|
| 266 | *
|
|---|
| 267 | * @param v the <code>double</code> to compare
|
|---|
| 268 | * @return whether the argument is (-/+) infinity.
|
|---|
| 269 | */
|
|---|
| 270 | public static boolean isInfinite(double v)
|
|---|
| 271 | {
|
|---|
| 272 | return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /**
|
|---|
| 276 | * Return <code>true</code> if the value of this <code>Double</code>
|
|---|
| 277 | * is the same as <code>NaN</code>, otherwise return <code>false</code>.
|
|---|
| 278 | *
|
|---|
| 279 | * @return whether this <code>Double</code> is <code>NaN</code>
|
|---|
| 280 | */
|
|---|
| 281 | public boolean isNaN()
|
|---|
| 282 | {
|
|---|
| 283 | return isNaN(value);
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /**
|
|---|
| 287 | * Return <code>true</code> if the value of this <code>Double</code>
|
|---|
| 288 | * is the same as <code>NEGATIVE_INFINITY</code> or
|
|---|
| 289 | * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
|
|---|
| 290 | *
|
|---|
| 291 | * @return whether this <code>Double</code> is (-/+) infinity
|
|---|
| 292 | */
|
|---|
| 293 | public boolean isInfinite()
|
|---|
| 294 | {
|
|---|
| 295 | return isInfinite(value);
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | /**
|
|---|
| 299 | * Convert the <code>double</code> value of this <code>Double</code>
|
|---|
| 300 | * to a <code>String</code>. This method calls
|
|---|
| 301 | * <code>Double.toString(double)</code> to do its dirty work.
|
|---|
| 302 | *
|
|---|
| 303 | * @return the <code>String</code> representation
|
|---|
| 304 | * @see #toString(double)
|
|---|
| 305 | */
|
|---|
| 306 | public String toString()
|
|---|
| 307 | {
|
|---|
| 308 | return toString(value);
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | /**
|
|---|
| 312 | * Return the value of this <code>Double</code> as a <code>byte</code>.
|
|---|
| 313 | *
|
|---|
| 314 | * @return the byte value
|
|---|
| 315 | * @since 1.1
|
|---|
| 316 | */
|
|---|
| 317 | public byte byteValue()
|
|---|
| 318 | {
|
|---|
| 319 | return (byte) value;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | /**
|
|---|
| 323 | * Return the value of this <code>Double</code> as a <code>short</code>.
|
|---|
| 324 | *
|
|---|
| 325 | * @return the short value
|
|---|
| 326 | * @since 1.1
|
|---|
| 327 | */
|
|---|
| 328 | public short shortValue()
|
|---|
| 329 | {
|
|---|
| 330 | return (short) value;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | /**
|
|---|
| 334 | * Return the value of this <code>Double</code> as an <code>int</code>.
|
|---|
| 335 | *
|
|---|
| 336 | * @return the int value
|
|---|
| 337 | */
|
|---|
| 338 | public int intValue()
|
|---|
| 339 | {
|
|---|
| 340 | return (int) value;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | /**
|
|---|
| 344 | * Return the value of this <code>Double</code> as a <code>long</code>.
|
|---|
| 345 | *
|
|---|
| 346 | * @return the long value
|
|---|
| 347 | */
|
|---|
| 348 | public long longValue()
|
|---|
| 349 | {
|
|---|
| 350 | return (long) value;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /**
|
|---|
| 354 | * Return the value of this <code>Double</code> as a <code>float</code>.
|
|---|
| 355 | *
|
|---|
| 356 | * @return the float value
|
|---|
| 357 | */
|
|---|
| 358 | public float floatValue()
|
|---|
| 359 | {
|
|---|
| 360 | return (float) value;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | /**
|
|---|
| 364 | * Return the value of this <code>Double</code>.
|
|---|
| 365 | *
|
|---|
| 366 | * @return the double value
|
|---|
| 367 | */
|
|---|
| 368 | public double doubleValue()
|
|---|
| 369 | {
|
|---|
| 370 | return value;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | /**
|
|---|
| 374 | * Return a hashcode representing this Object. <code>Double</code>'s hash
|
|---|
| 375 | * code is calculated by:<br>
|
|---|
| 376 | * <code>long v = Double.doubleToLongBits(doubleValue());<br>
|
|---|
| 377 | * int hash = (int)(v^(v>>32))</code>.
|
|---|
| 378 | *
|
|---|
| 379 | * @return this Object's hash code
|
|---|
| 380 | * @see #doubleToLongBits(double)
|
|---|
| 381 | */
|
|---|
| 382 | public int hashCode()
|
|---|
| 383 | {
|
|---|
| 384 | long v = doubleToLongBits(value);
|
|---|
| 385 | return (int) (v ^ (v >>> 32));
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | /**
|
|---|
| 389 | * Returns <code>true</code> if <code>obj</code> is an instance of
|
|---|
| 390 | * <code>Double</code> and represents the same double value. Unlike comparing
|
|---|
| 391 | * two doubles with <code>==</code>, this treats two instances of
|
|---|
| 392 | * <code>Double.NaN</code> as equal, but treats <code>0.0</code> and
|
|---|
| 393 | * <code>-0.0</code> as unequal.
|
|---|
| 394 | *
|
|---|
| 395 | * <p>Note that <code>d1.equals(d2)<code> is identical to
|
|---|
| 396 | * <code>doubleToLongBits(d1.doubleValue()) ==
|
|---|
| 397 | * doubleToLongBits(d2.doubleValue())<code>.
|
|---|
| 398 | *
|
|---|
| 399 | * @param obj the object to compare
|
|---|
| 400 | * @return whether the objects are semantically equal
|
|---|
| 401 | */
|
|---|
| 402 | public boolean equals(Object obj)
|
|---|
| 403 | {
|
|---|
| 404 | if (! (obj instanceof Double))
|
|---|
| 405 | return false;
|
|---|
| 406 |
|
|---|
| 407 | double d = ((Double) obj).value;
|
|---|
| 408 |
|
|---|
| 409 | // Avoid call to native method. However, some implementations, like gcj,
|
|---|
| 410 | // are better off using floatToIntBits(value) == floatToIntBits(f).
|
|---|
| 411 | // Check common case first, then check NaN and 0.
|
|---|
| 412 | if (value == d)
|
|---|
| 413 | return (value != 0) || (1 / value == 1 / d);
|
|---|
| 414 | return isNaN(value) && isNaN(d);
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | /**
|
|---|
| 418 | * Convert the double to the IEEE 754 floating-point "double format" bit
|
|---|
| 419 | * layout. Bit 63 (the most significant) is the sign bit, bits 62-52
|
|---|
| 420 | * (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
|
|---|
| 421 | * (masked by 0x000fffffffffffffL) are the mantissa. This function
|
|---|
| 422 | * collapses all versions of NaN to 0x7ff8000000000000L. The result of this
|
|---|
| 423 | * function can be used as the argument to
|
|---|
| 424 | * <code>Double.longBitsToDouble(long)</code> to obtain the original
|
|---|
| 425 | * <code>double</code> value.
|
|---|
| 426 | *
|
|---|
| 427 | * @param value the <code>double</code> to convert
|
|---|
| 428 | * @return the bits of the <code>double</code>
|
|---|
| 429 | * @see #longBitsToDouble(long)
|
|---|
| 430 | */
|
|---|
| 431 | public static native long doubleToLongBits(double value);
|
|---|
| 432 |
|
|---|
| 433 | /**
|
|---|
| 434 | * Convert the double to the IEEE 754 floating-point "double format" bit
|
|---|
| 435 | * layout. Bit 63 (the most significant) is the sign bit, bits 62-52
|
|---|
| 436 | * (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
|
|---|
| 437 | * (masked by 0x000fffffffffffffL) are the mantissa. This function
|
|---|
| 438 | * leaves NaN alone, rather than collapsing to a canonical value. The
|
|---|
| 439 | * result of this function can be used as the argument to
|
|---|
| 440 | * <code>Double.longBitsToDouble(long)</code> to obtain the original
|
|---|
| 441 | * <code>double</code> value.
|
|---|
| 442 | *
|
|---|
| 443 | * @param value the <code>double</code> to convert
|
|---|
| 444 | * @return the bits of the <code>double</code>
|
|---|
| 445 | * @see #longBitsToDouble(long)
|
|---|
| 446 | */
|
|---|
| 447 | public static native long doubleToRawLongBits(double value);
|
|---|
| 448 |
|
|---|
| 449 | /**
|
|---|
| 450 | * Convert the argument in IEEE 754 floating-point "double format" bit
|
|---|
| 451 | * layout to the corresponding float. Bit 63 (the most significant) is the
|
|---|
| 452 | * sign bit, bits 62-52 (masked by 0x7ff0000000000000L) represent the
|
|---|
| 453 | * exponent, and bits 51-0 (masked by 0x000fffffffffffffL) are the mantissa.
|
|---|
| 454 | * This function leaves NaN alone, so that you can recover the bit pattern
|
|---|
| 455 | * with <code>Double.doubleToRawLongBits(double)</code>.
|
|---|
| 456 | *
|
|---|
| 457 | * @param bits the bits to convert
|
|---|
| 458 | * @return the <code>double</code> represented by the bits
|
|---|
| 459 | * @see #doubleToLongBits(double)
|
|---|
| 460 | * @see #doubleToRawLongBits(double)
|
|---|
| 461 | */
|
|---|
| 462 | public static native double longBitsToDouble(long bits);
|
|---|
| 463 |
|
|---|
| 464 | /**
|
|---|
| 465 | * Compare two Doubles numerically by comparing their <code>double</code>
|
|---|
| 466 | * values. The result is positive if the first is greater, negative if the
|
|---|
| 467 | * second is greater, and 0 if the two are equal. However, this special
|
|---|
| 468 | * cases NaN and signed zero as follows: NaN is considered greater than
|
|---|
| 469 | * all other doubles, including <code>POSITIVE_INFINITY</code>, and positive
|
|---|
| 470 | * zero is considered greater than negative zero.
|
|---|
| 471 | *
|
|---|
| 472 | * @param d the Double to compare
|
|---|
| 473 | * @return the comparison
|
|---|
| 474 | * @since 1.2
|
|---|
| 475 | */
|
|---|
| 476 | public int compareTo(Double d)
|
|---|
| 477 | {
|
|---|
| 478 | return compare(value, d.value);
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | /**
|
|---|
| 482 | * Behaves like <code>compareTo(Double)</code> unless the Object
|
|---|
| 483 | * is not an <code>Double</code>.
|
|---|
| 484 | *
|
|---|
| 485 | * @param o the object to compare
|
|---|
| 486 | * @return the comparison
|
|---|
| 487 | * @throws ClassCastException if the argument is not a <code>Double</code>
|
|---|
| 488 | * @see #compareTo(Double)
|
|---|
| 489 | * @see Comparable
|
|---|
| 490 | * @since 1.2
|
|---|
| 491 | */
|
|---|
| 492 | public int compareTo(Object o)
|
|---|
| 493 | {
|
|---|
| 494 | return compare(value, ((Double) o).value);
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | /**
|
|---|
| 498 | * Behaves like <code>new Double(x).compareTo(new Double(y))</code>; in
|
|---|
| 499 | * other words this compares two doubles, special casing NaN and zero,
|
|---|
| 500 | * without the overhead of objects.
|
|---|
| 501 | *
|
|---|
| 502 | * @param x the first double to compare
|
|---|
| 503 | * @param y the second double to compare
|
|---|
| 504 | * @return the comparison
|
|---|
| 505 | * @since 1.4
|
|---|
| 506 | */
|
|---|
| 507 | public static int compare(double x, double y)
|
|---|
| 508 | {
|
|---|
| 509 | if (isNaN(x))
|
|---|
| 510 | return isNaN(y) ? 0 : 1;
|
|---|
| 511 | if (isNaN(y))
|
|---|
| 512 | return -1;
|
|---|
| 513 | // recall that 0.0 == -0.0, so we convert to infinites and try again
|
|---|
| 514 | if (x == 0 && y == 0)
|
|---|
| 515 | return (int) (1 / x - 1 / y);
|
|---|
| 516 | if (x == y)
|
|---|
| 517 | return 0;
|
|---|
| 518 |
|
|---|
| 519 | return x > y ? 1 : -1;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | /**
|
|---|
| 523 | * Helper method to convert to string.
|
|---|
| 524 | *
|
|---|
| 525 | * @param d the double to convert
|
|---|
| 526 | * @param isFloat true if the conversion is requested by Float (results in
|
|---|
| 527 | * fewer digits)
|
|---|
| 528 | */
|
|---|
| 529 | // Package visible for use by Float.
|
|---|
| 530 | static native String toString(double d, boolean isFloat);
|
|---|
| 531 |
|
|---|
| 532 | /**
|
|---|
| 533 | * Initialize JNI cache. This method is called only by the
|
|---|
| 534 | * static initializer when using JNI.
|
|---|
| 535 | */
|
|---|
| 536 | private static native void initIDs();
|
|---|
| 537 | }
|
|---|