source: trunk/src/gcc/libjava/java/lang/Double.java@ 1213

Last change on this file since 1213 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 17.3 KB
Line 
1/* Double.java -- object wrapper for double primitive
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.lang;
40
41import gnu.classpath.Configuration;
42
43/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
44 * "The Java Language Specification", ISBN 0-201-63451-1
45 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
46 * Status: Believed complete and correct.
47 */
48
49/**
50 * Instances of class <code>Double</code> represent primitive
51 * <code>double</code> values.
52 *
53 * Additionally, this class provides various helper functions and variables
54 * related to doubles.
55 *
56 * @author Paul Fisher
57 * @author Andrew Haley <[email protected]>
58 * @since JDK 1.0
59 */
60public final class Double extends Number implements Comparable
61{
62 /**
63 * The minimum positive value a <code>double</code> may represent
64 * is 5e-324.
65 */
66 public static final double MIN_VALUE = 5e-324;
67
68 /**
69 * The maximum positive value a <code>double</code> may represent
70 * is 1.7976931348623157e+308.
71 */
72 public static final double MAX_VALUE = 1.7976931348623157e+308;
73
74 /**
75 * The value of a double representation -1.0/0.0, negative
76 * infinity.
77 */
78 public static final double NEGATIVE_INFINITY = -1.0d/0.0d;
79
80 /**
81 * The value of a double representing 1.0/0.0, positive infinity.
82 */
83 public static final double POSITIVE_INFINITY = 1.0d/0.0d;
84
85 /**
86 * All IEEE 754 values of NaN have the same value in Java.
87 */
88 public static final double NaN = 0.0d/0.0d;
89
90 /**
91 * The primitive type <code>double</code> is represented by this
92 * <code>Class</code> object.
93 */
94 public static final Class TYPE = VMClassLoader.getPrimitiveClass('D');
95
96 /**
97 * The immutable value of this Double.
98 */
99 private final double value;
100
101 private static final long serialVersionUID = -9172774392245257468L;
102
103 /**
104 * Load native routines necessary for this class.
105 */
106 static
107 {
108 if (Configuration.INIT_LOAD_LIBRARY)
109 {
110 System.loadLibrary ("javalang");
111 initIDs ();
112 }
113 }
114
115 /**
116 * Create a <code>Double</code> from the primitive <code>double</code>
117 * specified.
118 *
119 * @param value the <code>double</code> argument
120 */
121 public Double (double value)
122 {
123 this.value = value;
124 }
125
126 /**
127 * Create a <code>Double</code> from the specified
128 * <code>String</code>.
129 *
130 * This method calls <code>Double.parseDouble()</code>.
131 *
132 * @exception NumberFormatException when the <code>String</code> cannot
133 * be parsed into a <code>Float</code>.
134 * @param s the <code>String</code> to convert
135 * @see #parseDouble(java.lang.String)
136 */
137 public Double (String s) throws NumberFormatException
138 {
139 value = parseDouble (s);
140 }
141
142 /**
143 * Convert the <code>double</code> value of this <code>Double</code>
144 * to a <code>String</code>. This method calls
145 * <code>Double.toString(double)</code> to do its dirty work.
146 *
147 * @return the <code>String</code> representation of this <code>Double</code>.
148 * @see #toString(double)
149 */
150 public String toString ()
151 {
152 return toString (value);
153 }
154
155 /**
156 * If the <code>Object</code> is not <code>null</code>, is an
157 * <code>instanceof</code> <code>Double</code>, and represents
158 * the same primitive <code>double</code> value return
159 * <code>true</code>. Otherwise <code>false</code> is returned.
160 * <p>
161 * Note that there are two differences between <code>==</code> and
162 * <code>equals()</code>. <code>0.0d == -0.0d</code> returns <code>true</code>
163 * but <code>new Double(0.0d).equals(new Double(-0.0d))</code> returns
164 * <code>false</code>. And <code>Double.NaN == Double.NaN</code> returns
165 * <code>false</code>, but
166 * <code>new Double(Double.NaN).equals(new Double(Double.NaN))</code> returns
167 * <code>true</code>.
168 *
169 * @param obj the object to compare to
170 * @return whether the objects are semantically equal.
171 */
172 public boolean equals (Object obj)
173 {
174 if (!(obj instanceof Double))
175 return false;
176
177 double d = ((Double) obj).value;
178
179 // GCJ LOCAL: this implementation is probably faster than
180 // Classpath's, especially once we inline doubleToLongBits.
181 return doubleToLongBits (value) == doubleToLongBits (d);
182 // END GCJ LOCAL
183 }
184
185 /**
186 * The hashcode is the value of the expression: <br>
187 * <br>
188 * <code>(int)(v^(v>>>32))</code><br>
189 * <br>
190 * where v is defined by: <br>
191 * <code>long v = Double.doubleToLongBits(this.longValue());</code><br>
192 */
193 public int hashCode ()
194 {
195 long v = doubleToLongBits (value);
196 return (int) (v ^ (v >>> 32));
197 }
198
199 /**
200 * Return the value of this <code>Double</code> when cast to an
201 * <code>int</code>.
202 */
203 public int intValue ()
204 {
205 return (int) value;
206 }
207
208 /**
209 * Return the value of this <code>Double</code> when cast to a
210 * <code>long</code>.
211 */
212 public long longValue ()
213 {
214 return (long) value;
215 }
216
217 /**
218 * Return the value of this <code>Double</code> when cast to a
219 * <code>float</code>.
220 */
221 public float floatValue ()
222 {
223 return (float) value;
224 }
225
226 /**
227 * Return the primitive <code>double</code> value represented by this
228 * <code>Double</code>.
229 */
230 public double doubleValue ()
231 {
232 return value;
233 }
234
235 /**
236 * Return the result of calling <code>new Double(java.lang.String)</code>.
237 *
238 * @param s the <code>String</code> to convert to a <code>Double</code>.
239 * @return a new <code>Double</code> representing the <code>String</code>'s
240 * numeric value.
241 *
242 * @exception NullPointerException thrown if <code>String</code> is
243 * <code>null</code>.
244 * @exception NumberFormatException thrown if <code>String</code> cannot
245 * be parsed as a <code>double</code>.
246 * @see #Double(java.lang.String)
247 * @see #parseDouble(java.lang.String)
248 */
249 public static Double valueOf (String s) throws NumberFormatException
250 {
251 return new Double (s);
252 }
253
254 /**
255 * Return <code>true</code> if the value of this <code>Double</code>
256 * is the same as <code>NaN</code>, otherwise return <code>false</code>.
257 * @return whether this <code>Double</code> is <code>NaN</code>.
258 */
259 public boolean isNaN ()
260 {
261 return isNaN (value);
262 }
263
264 /**
265 * Return <code>true</code> if the <code>double</code> has the same
266 * value as <code>NaN</code>, otherwise return <code>false</code>.
267 *
268 * @param v the <code>double</code> to compare
269 * @return whether the argument is <code>NaN</code>.
270 */
271 public static boolean isNaN (double v)
272 {
273 // This works since NaN != NaN is the only reflexive inequality
274 // comparison which returns true.
275 return v != v;
276 }
277
278 /**
279 * Return <code>true</code> if the value of this <code>Double</code>
280 * is the same as <code>NEGATIVE_INFINITY</code> or
281 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
282 *
283 * @return whether this <code>Double</code> is (-/+) infinity.
284 */
285 public boolean isInfinite ()
286 {
287 return isInfinite (value);
288 }
289
290 /**
291 * Return <code>true</code> if the <code>double</code> has a value
292 * equal to either <code>NEGATIVE_INFINITY</code> or
293 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
294 *
295 * @param v the <code>double</code> to compare
296 * @return whether the argument is (-/+) infinity.
297 */
298 public static boolean isInfinite (double v)
299 {
300 return (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY);
301 }
302
303 /**
304 * Returns 0 if the <code>double</code> value of the argument is
305 * equal to the value of this <code>Double</code>. Returns a number
306 * less than zero if the value of this <code>Double</code> is less
307 * than the <code>double</code> value of the argument, and returns a
308 * number greater than zero if the value of this <code>Double</code>
309 * is greater than the <code>double</code> value of the argument.
310 * <br>
311 * <code>Double.NaN</code> is greater than any number other than itself,
312 * even <code>Double.POSITIVE_INFINITY</code>.
313 * <br>
314 * <code>0.0d</code> is greater than <code>-0.0d</code>.
315 *
316 * @param d the Double to compare to.
317 * @return 0 if the <code>Double</code>s are the same, &lt; 0 if this
318 * <code>Double</code> is less than the <code>Double</code> in
319 * in question, or &gt; 0 if it is greater.
320 * @since 1.2
321 */
322 public int compareTo (Double d)
323 {
324 return compare (value, d.value);
325 }
326
327 /**
328 * Returns 0 if the first argument is equal to the second argument.
329 * Returns a number less than zero if the first argument is less than the
330 * second argument, and returns a number greater than zero if the first
331 * argument is greater than the second argument.
332 * <br>
333 * <code>Double.NaN</code> is greater than any number other than itself,
334 * even <code>Double.POSITIVE_INFINITY</code>.
335 * <br>
336 * <code>0.0d</code> is greater than <code>-0.0d</code>.
337 *
338 * @param x the first double to compare.
339 * @param y the second double to compare.
340 * @return 0 if the arguments are the same, &lt; 0 if the
341 * first argument is less than the second argument in
342 * in question, or &gt; 0 if it is greater.
343 * @since 1.4
344 */
345 public static int compare (double x, double y)
346 {
347 if (isNaN (x))
348 return isNaN (y) ? 0 : 1;
349 if (isNaN (y))
350 return -1;
351 // recall that 0.0 == -0.0, so we convert to infinites and try again
352 if (x == 0 && y == 0)
353 return (int) (1 / x - 1 / y);
354 if (x == y)
355 return 0;
356
357 return x > y ? 1 : -1;
358 }
359
360 /**
361 * Compares the specified <code>Object</code> to this <code>Double</code>
362 * if and only if the <code>Object</code> is an instanceof
363 * <code>Double</code>.
364 *
365 * @param o the Object to compare to.
366 * @return 0 if the <code>Double</code>s are the same, &lt; 0 if this
367 * <code>Double</code> is less than the <code>Double</code> in
368 * in question, or &gt; 0 if it is greater.
369 * @throws ClassCastException if the argument is not a <code>Double</code>
370 */
371 public int compareTo (Object o)
372 {
373 return compareTo ((Double) o);
374 }
375
376 /**
377 * Convert the <code>double</code> to a <code>String</code>.
378 * <P>
379 *
380 * Floating-point string representation is fairly complex: here is a
381 * rundown of the possible values. "<CODE>[-]</CODE>" indicates that a
382 * negative sign will be printed if the value (or exponent) is negative.
383 * "<CODE>&lt;number&gt;</CODE>" means a string of digits (0-9).
384 * "<CODE>&lt;digit&gt;</CODE>" means a single digit (0-9).
385 * <P>
386 *
387 * <TABLE BORDER=1>
388 * <TR><TH>Value of Float</TH><TH>String Representation</TH></TR>
389 * <TR>
390 * <TD>[+-] 0</TD>
391 * <TD>[<CODE>-</CODE>]<CODE>0.0</CODE></TD>
392 * </TR>
393 * <TR>
394 * <TD>Between [+-] 10<SUP>-3</SUP> and 10<SUP>7</SUP></TD>
395 * <TD><CODE>[-]number.number</CODE></TD>
396 * </TR>
397 * <TR>
398 * <TD>Other numeric value</TD>
399 * <TD><CODE>[-]&lt;digit&gt;.&lt;number&gt;E[-]&lt;number&gt;</CODE></TD>
400 * </TR>
401 * <TR>
402 * <TD>[+-] infinity</TD>
403 * <TD><CODE>[-]Infinity</CODE></TD>
404 * </TR>
405 * <TR>
406 * <TD>NaN</TD>
407 * <TD><CODE>NaN</CODE></TD>
408 * </TR>
409 * </TABLE>
410 *
411 * Yes, negative zero <EM>is</EM> a possible value. Note that there is
412 * <EM>always</EM> a <CODE>.</CODE> and at least one digit printed after
413 * it: even if the number is 3, it will be printed as <CODE>3.0</CODE>.
414 * After the ".", all digits will be printed except trailing zeros. No
415 * truncation or rounding is done by this function.
416 *
417 *
418 * @XXX specify where we are not in accord with the spec.
419 *
420 * @param d the <code>double</code> to convert
421 * @return the <code>String</code> representing the <code>double</code>.
422 */
423 public static String toString (double d)
424 {
425 return toString (d, false);
426 }
427
428 static native String toString (double d, boolean isFloat);
429
430 /**
431 * Return the long bits of the specified <code>double</code>.
432 * The result of this function can be used as the argument to
433 * <code>Double.longBitsToDouble(long)</code> to obtain the
434 * original <code>double</code> value.
435 *
436 * @param value the <code>double</code> to convert
437 * @return the bits of the <code>double</code>.
438 */
439 public static native long doubleToLongBits (double value);
440
441 /**
442 * Return the long bits of the specified <code>double</code>.
443 * The result of this function can be used as the argument to
444 * <code>Double.longBitsToDouble(long)</code> to obtain the
445 * original <code>double</code> value. This method differs from
446 * <code>doubleToLongBits</code> in that it does not collapse
447 * NaN values.
448 *
449 * @param value the <code>double</code> to convert
450 * @return the bits of the <code>double</code>.
451 */
452 public static native long doubleToRawLongBits (double value);
453
454 /**
455 * Return the <code>double</code> represented by the long
456 * bits specified.
457 *
458 * @param bits the long bits representing a <code>double</code>
459 * @return the <code>double</code> represented by the bits.
460 */
461 public static native double longBitsToDouble (long bits);
462
463 /**
464 * Parse the specified <code>String</code> as a <code>double</code>.
465 *
466 * The number is really read as <em>n * 10<sup>exponent</sup></em>. The
467 * first number is <em>n</em>, and if there is an "<code>E</code>"
468 * ("<code>e</code>" is also acceptable), then the integer after that is
469 * the exponent.
470 * <P>
471 * Here are the possible forms the number can take:
472 * <BR>
473 * <TABLE BORDER=1>
474 * <TR><TH>Form</TH><TH>Examples</TH></TR>
475 * <TR><TD><CODE>[+-]&lt;number&gt;[.]</CODE></TD><TD>345., -10, 12</TD></TR>
476 * <TR><TD><CODE>[+-]&lt;number&gt;.&lt;number&gt;</CODE></TD><TD>40.2, 80.00, -12.30</TD></TR>
477 * <TR><TD><CODE>[+-]&lt;number&gt;[.]E[+-]&lt;number&gt;</CODE></TD><TD>80E12, -12e+7, 4.E-123</TD></TR>
478 * <TR><TD><CODE>[+-]&lt;number&gt;.&lt;number&gt;E[+-]&lt;number&gt;</CODE></TD><TD>6.02e-22, -40.2E+6, 12.3e9</TD></TR>
479 * </TABLE>
480 *
481 * "<code>[+-]</code>" means either a plus or minus sign may go there, or
482 * neither, in which case + is assumed.
483 * <BR>
484 * "<code>[.]</code>" means a dot may be placed here, but is optional.
485 * <BR>
486 * "<code>&lt;number&gt;</code>" means a string of digits (0-9), basically
487 * an integer. "<code>&lt;number&gt;.&lt;number&gt;</code>" is basically
488 * a real number, a floating-point value.
489 * <P>
490 *
491 * Remember that a <code>double</code> has a limited range. If the
492 * number you specify is greater than <code>Double.MAX_VALUE</code> or less
493 * than <code>-Double.MAX_VALUE</code>, it will be set at
494 * <code>Double.POSITIVE_INFINITY</code> or
495 * <code>Double.NEGATIVE_INFINITY</code>, respectively.
496 * <P>
497 * Note also that <code>double</code> does not have perfect precision. Many
498 * numbers cannot be precisely represented. The number you specify
499 * will be rounded to the nearest representable value.
500 * <code>Double.MIN_VALUE</code> is the margin of error for
501 * <code>double</code> values.
502 * <P>
503 * If an unexpected character is found in the <code>String</code>, a
504 * <code>NumberFormatException</code> will be thrown. Spaces are not
505 * allowed, and will cause the same exception.
506 *
507 * @XXX specify where/how we are not in accord with the spec.
508 *
509 * @param str the <code>String</code> to convert
510 * @return the value of the <code>String</code> as a <code>double</code>.
511 * @exception NumberFormatException when the string cannot be parsed to a
512 * <code>double</code>.
513 * @exception NullPointerException when the string is null.
514 * @see #MIN_VALUE
515 * @see #MAX_VALUE
516 * @see #POSITIVE_INFINITY
517 * @see #NEGATIVE_INFINITY
518 * @since 1.2
519 */
520 public static native double parseDouble (String s)
521 throws NumberFormatException;
522
523 /**
524 * Initialize JNI cache. This method is called only by the
525 * static initializer when using JNI.
526 */
527 private static native void initIDs ();
528}
Note: See TracBrowser for help on using the repository browser.