7 /**
8 * <b>Java integer implementation of 63-bit precision floating point.</b>
9 * <br><i>Version 1.13</i>
10 *
11 * <p>Copyright 2003-2009 Roar Lauritzsen <roarl@pvv.org>
12 *
13 * <blockquote>
14 *
15 * <p>This library is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at your option)
18 * any later version.
19 *
20 * <p>This library is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
24 *
25 * <p>The following link provides a copy of the GNU General Public License:
26 * <br> <a
27 * href="http://www.gnu.org/licenses/gpl.txt">http://www.gnu.org/licenses/gpl.txt</a>
28 * <br>If you are unable to obtain the copy from this address, write to the
29 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 * 02111-1307 USA
31 *
32 * </blockquote>
33 *
34 * <p><b>General notes</b>
35 * <ul>
36 *
37 * <li><code>Real</code> objects are not immutable, like Java
38 * <code>Double</code> or <code>BigDecimal</code>. This means that you
39 * should not think of a <code>Real</code> object as a "number", but rather
40 * as a "register holding a number". This design choice is done to encourage
41 * object reuse and limit garbage production for more efficient execution on
42 * e.g. a limited MIDP device. The design choice is reflected in the API,
43 * where an operation like {@link #add(Real) add} does not return a new
44 * object containing the result (as with {@link
45 * java.math.BigDecimal#add(java.math.BigDecimal) BigDecimal}), but rather
46 * adds the argument to the object itself, and returns nothing.
47 *
48 * <li>This library implements infinities and NaN (Not-a-Number) following
49 * the IEEE 754 logic. If an operation produces a result larger (in
50 * magnitude) than the largest representable number, a value representing
51 * positive or negative infinity is generated. If an operation produces a
52 * result smaller than the smallest representable number, a positive or
53 * negative zero is generated. If an operation is undefined, a NaN value is
54 * produced. Abnormal numbers are often fine to use in further
55 * calculations. In most cases where the final result would be meaningful,
56 * abnormal numbers accomplish this, e.g. atan(1/0)=π/2. In most cases
57 * where the final result is not meaningful, a NaN will be produced.
58 * <i>No exception is ever (deliberately) thrown.</i>
59 *
60 * <li>Error bounds listed under <a href="#method_detail">Method Detail</a>
61 * are calculated using William Rossi's <a
62 * href="http://dfp.sourceforge.net/">rossi.dfp.dfp</a> at 40 decimal digits
63 * accuracy. Error bounds are for "typical arguments" and may increase when
64 * results approach zero or
65 * infinity. The abbreviation {@link Math#ulp(double) ULP} means Unit in the
66 * Last Place. An error bound of ½ ULP means that the result is correctly
67 * rounded. The relative execution time listed under each method is the
68 * average from running on SonyEricsson T610 (R3C), K700i, and Nokia 6230i.
69 *
70 * <li>The library is not thread-safe. Static <code>Real</code> objects are
71 * used extensively as temporary values to avoid garbage production and the
72 * overhead of <code>new</code>. To make the library thread-safe, references
73 * to all these static objects must be replaced with code that instead
74 * allocates new <code>Real</code> objects in their place.
75 *
76 * <li>There is one bug that occurs again and again and is really difficult
77 * to debug. Although the pre-calculated constants are declared <code>static
78 * final</code>, Java cannot really protect the contents of the objects in
79 * the same way as <code>const</code>s are protected in C/C++. Consequently,
80 * you can accidentally change these values if you send them into a function
81 * that modifies its arguments. If you were to modify {@link #ONE Real.ONE}
82 * for instance, many of the succeeding calculations would be wrong because
83 * the same variable is used extensively in the internal calculations of
84 * Real.java.
85 *
86 * </ul>
87 */
88 public final class Real
89 {
90 /**
91 * The mantissa of a <code>Real</code>. <i>To maintain numbers in a
92 * normalized state and to preserve the integrity of abnormal numbers, it
93 * is discouraged to modify the inner representation of a
94 * <code>Real</code> directly.</i>
95 *
96 * <p>The number represented by a <code>Real</code> equals:<br>
97 * -1<sup>sign</sup> · mantissa · 2<sup>-62</sup> · 2<sup>exponent-0x40000000</sup>
98 *
99 * <p>The normalized mantissa of a finite <code>Real</code> must be
100 * between <code>0x4000000000000000L</code> and
101 * <code>0x7fffffffffffffffL</code>. Using a denormalized
102 * <code>Real</code> in <u>any</u> operation other than {@link
103 * #normalize()} may produce undefined results. The mantissa of zero and
104 * of an infinite value is <code>0x0000000000000000L</code>.
105 *
106 * <p>The mantissa of a NaN is any nonzero value. However, it is
107 * recommended to use the value <code>0x4000000000000000L</code>. Any
108 * other values are reserved for future extensions.
109 */
111 /**
112 * The exponent of a <code>Real</code>. <i>To maintain numbers in a
113 * normalized state and to preserve the integrity of abnormal numbers, it
114 * is discouraged to modify the inner representation of a
115 * <code>Real</code> directly.</i>
116 *
117 * <p>The exponent of a finite <code>Real</code> must be between
118 * <code>0x00000000</code> and <code>0x7fffffff</code>. The exponent of
119 * zero <code>0x00000000</code>.
120 *
121 * <p>The exponent of an infinite value and of a NaN is any negative
122 * value. However, it is recommended to use the value
123 * <code>0x80000000</code>. Any other values are reserved for future
124 * extensions.
125 */
127 /**
128 * The sign of a <code>Real</code>. <i>To maintain numbers in a normalized
129 * state and to preserve the integrity of abnormal numbers, it is
130 * discouraged to modify the inner representation of a <code>Real</code>
131 * directly.</i>
132 *
133 * <p>The sign of a finite, zero or infinite <code>Real</code> is 0 for
134 * positive values and 1 for negative values. Any other values may produce
135 * undefined results.
136 *
137 * <p>The sign of a NaN is ignored. However, it is recommended to use the
138 * value <code>0</code>. Any other values are reserved for future
139 * extensions.
140 */
142 /**
143 * Set to <code>false</code> during numerical algorithms to favor accuracy
144 * over prettyness. This flag is initially set to <code>true</code>.
145 *
146 * <p>The flag controls the operation of a subtraction of two
147 * almost-identical numbers that differ only in the last three bits of the
148 * mantissa. With this flag enabled, the result of such a subtraction is
149 * rounded down to zero. Probabilistically, this is the correct course of
150 * action in an overwhelmingly large percentage of calculations.
151 * However, certain numerical algorithms such as differentiation depend
152 * on keeping maximum accuracy during subtraction.
153 *
154 * <p>Note, that because of <code>magicRounding</code>,
155 * <code>a.sub(b)</code> may produce zero even though
156 * <code>a.equalTo(b)</code> returns <code>false</code>. This must be
157 * considered e.g. when trying to avoid division by zero.
158 */
160 /**
161 * A <code>Real</code> constant holding the exact value of 0. Among other
162 * uses, this value is used as a result when a positive underflow occurs.
163 */
165 /**
166 * A <code>Real</code> constant holding the exact value of 1.
167 */
169 /**
170 * A <code>Real</code> constant holding the exact value of 2.
171 */
173 /**
174 * A <code>Real</code> constant holding the exact value of 3.
175 */
177 /**
178 * A <code>Real</code> constant holding the exact value of 5.
179 */
181 /**
182 * A <code>Real</code> constant holding the exact value of 10.
183 */
185 /**
186 * A <code>Real</code> constant holding the exact value of 100.
187 */
189 /**
190 * A <code>Real</code> constant holding the exact value of 1/2.
191 */
193 /**
194 * A <code>Real</code> constant that is closer than any other to 1/3.
195 */
197 /**
198 * A <code>Real</code> constant that is closer than any other to 1/10.
199 */
201 /**
202 * A <code>Real</code> constant that is closer than any other to 1/100.
203 */
205 /**
206 * A <code>Real</code> constant that is closer than any other to the
207 * square root of 2.
208 */
210 /**
211 * A <code>Real</code> constant that is closer than any other to the
212 * square root of 1/2.
213 */
215 /**
216 * A <code>Real</code> constant that is closer than any other to 2π.
217 */
219 /**
220 * A <code>Real</code> constant that is closer than any other to π, the
221 * ratio of the circumference of a circle to its diameter.
222 */
224 /**
225 * A <code>Real</code> constant that is closer than any other to π/2.
226 */
228 /**
229 * A <code>Real</code> constant that is closer than any other to π/4.
230 */
232 /**
233 * A <code>Real</code> constant that is closer than any other to π/8.
234 */
236 /**
237 * A <code>Real</code> constant that is closer than any other to <i>e</i>,
238 * the base of the natural logarithms.
239 */
241 /**
242 * A <code>Real</code> constant that is closer than any other to the
243 * natural logarithm of 2.
244 */
246 /**
247 * A <code>Real</code> constant that is closer than any other to the
248 * natural logarithm of 10.
249 */
251 /**
252 * A <code>Real</code> constant that is closer than any other to the
253 * base-2 logarithm of <i>e</i>.
254 */
256 /**
257 * A <code>Real</code> constant that is closer than any other to the
258 * base-10 logarithm of <i>e</i>.
259 */
261 /**
262 * A <code>Real</code> constant holding the maximum non-infinite positive
263 * number = 4.197e323228496.
264 */
266 /**
267 * A <code>Real</code> constant holding the minimum non-zero positive
268 * number = 2.383e-323228497.
269 */
271 /**
272 * A <code>Real</code> constant holding the value of NaN (not-a-number).
273 * This value is always used as a result to signal an invalid operation.
274 */
276 /**
277 * A <code>Real</code> constant holding the value of positive infinity.
278 * This value is always used as a result to signal a positive overflow.
279 */
281 /**
282 * A <code>Real</code> constant holding the value of negative infinity.
283 * This value is always used as a result to signal a negative overflow.
284 */
286 /**
287 * A <code>Real</code> constant holding the value of negative zero. This
288 * value is used as a result e.g. when a negative underflow occurs.
289 */
291 /**
292 * A <code>Real</code> constant holding the exact value of -1.
293 */
299 /**
300 * Creates a new <code>Real</code> with a value of zero.
301 */
303 }
304 /**
305 * Creates a new <code>Real</code>, assigning the value of another
306 * <code>Real</code>. See {@link #assign(Real)}.
307 *
308 * @param a the <code>Real</code> to assign.
309 */
312 }
313 /**
314 * Creates a new <code>Real</code>, assigning the value of an integer. See
315 * {@link #assign(int)}.
316 *
317 * @param a the <code>int</code> to assign.
318 */
321 }
322 /**
323 * Creates a new <code>Real</code>, assigning the value of a long
324 * integer. See {@link #assign(long)}.
325 *
326 * @param a the <code>long</code> to assign.
327 */
330 }
331 /**
332 * Creates a new <code>Real</code>, assigning the value encoded in a
333 * <code>String</code> using base-10. See {@link #assign(String)}.
334 *
335 * @param a the <code>String</code> to assign.
336 */
339 }
340 /**
341 * Creates a new <code>Real</code>, assigning the value encoded in a
342 * <code>String</code> using the specified number base. See {@link
343 * #assign(String,int)}.
344 *
345 * @param a the <code>String</code> to assign.
346 * @param base the number base of <code>a</code>. Valid base values are 2,
347 * 8, 10 and 16.
348 */
351 }
352 /**
353 * Creates a new <code>Real</code>, assigning a value by directly setting
354 * the fields of the internal representation. The arguments must represent
355 * a valid, normalized <code>Real</code>. This is the fastest way of
356 * creating a constant value. See {@link #assign(int,int,long)}.
357 *
358 * @param s {@link #sign} bit, 0 for positive sign, 1 for negative sign
359 * @param e {@link #exponent}
360 * @param m {@link #mantissa}
361 */
364 }
365 /**
366 * Creates a new <code>Real</code>, assigning the value previously encoded
367 * into twelve consecutive bytes in a byte array using {@link
368 * #toBytes(byte[],int) toBytes}. See {@link #assign(byte[],int)}.
369 *
370 * @param data byte array to decode into this <code>Real</code>.
371 * @param offset offset to start encoding from. The bytes
372 * <code>data[offset]...data[offset+11]</code> will be
373 * read.
374 */
377 }
378 /**
379 * Assigns this <code>Real</code> the value of another <code>Real</code>.
380 *
381 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
382 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
383 * Equivalent </i><code>double</code><i> code:</i></td><td>
384 * <code>this = a;</code>
385 * </td></tr><tr><td><i>Error bound:</i></td><td>
386 * 0 ULPs
387 * </td></tr><tr><td><i>
388 * Execution time relative to add:
389 * </i></td><td>
390 * 0.3
391 * </td></tr></table>
392 *
393 * @param a the <code>Real</code> to assign.
394 */
399 }
403 }
404 /**
405 * Assigns this <code>Real</code> the value of an integer.
406 * All integer values can be represented without loss of accuracy.
407 *
408 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
409 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
410 * Equivalent </i><code>double</code><i> code:</i></td><td>
411 * <code>this = (double)a;</code>
412 * </td></tr><tr><td><i>Error bound:</i></td><td>
413 * 0 ULPs
414 * </td></tr><tr><td><i>
415 * Execution time relative to add:
416 * </i></td><td>
417 * 0.6
418 * </td></tr></table>
419 *
420 * @param a the <code>int</code> to assign.
421 */
426 }
431 }
432 // Normalize int
437 }
438 /**
439 * Assigns this <code>Real</code> the value of a signed long integer.
440 * All long values can be represented without loss of accuracy.
441 *
442 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
443 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
444 * Equivalent </i><code>double</code><i> code:</i></td><td>
445 * <code>this = (double)a;</code>
446 * </td></tr><tr><td><i>Error bound:</i></td><td>
447 * 0 ULPs
448 * </td></tr><tr><td><i>
449 * Execution time relative to add:
450 * </i></td><td>
451 * 1.0
452 * </td></tr></table>
453 *
454 * @param a the <code>long</code> to assign.
455 */
461 }
465 }
466 /**
467 * Assigns this <code>Real</code> a value encoded in a <code>String</code>
468 * using base-10, as specified in {@link #assign(String,int)}.
469 *
470 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
471 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
472 * Equivalent </i><code>double</code><i> code:</i></td><td>
473 * <code>this = Double.{@link Double#valueOf(String) valueOf}(a);</code>
474 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
475 * ½-1 ULPs
476 * </td></tr><tr><td><i>
477 * Execution time relative to add:
478 * </i></td><td>
479 * 80
480 * </td></tr></table>
481 *
482 * @param a the <code>String</code> to assign.
483 */
486 }
487 /**
488 * Assigns this <code>Real</code> a value encoded in a <code>String</code>
489 * using the specified number base. The string is parsed as follows:
490 *
491 * <ul>
492 * <li>If the string is <code>null</code> or an empty string, zero is
493 * assigned.
494 * <li>Leading spaces are ignored.
495 * <li>An optional sign, '+', '-' or '/', where '/' precedes a negative
496 * two's-complement number, reading: "an infinite number of 1-bits
497 * preceding the number".
498 * <li>Optional digits preceding the radix, in the specified base.
499 * <ul>
500 * <li>In base-2, allowed digits are '01'.
501 * <li>In base-8, allowed digits are '01234567'.
502 * <li>In base-10, allowed digits are '0123456789'.
503 * <li>In base-16, allowed digits are '0123456789ABCDEF'.
504 </ul>
505 * <li>An optional radix character, '.' or ','.
506 * <li>Optional digits following the radix.
507 * <li>The following spaces are ignored.
508 * <li>An optional exponent indicator, 'e'. If not base-16, or after a
509 * space, 'E' is also accepted.
510 * <li>An optional sign, '+' or '-'.
511 * <li>Optional exponent digits <i><b>in base-10</b></i>.
512 * </ul>
513 *
514 * <p><i>Valid examples:</i><br>
515 * base-2: <code>"-.110010101e+5"</code><br>
516 * base-8: <code>"+5462E-99"</code><br>
517 * base-10: <code>" 3,1415927"</code><br>
518 * base-16: <code>"/FFF800C.CCCE e64"</code>
519 *
520 * <p>The number is parsed until the end of the string or an unknown
521 * character is encountered, then silently returns even if the whole
522 * string has not been parsed. Please note that specifying an
523 * excessive number of digits in base-10 may in fact decrease the
524 * accuracy of the result because of the extra multiplications performed.
525 *
526 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
527 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
528 * Equivalent </i><code>double</code><i> code:</i></td>
529 * <td colspan="2">
530 * <code>this = Double.{@link Double#valueOf(String) valueOf}(a);
531 * // Works only for base-10</code>
532 * </td></tr><tr><td valign="top" rowspan="2"><i>
533 * Approximate error bound:</i>
534 * </td><td width="1%">base-10</td><td>
535 * ½-1 ULPs
536 * </td></tr><tr><td>2/8/16</td><td>
537 * ½ ULPs
538 * </td></tr><tr><td valign="top" rowspan="4"><i>
539 * Execution time relative to add: </i>
540 * </td><td width="1%">base-2</td><td>
541 * 54
542 * </td></tr><tr><td>base-8</td><td>
543 * 60
544 * </td></tr><tr><td>base-10</td><td>
545 * 80
546 * </td></tr><tr><td>base-16 </td><td>
547 * 60
548 * </td></tr></table>
549 *
550 * @param a the <code>String</code> to assign.
551 * @param base the number base of <code>a</code>. Valid base values are
552 * 2, 8, 10 and 16.
553 */
558 }
560 }
561 /**
562 * Assigns this <code>Real</code> a value by directly setting the fields
563 * of the internal representation. The arguments must represent a valid,
564 * normalized <code>Real</code>. This is the fastest way of assigning a
565 * constant value.
566 *
567 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
568 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
569 * Equivalent </i><code>double</code><i> code:</i></td><td>
570 * <code>this = (1-2*s) * m *
571 * Math.{@link Math#pow(double,double)
572 * pow}(2.0,e-0x400000e3);</code>
573 * </td></tr><tr><td><i>Error bound:</i></td><td>
574 * 0 ULPs
575 * </td></tr><tr><td><i>
576 * Execution time relative to add:
577 * </i></td><td>
578 * 0.3
579 * </td></tr></table>
580 *
581 * @param s {@link #sign} bit, 0 for positive sign, 1 for negative sign
582 * @param e {@link #exponent}
583 * @param m {@link #mantissa}
584 */
589 }
590 /**
591 * Assigns this <code>Real</code> a value previously encoded into into
592 * twelve consecutive bytes in a byte array using {@link
593 * #toBytes(byte[],int) toBytes}.
594 *
595 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
596 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
597 * Error bound:</i></td><td>
598 * 0 ULPs
599 * </td></tr><tr><td><i>
600 * Execution time relative to add:
601 * </i></td><td>
602 * 1.2
603 * </td></tr></table>
604 *
605 * @param data byte array to decode into this <code>Real</code>.
606 * @param offset offset to start encoding from. The bytes
607 * <code>data[offset]...data[offset+11]</code> will be
608 * read.
609 */
624 }
625 /**
626 * Encodes an accurate representation of this <code>Real</code> value into
627 * twelve consecutive bytes in a byte array. Can be decoded using {@link
628 * #assign(byte[],int)}.
629 *
630 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
631 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
632 * Execution time relative to add:
633 * </i></td><td>
634 * 1.2
635 * </td></tr></table>
636 *
637 * @param data byte array to save this <code>Real</code> in.
638 * @param offset offset to start encoding to. The bytes
639 * <code>data[offset]...data[offset+11]</code> will be
640 * written.
641 */
655 }
656 /**
657 * Assigns this <code>Real</code> the value corresponding to a given bit
658 * representation. The argument is considered to be a representation of a
659 * floating-point value according to the IEEE 754 floating-point "single
660 * format" bit layout.
661 *
662 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
663 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
664 * Equivalent </i><code>float</code><i> code:</i></td><td>
665 * <code>this = Float.{@link Float#intBitsToFloat(int)
666 * intBitsToFloat}(bits);</code>
667 * </td></tr><tr><td><i>Error bound:</i></td><td>
668 * 0 ULPs
669 * </td></tr><tr><td><i>
670 * Execution time relative to add:
671 * </i></td><td>
672 * 0.6
673 * </td></tr></table>
674 *
675 * @param bits a <code>float</code> value encoded in an <code>int</code>.
676 */
684 // Degenerate small float
688 }
690 // Normal IEEE 754 float
694 }
697 else
699 }
700 /**
701 * Assigns this <code>Real</code> the value corresponding to a given bit
702 * representation. The argument is considered to be a representation of a
703 * floating-point value according to the IEEE 754 floating-point "double
704 * format" bit layout.
705 *
706 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
707 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
708 * Equivalent </i><code>double</code><i> code:</i></td><td>
709 * <code>this = Double.{@link Double#longBitsToDouble(long)
710 * longBitsToDouble}(bits);</code>
711 * </td></tr><tr><td><i>Error bound:</i></td><td>
712 * 0 ULPs
713 * </td></tr><tr><td><i>
714 * Execution time relative to add:
715 * </i></td><td>
716 * 0.6
717 * </td></tr></table>
718 *
719 * @param bits a <code>double</code> value encoded in a <code>long</code>.
720 */
728 // Degenerate small float
732 }
734 // Normal IEEE 754 float
738 }
741 else
743 }
744 /**
745 * Returns a representation of this <code>Real</code> according to the
746 * IEEE 754 floating-point "single format" bit layout.
747 *
748 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
749 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
750 * Equivalent </i><code>float</code><i> code:</i></td><td>
751 * <code>Float.{@link Float#floatToIntBits(float)
752 * floatToIntBits}(this)</code>
753 * </td></tr><tr><td><i>
754 * Execution time relative to add:
755 * </i></td><td>
756 * 0.7
757 * </td></tr></table>
758 *
759 * @return the bits that represent the floating-point number.
760 */
766 // Round properly!
770 e++;
773 }
780 // Normal IEEE 754 float
782 }
783 /**
784 * Returns a representation of this <code>Real</code> according to the
785 * IEEE 754 floating-point "double format" bit layout.
786 *
787 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
788 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
789 * Equivalent </i><code>double</code><i> code:</i></td><td>
790 * <code>Double.{@link Double#doubleToLongBits(double)
791 * doubleToLongBits}(this)</code>
792 * </td></tr><tr><td><i>
793 * Execution time relative to add:
794 * </i></td><td>
795 * 0.7
796 * </td></tr></table>
797 *
798 * @return the bits that represent the floating-point number.
799 */
805 // Round properly!
809 e++;
812 }
819 // Normal IEEE 754 double
821 }
822 /**
823 * Makes this <code>Real</code> the value of positive zero.
824 *
825 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
826 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
827 * Equivalent </i><code>double</code><i> code:</i></td><td>
828 * <code>this = 0;</code>
829 * </td></tr><tr><td><i>
830 * Execution time relative to add:
831 * </i></td><td>
832 * 0.2
833 * </td></tr></table>
834 */
839 }
840 /**
841 * Makes this <code>Real</code> the value of zero with the specified sign.
842 *
843 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
844 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
845 * Equivalent </i><code>double</code><i> code:</i></td><td>
846 * <code>this = 0.0 * (1-2*s);</code>
847 * </td></tr><tr><td><i>
848 * Execution time relative to add:
849 * </i></td><td>
850 * 0.2
851 * </td></tr></table>
852 *
853 * @param s sign bit, 0 to make a positive zero, 1 to make a negative zero
854 */
859 }
860 /**
861 * Makes this <code>Real</code> the value of infinity with the specified
862 * sign.
863 *
864 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
865 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
866 * Equivalent </i><code>double</code><i> code:</i></td><td>
867 * <code>this = Double.{@link Double#POSITIVE_INFINITY POSITIVE_INFINITY}
868 * * (1-2*s);</code>
869 * </td></tr><tr><td><i>
870 * Execution time relative to add:
871 * </i></td><td>
872 * 0.3
873 * </td></tr></table>
874 *
875 * @param s sign bit, 0 to make positive infinity, 1 to make negative
876 * infinity
877 */
882 }
883 /**
884 * Makes this <code>Real</code> the value of Not-a-Number (NaN).
885 *
886 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
887 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
888 * Equivalent </i><code>double</code><i> code:</i></td><td>
889 * <code>this = Double.{@link Double#NaN NaN};</code>
890 * </td></tr><tr><td><i>
891 * Execution time relative to add:
892 * </i></td><td>
893 * 0.3
894 * </td></tr></table>
895 */
900 }
901 /**
902 * Returns <code>true</code> if the value of this <code>Real</code> is
903 * zero, <code>false</code> otherwise.
904 *
905 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
906 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
907 * Equivalent </i><code>double</code><i> code:</i></td><td>
908 * <code>(this == 0)</code>
909 * </td></tr><tr><td><i>
910 * Execution time relative to add:
911 * </i></td><td>
912 * 0.3
913 * </td></tr></table>
914 *
915 * @return <code>true</code> if the value represented by this object is
916 * zero, <code>false</code> otherwise.
917 */
920 }
921 /**
922 * Returns <code>true</code> if the value of this <code>Real</code> is
923 * infinite, <code>false</code> otherwise.
924 *
925 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
926 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
927 * Equivalent </i><code>double</code><i> code:</i></td><td>
928 * <code>Double.{@link Double#isInfinite(double) isInfinite}(this)</code>
929 * </td></tr><tr><td><i>
930 * Execution time relative to add:
931 * </i></td><td>
932 * 0.3
933 * </td></tr></table>
934 *
935 * @return <code>true</code> if the value represented by this object is
936 * infinite, <code>false</code> if it is finite or NaN.
937 */
940 }
941 /**
942 * Returns <code>true</code> if the value of this <code>Real</code> is
943 * Not-a-Number (NaN), <code>false</code> otherwise.
944 *
945 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
946 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
947 * Equivalent </i><code>double</code><i> code:</i></td><td>
948 * <code>Double.{@link Double#isNaN(double) isNaN}(this)</code>
949 * </td></tr><tr><td><i>
950 * Execution time relative to add:
951 * </i></td><td>
952 * 0.3
953 * </td></tr></table>
954 *
955 * @return <code>true</code> if the value represented by this object is
956 * NaN, <code>false</code> otherwise.
957 */
960 }
961 /**
962 * Returns <code>true</code> if the value of this <code>Real</code> is
963 * finite, <code>false</code> otherwise.
964 *
965 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
966 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
967 * Equivalent </i><code>double</code><i> code:</i></td><td>
968 * <code>(!Double.{@link Double#isNaN(double) isNaN}(this) &&
969 * !Double.{@link Double#isInfinite(double)
970 * isInfinite}(this))</code>
971 * </td></tr><tr><td><i>
972 * Execution time relative to add:
973 * </i></td><td>
974 * 0.3
975 * </td></tr></table>
976 *
977 * @return <code>true</code> if the value represented by this object is
978 * finite, <code>false</code> if it is infinite or NaN.
979 */
981 // That is, non-infinite and non-nan
983 }
984 /**
985 * Returns <code>true</code> if the value of this <code>Real</code> is
986 * finite and nonzero, <code>false</code> otherwise.
987 *
988 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
989 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
990 * Equivalent </i><code>double</code><i> code:</i></td><td>
991 * <code>(!Double.{@link Double#isNaN(double) isNaN}(this) &&
992 * !Double.{@link Double#isInfinite(double) isInfinite}(this) &&
993 * (this!=0))</code>
994 * </td></tr><tr><td><i>
995 * Execution time relative to add:
996 * </i></td><td>
997 * 0.3
998 * </td></tr></table>
999 *
1000 * @return <code>true</code> if the value represented by this object is
1001 * finite and nonzero, <code>false</code> if it is infinite, NaN or
1002 * zero.
1003 */
1005 // That is, non-infinite and non-nan and non-zero
1007 }
1008 /**
1009 * Returns <code>true</code> if the value of this <code>Real</code> is
1010 * negative, <code>false</code> otherwise.
1011 *
1012 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1013 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1014 * Equivalent </i><code>double</code><i> code:</i></td><td>
1015 * <code>(this < 0)</code>
1016 * </td></tr><tr><td><i>
1017 * Execution time relative to add:
1018 * </i></td><td>
1019 * 0.3
1020 * </td></tr></table>
1021 *
1022 * @return <code>true</code> if the value represented by this object
1023 * is negative, <code>false</code> if it is positive or NaN.
1024 */
1027 }
1028 /**
1029 * Calculates the absolute value.
1030 * Replaces the contents of this <code>Real</code> with the result.
1031 *
1032 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1033 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1034 * Equivalent </i><code>double</code><i> code:</i></td><td>
1035 * <code>this = Math.{@link Math#abs(double) abs}(this);</code>
1036 * </td></tr><tr><td><i>Error bound:</i></td><td>
1037 * 0 ULPs
1038 * </td></tr><tr><td><i>
1039 * Execution time relative to add:
1040 * </i></td><td>
1041 * 0.2
1042 * </td></tr></table>
1043 */
1046 }
1047 /**
1048 * Negates this <code>Real</code>.
1049 * Replaces the contents of this <code>Real</code> with the result.
1050 *
1051 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1052 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1053 * Equivalent </i><code>double</code><i> code:</i></td><td>
1054 * <code>this = -this;</code>
1055 * </td></tr><tr><td><i>Error bound:</i></td><td>
1056 * 0 ULPs
1057 * </td></tr><tr><td><i>
1058 * Execution time relative to add:
1059 * </i></td><td>
1060 * 0.2
1061 * </td></tr></table>
1062 */
1066 }
1067 /**
1068 * Copies the sign from <code>a</code>.
1069 * Replaces the contents of this <code>Real</code> with the result.
1070 *
1071 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1072 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1073 * Equivalent </i><code>double</code><i> code:</i></td><td>
1074 * <code>this = Math.{@link Math#abs(double)
1075 * abs}(this)*Math.{@link Math#signum(double) signum}(a);</code>
1076 * </td></tr><tr><td><i>Error bound:</i></td><td>
1077 * 0 ULPs
1078 * </td></tr><tr><td><i>
1079 * Execution time relative to add:
1080 * </i></td><td>
1081 * 0.2
1082 * </td></tr></table>
1083 *
1084 * @param a the <code>Real</code> to copy the sign from.
1085 */
1090 }
1092 }
1093 /**
1094 * Readjusts the mantissa of this <code>Real</code>. The exponent is
1095 * adjusted accordingly. This is necessary when the mantissa has been
1096 * {@link #mantissa modified directly} for some purpose and may be
1097 * denormalized. The normalized mantissa of a finite <code>Real</code>
1098 * must have bit 63 cleared and bit 62 set. Using a denormalized
1099 * <code>Real</code> in <u>any</u> other operation may produce undefined
1100 * results.
1101 *
1102 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1103 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1104 * Error bound:</i></td><td>
1105 * ½ ULPs
1106 * </td></tr><tr><td><i>
1107 * Execution time relative to add:
1108 * </i></td><td>
1109 * 0.7
1110 * </td></tr></table>
1111 */
1115 {
1125 }
1127 {
1129 exponent ++;
1132 exponent ++;
1133 }
1136 }
1138 {
1140 }
1141 }
1142 }
1143 /**
1144 * Readjusts the mantissa of a <code>Real</code> with extended
1145 * precision. The exponent is adjusted accordingly. This is necessary when
1146 * the mantissa has been {@link #mantissa modified directly} for some
1147 * purpose and may be denormalized. The normalized mantissa of a finite
1148 * <code>Real</code> must have bit 63 cleared and bit 62 set. Using a
1149 * denormalized <code>Real</code> in <u>any</u> other operation may
1150 * produce undefined results.
1151 *
1152 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1153 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1154 * Approximate error bound:</i></td><td>
1155 * 2<sup>-64</sup> ULPs (i.e. of a normal precision <code>Real</code>)
1156 * </td></tr><tr><td><i>
1157 * Execution time relative to add:
1158 * </i></td><td>
1159 * 0.7
1160 * </td></tr></table>
1161 *
1162 * @param extra the extra 64 bits of mantissa of this extended precision
1163 * <code>Real</code>.
1164 * @return the extra 64 bits of mantissa of the resulting extended
1165 * precision <code>Real</code>.
1166 */
1174 }
1181 }
1182 }
1186 exponent ++;
1190 }
1192 }
1206 }
1208 }
1209 /**
1210 * Rounds an extended precision <code>Real</code> to the nearest
1211 * <code>Real</code> of normal precision. Replaces the contents of this
1212 * <code>Real</code> with the result.
1213 *
1214 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1215 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1216 * Error bound:</i></td><td>
1217 * ½ ULPs
1218 * </td></tr><tr><td><i>
1219 * Execution time relative to add:
1220 * </i></td><td>
1221 * 1.0
1222 * </td></tr></table>
1223 *
1224 * @param extra the extra 64 bits of mantissa of this extended precision
1225 * <code>Real</code>.
1226 */
1230 }
1231 /**
1232 * Returns <code>true</code> if this Java object is the same
1233 * object as <code>a</code>. Since a <code>Real</code> should be
1234 * thought of as a "register holding a number", this method compares the
1235 * object references, not the contents of the two objects.
1236 * This is very different from {@link #equalTo(Real)}.
1237 *
1238 * @param a the object to compare to this.
1239 * @return <code>true</code> if this object is the same as <code>a</code>.
1240 */
1243 }
1245 // Compare of normal floats, zeros, but not nan or equal-signed inf
1260 }
1263 ((this.exponent < 0 && this.mantissa == 0) && (a.exponent < 0 && a.mantissa == 0) && sign == a.sign));
1264 }
1265 /**
1266 * Returns <code>true</code> if this <code>Real</code> is equal to
1267 * <code>a</code>.
1268 * If the numbers are incomparable, i.e. the values are infinities of
1269 * the same sign or any of them is NaN, <code>false</code> is always
1270 * returned. This method must not be confused with {@link #equals(Object)}.
1271 *
1272 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1273 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1274 * Equivalent </i><code>double</code><i> code:</i></td><td>
1275 * <code>(this == a)</code>
1276 * </td></tr><tr><td><i>
1277 * Execution time relative to add:
1278 * </i></td><td>
1279 * 1.0
1280 * </td></tr></table>
1281 *
1282 * @param a the <code>Real</code> to compare to this.
1283 * @return <code>true</code> if the value represented by this object is
1284 * equal to the value represented by <code>a</code>. <code>false</code>
1285 * otherwise, or if the numbers are incomparable.
1286 */
1291 }
1292 /**
1293 * Returns <code>true</code> if this <code>Real</code> is equal to
1294 * the integer <code>a</code>.
1295 *
1296 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1297 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1298 * Equivalent </i><code>double</code><i> code:</i></td><td>
1299 * <code>(this == a)</code>
1300 * </td></tr><tr><td><i>
1301 * Execution time relative to add:
1302 * </i></td><td>
1303 * 1.7
1304 * </td></tr></table>
1305 *
1306 * @param a the <code>int</code> to compare to this.
1307 * @return <code>true</code> if the value represented by this object is
1308 * equal to the integer <code>a</code>. <code>false</code>
1309 * otherwise.
1310 */
1314 }
1315 /**
1316 * Returns <code>true</code> if this <code>Real</code> is not equal to
1317 * <code>a</code>.
1318 * If the numbers are incomparable, i.e. the values are infinities of
1319 * the same sign or any of them is NaN, <code>false</code> is always
1320 * returned.
1321 * This distinguishes <code>notEqualTo(a)</code> from the expression
1322 * <code>!equalTo(a)</code>.
1323 *
1324 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1325 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1326 * Equivalent </i><code>double</code><i> code:</i></td><td>
1327 * <code>(this != a)</code>
1328 * </td></tr><tr><td><i>
1329 * Execution time relative to add:
1330 * </i></td><td>
1331 * 1.0
1332 * </td></tr></table>
1333 *
1334 * @param a the <code>Real</code> to compare to this.
1335 * @return <code>true</code> if the value represented by this object is not
1336 * equal to the value represented by <code>a</code>. <code>false</code>
1337 * otherwise, or if the numbers are incomparable.
1338 */
1343 }
1344 /**
1345 * Returns <code>true</code> if this <code>Real</code> is not equal to
1346 * the integer <code>a</code>.
1347 * If this <code>Real</code> is NaN, <code>false</code> is always
1348 * returned.
1349 * This distinguishes <code>notEqualTo(a)</code> from the expression
1350 * <code>!equalTo(a)</code>.
1351 *
1352 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1353 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1354 * Equivalent </i><code>double</code><i> code:</i></td><td>
1355 * <code>(this != a)</code>
1356 * </td></tr><tr><td><i>
1357 * Execution time relative to add:
1358 * </i></td><td>
1359 * 1.7
1360 * </td></tr></table>
1361 *
1362 * @param a the <code>int</code> to compare to this.
1363 * @return <code>true</code> if the value represented by this object is not
1364 * equal to the integer <code>a</code>. <code>false</code>
1365 * otherwise, or if this <code>Real</code> is NaN.
1366 */
1370 }
1371 /**
1372 * Returns <code>true</code> if this <code>Real</code> is less than
1373 * <code>a</code>.
1374 * If the numbers are incomparable, i.e. the values are infinities of
1375 * the same sign or any of them is NaN, <code>false</code> is always
1376 * returned.
1377 *
1378 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1379 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1380 * Equivalent </i><code>double</code><i> code:</i></td><td>
1381 * <code>(this < a)</code>
1382 * </td></tr><tr><td><i>
1383 * Execution time relative to add:
1384 * </i></td><td>
1385 * 1.0
1386 * </td></tr></table>
1387 *
1388 * @param a the <code>Real</code> to compare to this.
1389 * @return <code>true</code> if the value represented by this object is
1390 * less than the value represented by <code>a</code>.
1391 * <code>false</code> otherwise, or if the numbers are incomparable.
1392 */
1397 }
1398 /**
1399 * Returns <code>true</code> if this <code>Real</code> is less than
1400 * the integer <code>a</code>.
1401 * If this <code>Real</code> is NaN, <code>false</code> is always
1402 * returned.
1403 *
1404 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1405 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1406 * Equivalent </i><code>double</code><i> code:</i></td><td>
1407 * <code>(this < a)</code>
1408 * </td></tr><tr><td><i>
1409 * Execution time relative to add:
1410 * </i></td><td>
1411 * 1.7
1412 * </td></tr></table>
1413 *
1414 * @param a the <code>int</code> to compare to this.
1415 * @return <code>true</code> if the value represented by this object is
1416 * less than the integer <code>a</code>. <code>false</code> otherwise,
1417 * or if this <code>Real</code> is NaN.
1418 */
1422 }
1423 /**
1424 * Returns <code>true</code> if this <code>Real</code> is less than or
1425 * equal to <code>a</code>.
1426 * If the numbers are incomparable, i.e. the values are infinities of
1427 * the same sign or any of them is NaN, <code>false</code> is always
1428 * returned.
1429 *
1430 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1431 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1432 * Equivalent </i><code>double</code><i> code:</i></td><td>
1433 * <code>(this <= a)</code>
1434 * </td></tr><tr><td><i>
1435 * Execution time relative to add:
1436 * </i></td><td>
1437 * 1.0
1438 * </td></tr></table>
1439 *
1440 * @param a the <code>Real</code> to compare to this.
1441 * @return <code>true</code> if the value represented by this object is
1442 * less than or equal to the value represented by <code>a</code>.
1443 * <code>false</code> otherwise, or if the numbers are incomparable.
1444 */
1449 }
1450 /**
1451 * Returns <code>true</code> if this <code>Real</code> is less than or
1452 * equal to the integer <code>a</code>.
1453 * If this <code>Real</code> is NaN, <code>false</code> is always
1454 * returned.
1455 *
1456 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1457 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1458 * Equivalent </i><code>double</code><i> code:</i></td><td>
1459 * <code>(this <= a)</code>
1460 * </td></tr><tr><td><i>
1461 * Execution time relative to add:
1462 * </i></td><td>
1463 * 1.7
1464 * </td></tr></table>
1465 *
1466 * @param a the <code>int</code> to compare to this.
1467 * @return <code>true</code> if the value represented by this object is
1468 * less than or equal to the integer <code>a</code>. <code>false</code>
1469 * otherwise, or if this <code>Real</code> is NaN.
1470 */
1474 }
1475 /**
1476 * Returns <code>true</code> if this <code>Real</code> is greater than
1477 * <code>a</code>.
1478 * If the numbers are incomparable, i.e. the values are infinities of
1479 * the same sign or any of them is NaN, <code>false</code> is always
1480 * returned.
1481 *
1482 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1483 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1484 * Equivalent </i><code>double</code><i> code:</i></td><td>
1485 * <code>(this > a)</code>
1486 * </td></tr><tr><td><i>
1487 * Execution time relative to add:
1488 * </i></td><td>
1489 * 1.0
1490 * </td></tr></table>
1491 *
1492 * @param a the <code>Real</code> to compare to this.
1493 * @return <code>true</code> if the value represented by this object is
1494 * greater than the value represented by <code>a</code>.
1495 * <code>false</code> otherwise, or if the numbers are incomparable.
1496 */
1501 }
1502 /**
1503 * Returns <code>true</code> if this <code>Real</code> is greater than
1504 * the integer <code>a</code>.
1505 * If this <code>Real</code> is NaN, <code>false</code> is always
1506 * returned.
1507 *
1508 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1509 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1510 * Equivalent </i><code>double</code><i> code:</i></td><td>
1511 * <code>(this > a)</code>
1512 * </td></tr><tr><td><i>
1513 * Execution time relative to add:
1514 * </i></td><td>
1515 * 1.7
1516 * </td></tr></table>
1517 *
1518 * @param a the <code>int</code> to compare to this.
1519 * @return <code>true</code> if the value represented by this object is
1520 * greater than the integer <code>a</code>.
1521 * <code>false</code> otherwise, or if this <code>Real</code> is NaN.
1522 */
1526 }
1527 /**
1528 * Returns <code>true</code> if this <code>Real</code> is greater than
1529 * or equal to <code>a</code>.
1530 * If the numbers are incomparable, i.e. the values are infinities of
1531 * the same sign or any of them is NaN, <code>false</code> is always
1532 * returned.
1533 *
1534 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1535 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1536 * Equivalent </i><code>double</code><i> code:</i></td><td>
1537 * <code>(this >= a)</code>
1538 * </td></tr><tr><td><i>
1539 * Execution time relative to add:
1540 * </i></td><td>
1541 * 1.0
1542 * </td></tr></table>
1543 *
1544 * @param a the <code>Real</code> to compare to this.
1545 * @return <code>true</code> if the value represented by this object is
1546 * greater than or equal to the value represented by <code>a</code>.
1547 * <code>false</code> otherwise, or if the numbers are incomparable.
1548 */
1553 }
1554 /**
1555 * Returns <code>true</code> if this <code>Real</code> is greater than
1556 * or equal to the integer <code>a</code>.
1557 * If this <code>Real</code> is NaN, <code>false</code> is always
1558 * returned.
1559 *
1560 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1561 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1562 * Equivalent </i><code>double</code><i> code:</i></td><td>
1563 * <code>(this >= a)</code>
1564 * </td></tr><tr><td><i>
1565 * Execution time relative to add:
1566 * </i></td><td>
1567 * 1.7
1568 * </td></tr></table>
1569 *
1570 * @param a the <code>int</code> to compare to this.
1571 * @return <code>true</code> if the value represented by this object is
1572 * greater than or equal to the integer <code>a</code>.
1573 * <code>false</code> otherwise, or if this <code>Real</code> is NaN.
1574 */
1578 }
1579 /**
1580 * Returns <code>true</code> if the absolute value of this
1581 * <code>Real</code> is less than the absolute value of
1582 * <code>a</code>.
1583 * If the numbers are incomparable, i.e. the values are both infinite
1584 * or any of them is NaN, <code>false</code> is always returned.
1585 *
1586 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1587 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1588 * Equivalent </i><code>double</code><i> code:</i></td><td>
1589 * <code>(Math.{@link Math#abs(double) abs}(this) <
1590 * Math.{@link Math#abs(double) abs}(a))</code>
1591 * </td></tr><tr><td><i>
1592 * Execution time relative to add:
1593 * </i></td><td>
1594 * 0.5
1595 * </td></tr></table>
1596 *
1597 * @param a the <code>Real</code> to compare to this.
1598 * @return <code>true</code> if the absolute of the value represented by
1599 * this object is less than the absolute of the value represented by
1600 * <code>a</code>.
1601 * <code>false</code> otherwise, or if the numbers are incomparable.
1602 */
1604 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0) || (this.exponent < 0 && this.mantissa == 0))
1611 }
1612 /**
1613 * Multiplies this <code>Real</code> by 2 to the power of <code>n</code>.
1614 * Replaces the contents of this <code>Real</code> with the result.
1615 * This operation is faster than normal multiplication since it only
1616 * involves adding to the exponent.
1617 *
1618 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1619 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1620 * Equivalent </i><code>double</code><i> code:</i></td><td>
1621 * <code>this *= Math.{@link Math#pow(double,double) pow}(2.0,n);</code>
1622 * </td></tr><tr><td><i>Error bound:</i></td><td>
1623 * 0 ULPs
1624 * </td></tr><tr><td><i>
1625 * Execution time relative to add:
1626 * </i></td><td>
1627 * 0.3
1628 * </td></tr></table>
1629 *
1630 * @param n the integer argument.
1631 */
1639 else
1641 }
1642 }
1643 /**
1644 * Calculates the next representable neighbour of this <code>Real</code>
1645 * in the direction towards <code>a</code>.
1646 * Replaces the contents of this <code>Real</code> with the result.
1647 * If the two values are equal, nothing happens.
1648 *
1649 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1650 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1651 * Equivalent </i><code>double</code><i> code:</i></td><td>
1652 * <code>this += Math.{@link Math#ulp(double) ulp}(this)*Math.{@link
1653 * Math#signum(double) signum}(a-this);</code>
1654 * </td></tr><tr><td><i>Error bound:</i></td><td>
1655 * 0 ULPs
1656 * </td></tr><tr><td><i>
1657 * Execution time relative to add:
1658 * </i></td><td>
1659 * 0.8
1660 * </td></tr></table>
1661 *
1662 * @param a the <code>Real</code> argument.
1663 */
1668 }
1669 if ((this.exponent < 0 && this.mantissa == 0) && (a.exponent < 0 && a.mantissa == 0) && sign == a.sign)
1678 }
1683 }
1685 mantissa ++;
1689 exponent--;
1690 }
1691 mantissa --;
1692 }
1694 }
1695 /**
1696 * Calculates the largest (closest to positive infinity)
1697 * <code>Real</code> value that is less than or equal to this
1698 * <code>Real</code> and is equal to a mathematical integer.
1699 * Replaces the contents of this <code>Real</code> with the result.
1700 *
1701 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1702 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1703 * Equivalent </i><code>double</code><i> code:</i></td><td>
1704 * <code>this = Math.{@link Math#floor(double) floor}(this);</code>
1705 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
1706 * 0 ULPs
1707 * </td></tr><tr><td><i>
1708 * Execution time relative to add:
1709 * </i></td><td>
1710 * 0.5
1711 * </td></tr></table>
1712 */
1722 // sign unchanged!
1723 }
1725 }
1734 }
1735 /**
1736 * Calculates the smallest (closest to negative infinity)
1737 * <code>Real</code> value that is greater than or equal to this
1738 * <code>Real</code> and is equal to a mathematical integer.
1739 * Replaces the contents of this <code>Real</code> with the result.
1740 *
1741 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1742 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1743 * Equivalent </i><code>double</code><i> code:</i></td><td>
1744 * <code>this = Math.{@link Math#ceil(double) ceil}(this);</code>
1745 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
1746 * 0 ULPs
1747 * </td></tr><tr><td><i>
1748 * Execution time relative to add:
1749 * </i></td><td>
1750 * 1.8
1751 * </td></tr></table>
1752 */
1757 }
1758 /**
1759 * Rounds this <code>Real</code> value to the closest value that is equal
1760 * to a mathematical integer. If two <code>Real</code> values that are
1761 * mathematical integers are equally close, the result is the integer
1762 * value with the largest magnitude (positive or negative). Replaces the
1763 * contents of this <code>Real</code> with the result.
1764 *
1765 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1766 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1767 * Equivalent </i><code>double</code><i> code:</i></td><td>
1768 * <code>this = Math.{@link Math#rint(double) rint}(this);</code>
1769 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
1770 * 0 ULPs
1771 * </td></tr><tr><td><i>
1772 * Execution time relative to add:
1773 * </i></td><td>
1774 * 1.3
1775 * </td></tr></table>
1776 */
1783 }
1790 }
1791 /**
1792 * Truncates this <code>Real</code> value to the closest value towards
1793 * zero that is equal to a mathematical integer.
1794 * Replaces the contents of this <code>Real</code> with the result.
1795 *
1796 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1797 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1798 * Equivalent </i><code>double</code><i> code:</i></td><td>
1799 * <code>this = (double)((long)this);</code>
1800 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
1801 * 0 ULPs
1802 * </td></tr><tr><td><i>
1803 * Execution time relative to add:
1804 * </i></td><td>
1805 * 1.2
1806 * </td></tr></table>
1807 */
1814 }
1820 }
1821 /**
1822 * Calculates the fractional part of this <code>Real</code> by subtracting
1823 * the closest value towards zero that is equal to a mathematical integer.
1824 * Replaces the contents of this <code>Real</code> with the result.
1825 *
1826 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1827 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1828 * Equivalent </i><code>double</code><i> code:</i></td><td>
1829 * <code>this -= (double)((long)this);</code>
1830 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
1831 * 0 ULPs
1832 * </td></tr><tr><td><i>
1833 * Execution time relative to add:
1834 * </i></td><td>
1835 * 1.2
1836 * </td></tr></table>
1837 */
1845 }
1848 }
1849 /**
1850 * Converts this <code>Real</code> value to the closest <code>int</code>
1851 * value towards zero.
1852 *
1853 * <p>If the value of this <code>Real</code> is too large, {@link
1854 * Integer#MAX_VALUE} is returned. However, if the value of this
1855 * <code>Real</code> is too small, <code>-Integer.MAX_VALUE</code> is
1856 * returned, not {@link Integer#MIN_VALUE}. This is done to ensure that
1857 * the sign will be correct if you calculate
1858 * <code>-this.toInteger()</code>. A NaN is converted to 0.
1859 *
1860 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1861 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1862 * Equivalent </i><code>double</code><i> code:</i></td><td>
1863 * <code>(int)this</code>
1864 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
1865 * 0 ULPs
1866 * </td></tr><tr><td><i>
1867 * Execution time relative to add:
1868 * </i></td><td>
1869 * 0.6
1870 * </td></tr></table>
1871 *
1872 * @return an <code>int</code> representation of this <code>Real</code>.
1873 */
1879 // 0x80000001, so that you can take -x.toInteger()
1880 }
1886 // 0x80000001, so that you can take -x.toInteger()
1887 }
1890 }
1891 /**
1892 * Converts this <code>Real</code> value to the closest <code>long</code>
1893 * value towards zero.
1894 *
1895 * <p>If the value of this <code>Real</code> is too large, {@link
1896 * Long#MAX_VALUE} is returned. However, if the value of this
1897 * <code>Real</code> is too small, <code>-Long.MAX_VALUE</code> is
1898 * returned, not {@link Long#MIN_VALUE}. This is done to ensure that the
1899 * sign will be correct if you calculate <code>-this.toLong()</code>.
1900 * A NaN is converted to 0.
1901 *
1902 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1903 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1904 * Equivalent </i><code>double</code><i> code:</i></td><td>
1905 * <code>(long)this</code>
1906 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
1907 * 0 ULPs
1908 * </td></tr><tr><td><i>
1909 * Execution time relative to add:
1910 * </i></td><td>
1911 * 0.5
1912 * </td></tr></table>
1913 *
1914 * @return a <code>long</code> representation of this <code>Real</code>.
1915 */
1921 // 0x8000000000000001L, so that you can take -x.toLong()
1922 }
1928 // 0x8000000000000001L, so that you can take -x.toLong()
1929 }
1931 }
1932 /**
1933 * Returns <code>true</code> if the value of this <code>Real</code>
1934 * represents a mathematical integer. If the value is too large to
1935 * determine if it is an integer, <code>true</code> is returned.
1936 *
1937 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1938 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1939 * Equivalent </i><code>double</code><i> code:</i></td><td>
1940 * <code>(this == (long)this)</code>
1941 * </td></tr><tr><td><i>
1942 * Execution time relative to add:
1943 * </i></td><td>
1944 * 0.6
1945 * </td></tr></table>
1946 *
1947 * @return <code>true</code> if the value represented by this object
1948 * represents a mathematical integer, <code>false</code> otherwise.
1949 */
1961 }
1962 /**
1963 * Returns <code>true</code> if the mathematical integer represented
1964 * by this <code>Real</code> is odd. You <u>must</u> first determine
1965 * that the value is actually an integer using {@link
1966 * #isIntegral()}. If the value is too large to determine if the
1967 * integer is odd, <code>false</code> is returned.
1968 *
1969 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1970 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1971 * Equivalent </i><code>double</code><i> code:</i></td><td>
1972 * <code>((((long)this)&1) == 1)</code>
1973 * </td></tr><tr><td><i>
1974 * Execution time relative to add:
1975 * </i></td><td>
1976 * 0.6
1977 * </td></tr></table>
1978 *
1979 * @return <code>true</code> if the mathematical integer represented by
1980 * this <code>Real</code> is odd, <code>false</code> otherwise.
1981 */
1988 }
1989 /**
1990 * Exchanges the contents of this <code>Real</code> and <code>a</code>.
1991 *
1992 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1993 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1994 * Equivalent </i><code>double</code><i> code:</i></td><td>
1995 * <code>tmp=this; this=a; a=tmp;</code>
1996 * </td></tr><tr><td><i>Error bound:</i></td><td>
1997 * 0 ULPs
1998 * </td></tr><tr><td><i>
1999 * Execution time relative to add:
2000 * </i></td><td>
2001 * 0.5
2002 * </td></tr></table>
2003 *
2004 * @param a the <code>Real</code> to exchange with this.
2005 */
2010 }
2011 // Temporary values used by functions (to avoid "new" inside functions)
2024 /**
2025 * Calculates the sum of this <code>Real</code> and <code>a</code>.
2026 * Replaces the contents of this <code>Real</code> with the result.
2027 *
2028 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2029 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2030 * Equivalent </i><code>double</code><i> code:</i></td><td>
2031 * <code>this += a;</code>
2032 * </td></tr><tr><td><i>Error bound:</i></td><td>
2033 * ½ ULPs
2034 * </td></tr><tr><td><i>
2035 * Execution time relative to add:
2036 * </i></td><td>
2037 * «« 1.0 »»
2038 * </td></tr></table>
2039 *
2040 * @param a the <code>Real</code> to add to this.
2041 */
2046 }
2048 if ((this.exponent < 0 && this.mantissa == 0) && (a.exponent < 0 && a.mantissa == 0) && sign != a.sign)
2050 else
2053 }
2060 }
2066 {
2077 }
2086 // Simplified normalize()
2088 exponent ++;
2092 }
2093 }
2096 // Shift mantissa up to increase accuracy
2098 exponent --;
2099 shift --;
2100 }
2106 // Simplified normalize()
2110 // Operands have equal exponents => many bits may be cancelled
2111 // Magic rounding: if result of subtract leaves only a few bits
2112 // standing, the result should most likely be 0...
2114 // If arguments were integers <= 2^63-1, then don't
2115 // do the magic rounding anyway.
2116 // This is a bit "post mortem" investigation but it happens
2117 // so seldom that it's no problem to spend the extra time.
2126 }
2129 }
2132 }
2133 /**
2134 * Calculates the sum of this <code>Real</code> and the integer
2135 * <code>a</code>.
2136 * Replaces the contents of this <code>Real</code> with the result.
2137 *
2138 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2139 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2140 * Equivalent </i><code>double</code><i> code:</i></td><td>
2141 * <code>this += a;</code>
2142 * </td></tr><tr><td><i>Error bound:</i></td><td>
2143 * ½ ULPs
2144 * </td></tr><tr><td><i>
2145 * Execution time relative to add:
2146 * </i></td><td>
2147 * 1.8
2148 * </td></tr></table>
2149 *
2150 * @param a the <code>int</code> to add to this.
2151 */
2155 }
2156 /**
2157 * Calculates the sum of this <code>Real</code> and <code>a</code> with
2158 * extended precision. Replaces the contents of this <code>Real</code>
2159 * with the result. Returns the extra mantissa of the extended precision
2160 * result.
2161 *
2162 * <p>An extra 64 bits of mantissa is added to both arguments for extended
2163 * precision. If any of the arguments are not of extended precision, use
2164 * <code>0</code> for the extra mantissa.
2165 *
2166 * <p>Extended prevision can be useful in many situations. For instance,
2167 * when accumulating a lot of very small values it is advantageous for the
2168 * accumulator to have extended precision. To convert the extended
2169 * precision value back to a normal <code>Real</code> for further
2170 * processing, use {@link #roundFrom128(long)}.
2171 *
2172 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2173 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2174 * Equivalent </i><code>double</code><i> code:</i></td><td>
2175 * <code>this += a;</code>
2176 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
2177 * 2<sup>-62</sup> ULPs (i.e. of a normal precision <code>Real</code>)
2178 * </td></tr><tr><td><i>
2179 * Execution time relative to add:
2180 * </i></td><td>
2181 * 2.0
2182 * </td></tr></table>
2183 *
2184 * @param extra the extra 64 bits of mantissa of this extended precision
2185 * <code>Real</code>.
2186 * @param a the <code>Real</code> to add to this.
2187 * @param aExtra the extra 64 bits of mantissa of the extended precision
2188 * value <code>a</code>.
2189 * @return the extra 64 bits of mantissa of the resulting extended
2190 * precision <code>Real</code>.
2191 */
2196 }
2198 if ((this.exponent < 0 && this.mantissa == 0) && (a.exponent < 0 && a.mantissa == 0) && sign != a.sign)
2200 else
2203 }
2208 }
2212 }
2221 {
2235 }
2245 }
2256 // Magic rounding: if result of subtract leaves only a few bits
2257 // standing, the result should most likely be 0...
2260 }
2266 }
2267 /**
2268 * Calculates the difference between this <code>Real</code> and
2269 * <code>a</code>.
2270 * Replaces the contents of this <code>Real</code> with the result.
2271 *
2272 * <p>(To achieve extended precision subtraction, it is enough to call
2273 * <code>a.{@link #neg() neg}()</code> before calling <code>{@link
2274 * #add128(long,Real,long) add128}(extra,a,aExtra)</code>, since only
2275 * the sign bit of <code>a</code> need to be changed.)
2276 *
2277 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2278 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2279 * Equivalent </i><code>double</code><i> code:</i></td><td>
2280 * <code>this -= a;</code>
2281 * </td></tr><tr><td><i>Error bound:</i></td><td>
2282 * ½ ULPs
2283 * </td></tr><tr><td><i>
2284 * Execution time relative to add:
2285 * </i></td><td>
2286 * 2.0
2287 * </td></tr></table>
2288 *
2289 * @param a the <code>Real</code> to subtract from this.
2290 */
2296 }
2297 /**
2298 * Calculates the difference between this <code>Real</code> and the
2299 * integer <code>a</code>.
2300 * Replaces the contents of this <code>Real</code> with the result.
2301 *
2302 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2303 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2304 * Equivalent </i><code>double</code><i> code:</i></td><td>
2305 * <code>this -= a;</code>
2306 * </td></tr><tr><td><i>Error bound:</i></td><td>
2307 * ½ ULPs
2308 * </td></tr><tr><td><i>
2309 * Execution time relative to add:
2310 * </i></td><td>
2311 * 2.4
2312 * </td></tr></table>
2313 *
2314 * @param a the <code>int</code> to subtract from this.
2315 */
2319 }
2320 /**
2321 * Calculates the product of this <code>Real</code> and <code>a</code>.
2322 * Replaces the contents of this <code>Real</code> with the result.
2323 *
2324 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2325 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2326 * Equivalent </i><code>double</code><i> code:</i></td><td>
2327 * <code>this *= a;</code>
2328 * </td></tr><tr><td><i>Error bound:</i></td><td>
2329 * ½ ULPs
2330 * </td></tr><tr><td><i>
2331 * Execution time relative to add:
2332 * </i></td><td>
2333 * 1.3
2334 * </td></tr></table>
2335 *
2336 * @param a the <code>Real</code> to multiply to this.
2337 */
2342 }
2347 else
2350 }
2354 }
2360 // If we're going to need normalization, we don't want to round twice
2367 // Not underflow after all, it will be corrected in the
2368 // normalization below
2372 else
2375 }
2376 }
2377 // Simplified normalize()
2380 exponent ++;
2383 }
2384 }
2385 /**
2386 * Calculates the product of this <code>Real</code> and the integer
2387 * <code>a</code>.
2388 * Replaces the contents of this <code>Real</code> with the result.
2389 *
2390 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2391 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2392 * Equivalent </i><code>double</code><i> code:</i></td><td>
2393 * <code>this *= a;</code>
2394 * </td></tr><tr><td><i>Error bound:</i></td><td>
2395 * ½ ULPs
2396 * </td></tr><tr><td><i>
2397 * Execution time relative to add:
2398 * </i></td><td>
2399 * 1.3
2400 * </td></tr></table>
2401 *
2402 * @param a the <code>int</code> to multiply to this.
2403 */
2410 }
2414 else
2417 }
2420 // Normalize int
2428 }
2433 // If we're going to need normalization, we don't want to round twice
2436 // Simplified normalize()
2439 exponent ++;
2442 }
2443 }
2444 /**
2445 * Calculates the product of this <code>Real</code> and <code>a</code> with
2446 * extended precision.
2447 * Replaces the contents of this <code>Real</code> with the result.
2448 * Returns the extra mantissa of the extended precision result.
2449 *
2450 * <p>An extra 64 bits of mantissa is added to both arguments for
2451 * extended precision. If any of the arguments are not of extended
2452 * precision, use <code>0</code> for the extra mantissa. See also {@link
2453 * #add128(long,Real,long)}.
2454 *
2455 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2456 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2457 * Equivalent </i><code>double</code><i> code:</i></td><td>
2458 * <code>this *= a;</code>
2459 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
2460 * 2<sup>-60</sup> ULPs
2461 * </td></tr><tr><td><i>
2462 * Execution time relative to add:
2463 * </i></td><td>
2464 * 3.1
2465 * </td></tr></table>
2466 *
2467 * @param extra the extra 64 bits of mantissa of this extended precision
2468 * <code>Real</code>.
2469 * @param a the <code>Real</code> to multiply to this.
2470 * @param aExtra the extra 64 bits of mantissa of the extended precision
2471 * value <code>a</code>.
2472 * @return the extra 64 bits of mantissa of the resulting extended
2473 * precision <code>Real</code>.
2474 */
2479 }
2484 else
2487 }
2491 }
2497 else
2500 }
2515 //(a2*b0>>>34)+(a1*b1>>>34)+(a0*b2>>>34)+0x08000000)>>>28;
2529 }
2537 exponent++;
2538 }
2541 }
2542 /**
2543 * Calculates the square of this <code>Real</code>.
2544 * Replaces the contents of this <code>Real</code> with the result.
2545 *
2546 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2547 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2548 * Equivalent </i><code>double</code><i> code:</i></td><td>
2549 * <code>this = this*this;</code>
2550 * </td></tr><tr><td><i>Error bound:</i></td><td>
2551 * ½ ULPs
2552 * </td></tr><tr><td><i>
2553 * Execution time relative to add:
2554 * </i></td><td>
2555 * 1.1
2556 * </td></tr></table>
2557 */
2567 else
2570 }
2574 // If we're going to need normalization, we don't want to round twice
2577 // Simplified normalize()
2580 exponent ++;
2583 }
2584 }
2586 // Calculate (a<<63)/b, where a<2**64, b<2**63, b<=a and a<2*b The
2587 // result will always be 63 bits, leading to a 3-stage radix-2**21
2588 // (very high radix) algorithm, as described here:
2589 // S.F. Oberman and M.J. Flynn, "Division Algorithms and
2590 // Implementations," IEEE Trans. Computers, vol. 46, no. 8,
2591 // pp. 833-854, Aug 1997 Section 4: "Very High Radix Algorithms"
2596 // Preparations
2600 // Now perform the division
2603 // No need to remove remainder, this will be cared for in next block
2607 // Two more almost identical blocks...
2616 // Remove final remainder
2619 // Round correctly
2622 }
2623 /**
2624 * Calculates the quotient of this <code>Real</code> and <code>a</code>.
2625 * Replaces the contents of this <code>Real</code> with the result.
2626 *
2627 * <p>(To achieve extended precision division, call
2628 * <code>aExtra=a.{@link #recip128(long) recip128}(aExtra)</code> before
2629 * calling <code>{@link #mul128(long,Real,long)
2630 * mul128}(extra,a,aExtra)</code>.)
2631 *
2632 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2633 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2634 * Equivalent </i><code>double</code><i> code:</i></td><td>
2635 * <code>this /= a;</code>
2636 * </td></tr><tr><td><i>Error bound:</i></td><td>
2637 * ½ ULPs
2638 * </td></tr><tr><td><i>
2639 * Execution time relative to add:
2640 * </i></td><td>
2641 * 2.6
2642 * </td></tr></table>
2643 *
2644 * @param a the <code>Real</code> to divide this with.
2645 */
2650 }
2656 }
2660 }
2665 }
2669 }
2673 exponent--;
2674 }
2678 else
2681 }
2685 }
2686 /**
2687 * Calculates the quotient of this <code>Real</code> and the integer
2688 * <code>a</code>.
2689 * Replaces the contents of this <code>Real</code> with the result.
2690 *
2691 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2692 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2693 * Equivalent </i><code>double</code><i> code:</i></td><td>
2694 * <code>this /= a;</code>
2695 * </td></tr><tr><td><i>Error bound:</i></td><td>
2696 * ½ ULPs
2697 * </td></tr><tr><td><i>
2698 * Execution time relative to add:
2699 * </i></td><td>
2700 * 2.6
2701 * </td></tr></table>
2702 *
2703 * @param a the <code>int</code> to divide this with.
2704 */
2711 }
2718 }
2722 }
2726 // Normalizing mantissa and scaling remainder accordingly
2735 // Final division, correctly rounded
2740 }
2741 /**
2742 * Calculates the quotient of <code>a</code> and this <code>Real</code>.
2743 * Replaces the contents of this <code>Real</code> with the result.
2744 *
2745 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2746 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2747 * Equivalent </i><code>double</code><i> code:</i></td><td>
2748 * <code>this = a/this;</code>
2749 * </td></tr><tr><td><i>Error bound:</i></td><td>
2750 * ½ ULPs
2751 * </td></tr><tr><td><i>
2752 * Execution time relative to add:
2753 * </i></td><td>
2754 * 3.1
2755 * </td></tr></table>
2756 *
2757 * @param a the <code>Real</code> to be divided by this.
2758 */
2762 { this.mantissa = recipTmp.mantissa; this.exponent = recipTmp.exponent; this.sign = recipTmp.sign; };
2763 }
2764 /**
2765 * Calculates the quotient of the integer <code>a</code> and this
2766 * <code>Real</code>.
2767 * Replaces the contents of this <code>Real</code> with the result.
2768 *
2769 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2770 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2771 * Equivalent </i><code>double</code><i> code:</i></td><td>
2772 * <code>this = a/this;</code>
2773 * </td></tr><tr><td><i>Error bound:</i></td><td>
2774 * ½ ULPs
2775 * </td></tr><tr><td><i>
2776 * Execution time relative to add:
2777 * </i></td><td>
2778 * 3.9
2779 * </td></tr></table>
2780 *
2781 * @param a the <code>int</code> to be divided by this.
2782 */
2786 }
2787 /**
2788 * Calculates the reciprocal of this <code>Real</code>.
2789 * Replaces the contents of this <code>Real</code> with the result.
2790 *
2791 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2792 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2793 * Equivalent </i><code>double</code><i> code:</i></td><td>
2794 * <code>this = 1/this;</code>
2795 * </td></tr><tr><td><i>Error bound:</i></td><td>
2796 * ½ ULPs
2797 * </td></tr><tr><td><i>
2798 * Execution time relative to add:
2799 * </i></td><td>
2800 * 2.3
2801 * </td></tr></table>
2802 */
2809 }
2813 }
2819 }
2820 exponent--;
2822 }
2823 /**
2824 * Calculates the reciprocal of this <code>Real</code> with
2825 * extended precision.
2826 * Replaces the contents of this <code>Real</code> with the result.
2827 * Returns the extra mantissa of the extended precision result.
2828 *
2829 * <p>An extra 64 bits of mantissa is added for extended precision.
2830 * If the argument is not of extended precision, use <code>0</code>
2831 * for the extra mantissa. See also {@link #add128(long,Real,long)}.
2832 *
2833 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2834 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2835 * Equivalent </i><code>double</code><i> code:</i></td><td>
2836 * <code>this = 1/this;</code>
2837 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
2838 * 2<sup>-60</sup> ULPs
2839 * </td></tr><tr><td><i>
2840 * Execution time relative to add:
2841 * </i></td><td>
2842 * 17
2843 * </td></tr></table>
2844 *
2845 * @param extra the extra 64 bits of mantissa of this extended precision
2846 * <code>Real</code>.
2847 * @return the extra 64 bits of mantissa of the resulting extended
2848 * precision <code>Real</code>.
2849 */
2856 }
2860 }
2863 // Special case, simple power of 2
2869 }
2870 // Normalize exponent
2873 // Save -A
2874 { recipTmp.mantissa = this.mantissa; recipTmp.exponent = this.exponent; recipTmp.sign = this.sign; };
2877 // First establish approximate result (actually 63 bit accurate)
2879 // Perform one Newton-Raphson iteration
2880 // Xn+1 = Xn + Xn*(1-A*Xn)
2881 { recipTmp2.mantissa = this.mantissa; recipTmp2.exponent = this.exponent; recipTmp2.sign = this.sign; };
2886 // Fix exponent
2888 // Fix sign
2892 }
2893 /**
2894 * Calculates the mathematical integer that is less than or equal to
2895 * this <code>Real</code> divided by <code>a</code>.
2896 * Replaces the contents of this <code>Real</code> with the result.
2897 *
2898 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2899 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2900 * Equivalent </i><code>double</code><i> code:</i></td><td>
2901 * <code>this = Math.{@link Math#floor(double) floor}(this/a);</code>
2902 * </td></tr><tr><td><i>Error bound:</i></td><td>
2903 * 0 ULPs
2904 * </td></tr><tr><td><i>
2905 * Execution time relative to add:
2906 * </i></td><td>
2907 * 22
2908 * </td></tr></table>
2909 *
2910 * @param a the <code>Real</code> argument.
2911 */
2916 }
2921 }
2925 }
2930 }
2934 }
2935 { tmp0.mantissa = a.mantissa; tmp0.exponent = a.exponent; tmp0.sign = a.sign; }; // tmp0 should be free
2936 // Perform same division as with mod, and don't round up
2941 {
2942 // For accurate floor()
2943 mantissa++;
2945 }
2947 }
2949 { tmp0.mantissa = a.mantissa; tmp0.exponent = a.exponent; tmp0.sign = a.sign; }; // tmp0 should be free
2953 // floor() will be inaccurate
2956 }
2959 {
2960 // For accurate floor() with a bit of "magical rounding"
2963 }
2969 }
2970 /**
2971 * Calculates the value of this <code>Real</code> modulo <code>a</code>.
2972 * Replaces the contents of this <code>Real</code> with the result.
2973 * The modulo in this case is defined as the remainder after subtracting
2974 * <code>a</code> multiplied by the mathematical integer that is less than
2975 * or equal to this <code>Real</code> divided by <code>a</code>.
2976 *
2977 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2978 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2979 * Equivalent </i><code>double</code><i> code:</i></td><td>
2980 * <code>this = this -
2981 * a*Math.{@link Math#floor(double) floor}(this/a);</code>
2982 * </td></tr><tr><td><i>Error bound:</i></td><td>
2983 * 0 ULPs
2984 * </td></tr><tr><td><i>
2985 * Execution time relative to add:
2986 * </i></td><td>
2987 * 27
2988 * </td></tr></table>
2989 *
2990 * @param a the <code>Real</code> argument.
2991 */
2996 }
3000 }
3004 else
3007 }
3012 }
3016 }
3018 }
3019 /**
3020 * Calculates the logical <i>AND</i> of this <code>Real</code> and
3021 * <code>a</code>.
3022 * Replaces the contents of this <code>Real</code> with the result.
3023 *
3024 * <p>Semantics of bitwise logical operations exactly mimic those of
3025 * Java's bitwise integer operators. In these operations, the
3026 * internal binary representation of the numbers are used. If the
3027 * values represented by the operands are not mathematical
3028 * integers, the fractional bits are also included in the operation.
3029 *
3030 * <p>Negative numbers are interpreted as two's-complement,
3031 * generalized to real numbers: Negating the number inverts all
3032 * bits, including an infinite number of 1-bits before the radix
3033 * point and an infinite number of 1-bits after the radix point. The
3034 * infinite number of 1-bits after the radix is rounded upwards
3035 * producing an infinite number of 0-bits, until the first 0-bit is
3036 * encountered which will be switched to a 1 (rounded or not, these
3037 * two forms are mathematically equivalent). For example, the number
3038 * "1" negated, becomes (in binary form)
3039 * <code>...1111110.111111....</code> Rounding of the infinite
3040 * number of 1's after the radix gives the number
3041 * <code>...1111111.000000...</code>, which is exactly the way we
3042 * usually see "-1" as two's-complement.
3043 *
3044 * <p>This method calculates a negative value if and only
3045 * if this and <code>a</code> are both negative.
3046 *
3047 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3048 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3049 * Equivalent </i><code>int</code><i> code:</i></td><td>
3050 * <code>this &= a;</code>
3051 * </td></tr><tr><td><i>Error bound:</i></td><td>
3052 * 0 ULPs
3053 * </td></tr><tr><td><i>
3054 * Execution time relative to add:
3055 * </i></td><td>
3056 * 1.5
3057 * </td></tr></table>
3058 *
3059 * @param a the <code>Real</code> argument
3060 */
3065 }
3069 }
3078 else
3081 }
3096 }
3102 }
3112 }
3114 }
3115 /**
3116 * Calculates the logical <i>OR</i> of this <code>Real</code> and
3117 * <code>a</code>.
3118 * Replaces the contents of this <code>Real</code> with the result.
3119 *
3120 * <p>See {@link #and(Real)} for an explanation of the
3121 * interpretation of a <code>Real</code> in bitwise operations.
3122 * This method calculates a negative value if and only
3123 * if either this or <code>a</code> is negative.
3124 *
3125 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3126 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3127 * Equivalent </i><code>int</code><i> code:</i></td><td>
3128 * <code>this |= a;</code>
3129 * </td></tr><tr><td><i>Error bound:</i></td><td>
3130 * 0 ULPs
3131 * </td></tr><tr><td><i>
3132 * Execution time relative to add:
3133 * </i></td><td>
3134 * 1.6
3135 * </td></tr></table>
3136 *
3137 * @param a the <code>Real</code> argument
3138 */
3143 }
3148 }
3157 }
3163 {
3174 }
3184 else
3190 }
3192 }
3193 /**
3194 * Calculates the logical <i>XOR</i> of this <code>Real</code> and
3195 * <code>a</code>.
3196 * Replaces the contents of this <code>Real</code> with the result.
3197 *
3198 * <p>See {@link #and(Real)} for an explanation of the
3199 * interpretation of a <code>Real</code> in bitwise operations.
3200 * This method calculates a negative value if and only
3201 * if exactly one of this and <code>a</code> is negative.
3202 *
3203 * <p>The operation <i>NOT</i> has been omitted in this library
3204 * because it cannot be generalized to fractional numbers. If this
3205 * <code>Real</code> represents a mathematical integer, the
3206 * operation <i>NOT</i> can be calculated as "this <i>XOR</i> -1",
3207 * which is equivalent to "this <i>XOR</i>
3208 * <code>/FFFFFFFF.0000</code>".
3209 *
3210 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3211 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3212 * Equivalent </i><code>int</code><i> code:</i></td><td>
3213 * <code>this ^= a;</code>
3214 * </td></tr><tr><td><i>Error bound:</i></td><td>
3215 * 0 ULPs
3216 * </td></tr><tr><td><i>
3217 * Execution time relative to add:
3218 * </i></td><td>
3219 * 1.5
3220 * </td></tr></table>
3221 *
3222 * @param a the <code>Real</code> argument
3223 */
3228 }
3233 }
3237 }
3252 }
3265 }
3267 }
3268 /**
3269 * Calculates the value of this <code>Real</code> <i>AND NOT</i>
3270 * <code>a</code>. The opeation is read as "bit clear".
3271 * Replaces the contents of this <code>Real</code> with the result.
3272 *
3273 * <p>See {@link #and(Real)} for an explanation of the
3274 * interpretation of a <code>Real</code> in bitwise operations.
3275 * This method calculates a negative value if and only
3276 * if this is negative and not <code>a</code> is negative.
3277 *
3278 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3279 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3280 * Equivalent </i><code>int</code><i> code:</i></td><td>
3281 * <code>this &= ~a;</code>
3282 * </td></tr><tr><td><i>Error bound:</i></td><td>
3283 * 0 ULPs
3284 * </td></tr><tr><td><i>
3285 * Execution time relative to add:
3286 * </i></td><td>
3287 * 1.5
3288 * </td></tr></table>
3289 *
3290 * @param a the <code>Real</code> argument
3291 */
3296 }
3304 else
3309 else
3311 }
3313 }
3326 else
3337 }
3339 }
3343 }
3344 /**
3345 * Calculates the square root of this <code>Real</code>.
3346 * Replaces the contents of this <code>Real</code> with the result.
3347 *
3348 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3349 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3350 * Equivalent </i><code>double</code><i> code:</i></td><td>
3351 * <code>this = Math.{@link Math#sqrt(double) sqrt}(this);</code>
3352 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3353 * 1 ULPs
3354 * </td></tr><tr><td><i>
3355 * Execution time relative to add:
3356 * </i></td><td>
3357 * 19
3358 * </td></tr></table>
3359 */
3361 /*
3362 * Adapted from:
3363 * Cephes Math Library Release 2.2: December, 1990
3364 * Copyright 1984, 1990 by Stephen L. Moshier
3365 *
3366 * sqrtl.c
3367 *
3368 * long double sqrtl(long double x);
3369 */
3375 }
3379 }
3382 // Save X
3383 { recipTmp.mantissa = this.mantissa; recipTmp.exponent = this.exponent; recipTmp.sign = this.sign; };
3384 // normalize to range [0.5, 1)
3387 // quadratic approximation, relative error 6.45e-4
3388 { recipTmp2.mantissa = this.mantissa; recipTmp2.exponent = this.exponent; recipTmp2.sign = this.sign; };
3389 { sqrtTmp.sign=(byte)1; sqrtTmp.exponent=0x3ffffffd; sqrtTmp.mantissa=0x68a7e193370ff21bL; };//-0.2044058315473477195990
3391 { sqrtTmp.sign=(byte)0; sqrtTmp.exponent=0x3fffffff; sqrtTmp.mantissa=0x71f1e120690deae8L; };//0.89019407351052789754347
3394 { sqrtTmp.sign=(byte)0; sqrtTmp.exponent=0x3ffffffe; sqrtTmp.mantissa=0x5045ee6baf28677aL; };//0.31356706742295303132394
3396 // adjust for odd powers of 2
3399 // calculate exponent
3401 // Newton iteratios:
3402 // Yn+1 = (Yn + X/Yn)/2
3404 { recipTmp2.mantissa = recipTmp.mantissa; recipTmp2.exponent = recipTmp.exponent; recipTmp2.sign = recipTmp.sign; };
3408 }
3409 }
3410 /**
3411 * Calculates the reciprocal square root of this <code>Real</code>.
3412 * Replaces the contents of this <code>Real</code> with the result.
3413 *
3414 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3415 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3416 * Equivalent </i><code>double</code><i> code:</i></td><td>
3417 * <code>this = 1/Math.{@link Math#sqrt(double) sqrt}(this);</code>
3418 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3419 * 1 ULPs
3420 * </td></tr><tr><td><i>
3421 * Execution time relative to add:
3422 * </i></td><td>
3423 * 21
3424 * </td></tr></table>
3425 */
3429 }
3430 /**
3431 * Calculates the cube root of this <code>Real</code>.
3432 * Replaces the contents of this <code>Real</code> with the result.
3433 * The cube root of a negative value is the negative of the cube
3434 * root of that value's magnitude.
3435 *
3436 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3437 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3438 * Equivalent </i><code>double</code><i> code:</i></td><td>
3439 * <code>this = Math.{@link Math#cbrt(double) cbrt}(this);</code>
3440 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3441 * 2 ULPs
3442 * </td></tr><tr><td><i>
3443 * Execution time relative to add:
3444 * </i></td><td>
3445 * 32
3446 * </td></tr></table>
3447 */
3453 // Calculates recipocal cube root of normalized Real,
3454 // not zero, nan or infinity
3456 // Save -A
3457 { recipTmp.mantissa = this.mantissa; recipTmp.exponent = this.exponent; recipTmp.sign = this.sign; };
3459 // First establish approximate result
3465 { recipTmp2.sign=(byte)0; recipTmp2.exponent=0x3fffffff; recipTmp2.mantissa=0x6597fa94f5b8f20bL; }; // cbrt(1/2)
3469 }
3470 // Now perform Newton-Raphson iteration
3471 // Xn+1 = (4*Xn - A*Xn**4)/3
3473 { recipTmp2.mantissa = this.mantissa; recipTmp2.exponent = this.exponent; recipTmp2.sign = this.sign; };
3480 }
3484 }
3485 /**
3486 * Calculates the n'th root of this <code>Real</code>.
3487 * Replaces the contents of this <code>Real</code> with the result.
3488 * For odd integer n, the n'th root of a negative value is the
3489 * negative of the n'th root of that value's magnitude.
3490 *
3491 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3492 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3493 * Equivalent </i><code>double</code><i> code:</i></td><td>
3494 * <code>this = Math.{@link Math#pow(double,double)
3495 * pow}(this,1/a);</code>
3496 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3497 * 2 ULPs
3498 * </td></tr><tr><td><i>
3499 * Execution time relative to add:
3500 * </i></td><td>
3501 * 110
3502 * </td></tr></table>
3503 *
3504 * @param n the <code>Real</code> argument.
3505 */
3510 }
3517 }
3522 }
3523 { tmp2.mantissa = n.mantissa; tmp2.exponent = n.exponent; tmp2.sign = n.sign; }; // Copy to temporary location in case of x.nroot(x)
3528 }
3529 /**
3530 * Calculates <code>sqrt(this*this+a*a)</code>.
3531 * Replaces the contents of this <code>Real</code> with the result.
3532 *
3533 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3534 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3535 * Equivalent </i><code>double</code><i> code:</i></td><td>
3536 * <code>this = Math.{@link Math#hypot(double,double)
3537 * hypot}(this,a);</code>
3538 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3539 * 1 ULPs
3540 * </td></tr><tr><td><i>
3541 * Execution time relative to add:
3542 * </i></td><td>
3543 * 24
3544 * </td></tr></table>
3545 *
3546 * @param a the <code>Real</code> argument.
3547 */
3549 { tmp1.mantissa = a.mantissa; tmp1.exponent = a.exponent; tmp1.sign = a.sign; }; // Copy to temporary location in case of x.hypot(x)
3554 }
3562 }
3566 }
3567 // Extract integer part
3568 { expTmp.mantissa = this.mantissa; expTmp.exponent = this.exponent; expTmp.sign = this.sign; };
3575 }
3579 }
3580 // Subtract integer part (this is where we need the extra accuracy)
3583 /*
3584 * Adapted from:
3585 * Cephes Math Library Release 2.7: May, 1998
3586 * Copyright 1984, 1991, 1998 by Stephen L. Moshier
3587 *
3588 * exp2l.c
3589 *
3590 * long double exp2l(long double x);
3591 */
3592 // Now -0.5<X<0.5
3593 // rational approximation
3594 // exp2(x) = 1 + 2x P(x²)/(Q(x²) - x P(x²))
3595 { expTmp2.mantissa = this.mantissa; expTmp2.exponent = this.exponent; expTmp2.sign = this.sign; };
3597 // P(x²)
3598 { expTmp.sign=(byte)0; expTmp.exponent=0x40000005; expTmp.mantissa=0x793ace15b56b7fecL; };//60.614853552242266094567
3600 { expTmp3.sign=(byte)0; expTmp3.exponent=0x4000000e; expTmp3.mantissa=0x764ef8cf96e29a13L; };//30286.971917562792508623
3603 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000014; expTmp3.mantissa=0x7efa0173e820bf60L; };//2080384.3631901852422887
3606 // Q(x²)
3608 { expTmp3.sign=(byte)0; expTmp3.exponent=0x4000000a; expTmp3.mantissa=0x6d549a6b4dc9abadL; };//1749.2876999891839021063
3611 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000012; expTmp3.mantissa=0x5002d27836ba71c6L; };//327725.15434906797273099
3614 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000016; expTmp3.mantissa=0x5b98206867dd59bfL; };//6002720.4078348487957118
3620 // Scale by power of 2
3622 }
3623 /**
3624 * Calculates <i>e</i> raised to the power of this <code>Real</code>.
3625 * Replaces the contents of this <code>Real</code> with the result.
3626 *
3627 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3628 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3629 * Equivalent </i><code>double</code><i> code:</i></td><td>
3630 * <code>this = Math.{@link Math#exp(double) exp}(this);</code>
3631 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3632 * 1 ULPs
3633 * </td></tr><tr><td><i>
3634 * Execution time relative to add:
3635 * </i></td><td>
3636 * 31
3637 * </td></tr></table>
3638 */
3640 { expTmp.sign=(byte)0; expTmp.exponent=0x40000000; expTmp.mantissa=0x5c551d94ae0bf85dL; }; // log2(e)
3643 }
3644 /**
3645 * Calculates 2 raised to the power of this <code>Real</code>.
3646 * Replaces the contents of this <code>Real</code> with the result.
3647 *
3648 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3649 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3650 * Equivalent </i><code>double</code><i> code:</i></td><td>
3651 * <code>this = Math.{@link Math#exp(double) exp}(this *
3652 * Math.{@link Math#log(double) log}(2));</code>
3653 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3654 * 1 ULPs
3655 * </td></tr><tr><td><i>
3656 * Execution time relative to add:
3657 * </i></td><td>
3658 * 27
3659 * </td></tr></table>
3660 */
3663 }
3664 /**
3665 * Calculates 10 raised to the power of this <code>Real</code>.
3666 * Replaces the contents of this <code>Real</code> with the result.
3667 *
3668 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3669 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3670 * Equivalent </i><code>double</code><i> code:</i></td><td>
3671 * <code>this = Math.{@link Math#exp(double) exp}(this *
3672 * Math.{@link Math#log(double) log}(10));</code>
3673 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3674 * 1 ULPs
3675 * </td></tr><tr><td><i>
3676 * Execution time relative to add:
3677 * </i></td><td>
3678 * 31
3679 * </td></tr></table>
3680 */
3682 { expTmp.sign=(byte)0; expTmp.exponent=0x40000001; expTmp.mantissa=0x6a4d3c25e68dc57fL; }; // log2(10)
3685 }
3687 {
3693 }
3697 }
3700 /*
3701 * Adapted from:
3702 * Cephes Math Library Release 2.7: May, 1998
3703 * Copyright 1984, 1990, 1998 by Stephen L. Moshier
3704 *
3705 * logl.c
3706 *
3707 * long double logl(long double x);
3708 */
3709 // normalize to range [0.5, 1)
3712 // rational appriximation
3713 // log(1+x) = x - x²/2 + x³ P(x)/Q(x)
3715 e--;
3716 exponent++;
3717 }
3719 { expTmp2.mantissa = this.mantissa; expTmp2.exponent = this.exponent; expTmp2.sign = this.sign; };
3720 // P(x)
3721 { this.sign=(byte)0; this.exponent=0x3ffffff1; this.mantissa=0x5ef0258ace5728ddL; };//4.5270000862445199635215E-5
3723 { expTmp3.sign=(byte)0; expTmp3.exponent=0x3ffffffe; expTmp3.mantissa=0x7fa06283f86a0ce8L; };//0.4985410282319337597221
3726 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000002; expTmp3.mantissa=0x69427d1bd3e94ca1L; };//6.5787325942061044846969
3729 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000004; expTmp3.mantissa=0x77a5ce2e32e7256eL; };//29.911919328553073277375
3732 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000005; expTmp3.mantissa=0x79e63ae1b0cd4222L; };//60.949667980987787057556
3735 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000005; expTmp3.mantissa=0x7239d65d1e6840d6L; };//57.112963590585538103336
3738 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000004; expTmp3.mantissa=0x502880b6660c265fL; };//20.039553499201281259648
3740 // Q(x)
3741 { expTmp.mantissa = expTmp2.mantissa; expTmp.exponent = expTmp2.exponent; expTmp.sign = expTmp2.sign; };
3742 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000003; expTmp3.mantissa=0x7880d67a40f8dc5cL; };//15.062909083469192043167
3745 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000006; expTmp3.mantissa=0x530c2d4884d25e18L; };//83.047565967967209469434
3748 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000007; expTmp3.mantissa=0x6ee19643f3ed5776L; };//221.76239823732856465394
3751 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000008; expTmp3.mantissa=0x4d465177242295efL; };//309.09872225312059774938
3754 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000007; expTmp3.mantissa=0x6c36c4f923819890L; };//216.42788614495947685003
3757 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000005; expTmp3.mantissa=0x783cc111991239a3L; };//60.118660497603843919306
3760 { expTmp3.mantissa = expTmp2.mantissa; expTmp3.exponent = expTmp2.exponent; expTmp3.sign = expTmp2.sign; };
3768 }
3769 /**
3770 * Calculates the natural logarithm (base-<i>e</i>) of this
3771 * <code>Real</code>.
3772 * Replaces the contents of this <code>Real</code> with the result.
3773 *
3774 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3775 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3776 * Equivalent </i><code>double</code><i> code:</i></td><td>
3777 * <code>this = Math.{@link Math#log(double) log}(this);</code>
3778 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3779 * 2 ULPs
3780 * </td></tr><tr><td><i>
3781 * Execution time relative to add:
3782 * </i></td><td>
3783 * 51
3784 * </td></tr></table>
3785 */
3791 }
3792 /**
3793 * Calculates the base-2 logarithm of this <code>Real</code>.
3794 * Replaces the contents of this <code>Real</code> with the result.
3795 *
3796 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3797 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3798 * Equivalent </i><code>double</code><i> code:</i></td><td>
3799 * <code>this = Math.{@link Math#log(double) log}(this)/Math.{@link
3800 * Math#log(double) log}(2);</code>
3801 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3802 * 1 ULPs
3803 * </td></tr><tr><td><i>
3804 * Execution time relative to add:
3805 * </i></td><td>
3806 * 51
3807 * </td></tr></table>
3808 */
3813 }
3814 /**
3815 * Calculates the base-10 logarithm of this <code>Real</code>.
3816 * Replaces the contents of this <code>Real</code> with the result.
3817 *
3818 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3819 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3820 * Equivalent </i><code>double</code><i> code:</i></td><td>
3821 * <code>this = Math.{@link Math#log10(double) log10}(this);</code>
3822 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3823 * 2 ULPs
3824 * </td></tr><tr><td><i>
3825 * Execution time relative to add:
3826 * </i></td><td>
3827 * 53
3828 * </td></tr></table>
3829 */
3836 }
3837 /**
3838 * Calculates the closest power of 10 that is less than or equal to this
3839 * <code>Real</code>.
3840 * Replaces the contents of this <code>Real</code> with the result.
3841 * The base-10 exponent of the result is returned.
3842 *
3843 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3844 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3845 * Equivalent </i><code>double</code><i> code:</i></td><td>
3846 * <code>int exp = (int)(Math.{@link Math#floor(double)
3847 * floor}(Math.{@link Math#log10(double) log10}(this)));
3848 * <br>this = Math.{@link Math#pow(double,double) pow}(10, exp);<br>
3849 * return exp;</code>
3850 * </td></tr><tr><td><i>Error bound:</i></td><td>
3851 * ½ ULPs
3852 * </td></tr><tr><td><i>
3853 * Execution time relative to add:
3854 * </i></td><td>
3855 * 3.6
3856 * </td></tr></table>
3857 *
3858 * @return the base-10 exponent
3859 */
3864 // Approximate log10 using exponent only
3868 else
3870 // Now, e < log10(this) < e+1
3879 }
3881 // First estimate of log10 was too low
3882 e++;
3884 }
3886 }
3887 /**
3888 * Calculates the value of this <code>Real</code> raised to the power of
3889 * <code>a</code>.
3890 * Replaces the contents of this <code>Real</code> with the result.
3891 *
3892 * <p> Special cases:
3893 * <ul>
3894 * <li> if a is 0.0 or -0.0 then result is 1.0
3895 * <li> if a is NaN then result is NaN
3896 * <li> if this is NaN and a is not zero then result is NaN
3897 * <li> if a is 1.0 then result is this
3898 * <li> if |this| > 1.0 and a is +Infinity then result is +Infinity
3899 * <li> if |this| < 1.0 and a is -Infinity then result is +Infinity
3900 * <li> if |this| > 1.0 and a is -Infinity then result is +0
3901 * <li> if |this| < 1.0 and a is +Infinity then result is +0
3902 * <li> if |this| = 1.0 and a is ±Infinity then result is NaN
3903 * <li> if this = +0 and a > 0 then result is +0
3904 * <li> if this = +0 and a < 0 then result is +Inf
3905 * <li> if this = -0 and a > 0, and odd integer then result is -0
3906 * <li> if this = -0 and a < 0, and odd integer then result is -Inf
3907 * <li> if this = -0 and a > 0, not odd integer then result is +0
3908 * <li> if this = -0 and a < 0, not odd integer then result is +Inf
3909 * <li> if this = +Inf and a > 0 then result is +Inf
3910 * <li> if this = +Inf and a < 0 then result is +0
3911 * <li> if this = -Inf and a not integer then result is NaN
3912 * <li> if this = -Inf and a > 0, and odd integer then result is -Inf
3913 * <li> if this = -Inf and a > 0, not odd integer then result is +Inf
3914 * <li> if this = -Inf and a < 0, and odd integer then result is -0
3915 * <li> if this = -Inf and a < 0, not odd integer then result is +0
3916 * <li> if this < 0 and a not integer then result is NaN
3917 * <li> if this < 0 and a odd integer then result is -(|this|<sup>a</sup>)
3918 * <li> if this < 0 and a not odd integer then result is |this|<sup>a</sup>
3919 * <li> else result is exp(ln(this)*a)
3920 * </ul>
3921 *
3922 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3923 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3924 * Equivalent </i><code>double</code><i> code:</i></td><td>
3925 * <code>this = Math.{@link Math#pow(double,double) pow}(this, a);</code>
3926 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
3927 * 2 ULPs
3928 * </td></tr><tr><td><i>
3929 * Execution time relative to add:
3930 * </i></td><td>
3931 * 110
3932 * </td></tr></table>
3933 *
3934 * @param a the <code>Real</code> argument.
3935 */
3940 }
3944 }
3954 else
3959 else
3963 }
3965 }
3970 else
3976 else
3981 else
3983 }
3984 }
3986 }
3991 else
3998 else
4003 else
4005 }
4008 }
4009 }
4011 }
4015 }
4024 }
4026 }
4029 // For increased accuracy, exponentiate with integer part of
4030 // exponent by successive squaring
4031 // (I really don't know why this works)
4039 }
4040 // Do log2 and maintain accuracy
4042 { tmp2.sign=(byte)0; tmp2.exponent=0x40000000; tmp2.mantissa=0x5c551d94ae0bf85dL; }; // log2(e)
4046 // Do exp2 of this multiplied by (fractional part of) exponent
4053 }
4054 /**
4055 * Calculates the value of this <code>Real</code> raised to the power of
4056 * the integer <code>a</code>.
4057 * Replaces the contents of this <code>Real</code> with the result.
4058 *
4059 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4060 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4061 * Equivalent </i><code>double</code><i> code:</i></td><td>
4062 * <code>this = Math.{@link Math#pow(double,double) pow}(this, a);</code>
4063 * </td></tr><tr><td><i>Error bound:</i></td><td>
4064 * ½ ULPs
4065 * </td></tr><tr><td><i>
4066 * Execution time relative to add:
4067 * </i></td><td>
4068 * 84
4069 * </td></tr></table>
4070 *
4071 * @param a the integer argument.
4072 */
4074 // Calculate power of integer by successive squaring
4079 }
4081 { expTmp.mantissa = this.mantissa; expTmp.exponent = this.exponent; expTmp.sign = this.sign; };
4087 }
4091 }
4093 /*
4094 * Adapted from:
4095 * Cephes Math Library Release 2.7: May, 1998
4096 * Copyright 1985, 1990, 1998 by Stephen L. Moshier
4097 *
4098 * sinl.c
4099 *
4100 * long double sinl(long double x);
4101 */
4102 // X<PI/4
4103 // polynomial approximation
4104 // sin(x) = x + x³ P(x²)
4108 { this.sign=(byte)1; this.exponent=0x3fffffd7; this.mantissa=0x6aa891c4f0eb2713L; };//-7.578540409484280575629E-13
4110 { tmp3.sign=(byte)0; tmp3.exponent=0x3fffffdf; tmp3.mantissa=0x58482311f383326cL; };//1.6058363167320443249231E-10
4113 { tmp3.sign=(byte)1; tmp3.exponent=0x3fffffe6; tmp3.mantissa=0x6b9914a35f9a00d8L; };//-2.5052104881870868784055E-8
4116 { tmp3.sign=(byte)0; tmp3.exponent=0x3fffffed; tmp3.mantissa=0x5c778e94cc22e47bL; };//2.7557319214064922217861E-6
4119 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffff3; tmp3.mantissa=0x680680680629b28aL; };//-1.9841269841254799668344E-4
4122 { tmp3.sign=(byte)0; tmp3.exponent=0x3ffffff9; tmp3.mantissa=0x4444444444442b4dL; };//8.3333333333333225058715E-3
4125 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffffd; tmp3.mantissa=0x555555555555554cL; };//-1.6666666666666666640255E-1
4130 }
4132 /*
4133 * Adapted from:
4134 * Cephes Math Library Release 2.7: May, 1998
4135 * Copyright 1985, 1990, 1998 by Stephen L. Moshier
4136 *
4137 * sinl.c
4138 *
4139 * long double cosl(long double x);
4140 */
4141 // X<PI/4
4142 // polynomial approximation
4143 // cos(x) = 1 - x²/2 + x**4 Q(x²)
4147 { this.sign=(byte)0; this.exponent=0x3fffffd3; this.mantissa=0x6aaf461d37ccba1bL; };//4.7377507964246204691685E-14
4149 { tmp3.sign=(byte)1; tmp3.exponent=0x3fffffdb; tmp3.mantissa=0x64e4c907ac7a179bL; };//-1.147028484342535976567E-11
4152 { tmp3.sign=(byte)0; tmp3.exponent=0x3fffffe3; tmp3.mantissa=0x47bb632432cf29a8L; };//2.0876754287081521758361E-9
4155 { tmp3.sign=(byte)1; tmp3.exponent=0x3fffffea; tmp3.mantissa=0x49f93edd7ae32696L; };//-2.7557319214999787979814E-7
4158 { tmp3.sign=(byte)0; tmp3.exponent=0x3ffffff0; tmp3.mantissa=0x68068068063329f7L; };//2.4801587301570552304991E-5L
4161 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffff6; tmp3.mantissa=0x5b05b05b05b03db3L; };//-1.3888888888888872993737E-3
4164 { tmp3.sign=(byte)0; tmp3.exponent=0x3ffffffb; tmp3.mantissa=0x555555555555554dL; };//4.1666666666666666609054E-2
4170 }
4171 /**
4172 * Calculates the trigonometric sine of this <code>Real</code>.
4173 * Replaces the contents of this <code>Real</code> with the result.
4174 * The input value is treated as an angle measured in radians.
4175 *
4176 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4177 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4178 * Equivalent </i><code>double</code><i> code:</i></td><td>
4179 * <code>this = Math.{@link Math#sin(double) sin}(this);</code>
4180 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4181 * 1 ULPs
4182 * </td></tr><tr><td><i>
4183 * Execution time relative to add:
4184 * </i></td><td>
4185 * 28
4186 * </td></tr></table>
4187 */
4193 }
4194 // Since sin(-x) = -sin(x) we can make sure that x > 0
4199 }
4200 // Then reduce the argument to the range of 0 < x < pi*2
4203 // Since sin(pi*2 - x) = -sin(x) we can reduce the range 0 < x < pi
4208 }
4209 // Since sin(x) = sin(pi - x) we can reduce the range to 0 < x < pi/2
4213 }
4214 // Since sin(x) = cos(pi/2 - x) we can reduce the range to 0 < x < pi/4
4221 }
4226 }
4227 /**
4228 * Calculates the trigonometric cosine of this <code>Real</code>.
4229 * Replaces the contents of this <code>Real</code> with the result.
4230 * The input value is treated as an angle measured in radians.
4231 *
4232 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4233 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4234 * Equivalent </i><code>double</code><i> code:</i></td><td>
4235 * <code>this = Math.{@link Math#cos(double) cos}(this);</code>
4236 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4237 * 1 ULPs
4238 * </td></tr><tr><td><i>
4239 * Execution time relative to add:
4240 * </i></td><td>
4241 * 37
4242 * </td></tr></table>
4243 */
4248 }
4256 }
4257 }
4258 /**
4259 * Calculates the trigonometric tangent of this <code>Real</code>.
4260 * Replaces the contents of this <code>Real</code> with the result.
4261 * The input value is treated as an angle measured in radians.
4262 *
4263 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4264 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4265 * Equivalent </i><code>double</code><i> code:</i></td><td>
4266 * <code>this = Math.{@link Math#tan(double) tan}(this);</code>
4267 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4268 * 2 ULPs
4269 * </td></tr><tr><td><i>
4270 * Execution time relative to add:
4271 * </i></td><td>
4272 * 70
4273 * </td></tr></table>
4274 */
4280 }
4281 /**
4282 * Calculates the trigonometric arc sine of this <code>Real</code>,
4283 * in the range -π/2 to π/2.
4284 * Replaces the contents of this <code>Real</code> with the result.
4285 *
4286 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4287 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4288 * Equivalent </i><code>double</code><i> code:</i></td><td>
4289 * <code>this = Math.{@link Math#asin(double) asin}(this);</code>
4290 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4291 * 3 ULPs
4292 * </td></tr><tr><td><i>
4293 * Execution time relative to add:
4294 * </i></td><td>
4295 * 68
4296 * </td></tr></table>
4297 */
4306 }
4307 /**
4308 * Calculates the trigonometric arc cosine of this <code>Real</code>,
4309 * in the range 0.0 to π.
4310 * Replaces the contents of this <code>Real</code> with the result.
4311 *
4312 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4313 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4314 * Equivalent </i><code>double</code><i> code:</i></td><td>
4315 * <code>this = Math.{@link Math#acos(double) acos}(this);</code>
4316 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4317 * 2 ULPs
4318 * </td></tr><tr><td><i>
4319 * Execution time relative to add:
4320 * </i></td><td>
4321 * 67
4322 * </td></tr></table>
4323 */
4337 }
4338 }
4339 /**
4340 * Calculates the trigonometric arc tangent of this <code>Real</code>,
4341 * in the range -π/2 to π/2.
4342 * Replaces the contents of this <code>Real</code> with the result.
4343 *
4344 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4345 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4346 * Equivalent </i><code>double</code><i> code:</i></td><td>
4347 * <code>this = Math.{@link Math#atan(double) atan}(this);</code>
4348 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4349 * 2 ULPs
4350 * </td></tr><tr><td><i>
4351 * Execution time relative to add:
4352 * </i></td><td>
4353 * 37
4354 * </td></tr></table>
4355 */
4357 /*
4358 * Adapted from:
4359 * Cephes Math Library Release 2.7: May, 1998
4360 * Copyright 1984, 1990, 1998 by Stephen L. Moshier
4361 *
4362 * atanl.c
4363 *
4364 * long double atanl(long double x);
4365 */
4373 }
4376 // range reduction
4393 }
4394 }
4395 // Now |X|<sqrt(2)-1
4396 // rational approximation
4397 // atan(x) = x + x³ P(x²)/Q(x²)
4402 { tmp3.sign=(byte)1; tmp3.exponent=0x3fffffff; tmp3.mantissa=0x6f2f89336729c767L; };//-0.8686381817809218753544
4404 { tmp4.sign=(byte)1; tmp4.exponent=0x40000003; tmp4.mantissa=0x7577d35fd03083f3L; };//-14.683508633175792446076
4407 { tmp4.sign=(byte)1; tmp4.exponent=0x40000005; tmp4.mantissa=0x7ff42abff948a9f7L; };//-63.976888655834347413154
4410 { tmp4.sign=(byte)1; tmp4.exponent=0x40000006; tmp4.mantissa=0x63fd1f9f76d37cebL; };//-99.988763777265819915721
4413 { tmp4.sign=(byte)1; tmp4.exponent=0x40000005; tmp4.mantissa=0x65c9c9b0b55e5b62L; };//-50.894116899623603312185
4417 { tmp4.sign=(byte)0; tmp4.exponent=0x40000004; tmp4.mantissa=0x5bed73b744a72a6aL; };//22.981886733594175366172
4420 { tmp4.sign=(byte)0; tmp4.exponent=0x40000007; tmp4.mantissa=0x47fed7d13d233b5cL; };//143.99096122250781605352
4423 { tmp4.sign=(byte)0; tmp4.exponent=0x40000008; tmp4.mantissa=0x5a5c35f774e071d5L; };//361.44079386152023162701
4426 { tmp4.sign=(byte)0; tmp4.exponent=0x40000008; tmp4.mantissa=0x61e4d84c2853d5e0L; };//391.57570175111990631099
4429 { tmp4.sign=(byte)0; tmp4.exponent=0x40000007; tmp4.mantissa=0x4c5757448806c48eL; };//152.68235069887081006606
4439 }
4440 /**
4441 * Calculates the trigonometric arc tangent of this
4442 * <code>Real</code> divided by <code>x</code>, in the range -π
4443 * to π. The signs of both arguments are used to determine the
4444 * quadrant of the result. Replaces the contents of this
4445 * <code>Real</code> with the result.
4446 *
4447 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4448 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4449 * Equivalent </i><code>double</code><i> code:</i></td><td>
4450 * <code>this = Math.{@link Math#atan2(double,double)
4451 * atan2}(this,x);</code>
4452 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4453 * 2 ULPs
4454 * </td></tr><tr><td><i>
4455 * Execution time relative to add:
4456 * </i></td><td>
4457 * 48
4458 * </td></tr></table>
4459 *
4460 * @param x the <code>Real</code> argument.
4461 */
4463 if ((this.exponent < 0 && this.mantissa != 0) || (x.exponent < 0 && x.mantissa != 0) || ((this.exponent < 0 && this.mantissa == 0) && (x.exponent < 0 && x.mantissa == 0))) {
4466 }
4478 }
4480 }
4481 /**
4482 * Calculates the hyperbolic sine of this <code>Real</code>.
4483 * Replaces the contents of this <code>Real</code> with the result.
4484 *
4485 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4486 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4487 * Equivalent </i><code>double</code><i> code:</i></td><td>
4488 * <code>this = Math.{@link Math#sinh(double) sinh}(this);</code>
4489 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4490 * 2 ULPs
4491 * </td></tr><tr><td><i>
4492 * Execution time relative to add:
4493 * </i></td><td>
4494 * 67
4495 * </td></tr></table>
4496 */
4504 }
4505 /**
4506 * Calculates the hyperbolic cosine of this <code>Real</code>.
4507 * Replaces the contents of this <code>Real</code> with the result.
4508 *
4509 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4510 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4511 * Equivalent </i><code>double</code><i> code:</i></td><td>
4512 * <code>this = Math.{@link Math#cosh(double) cosh}(this);</code>
4513 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4514 * 2 ULPs
4515 * </td></tr><tr><td><i>
4516 * Execution time relative to add:
4517 * </i></td><td>
4518 * 66
4519 * </td></tr></table>
4520 */
4528 }
4529 /**
4530 * Calculates the hyperbolic tangent of this <code>Real</code>.
4531 * Replaces the contents of this <code>Real</code> with the result.
4532 *
4533 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4534 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4535 * Equivalent </i><code>double</code><i> code:</i></td><td>
4536 * <code>this = Math.{@link Math#tanh(double) tanh}(this);</code>
4537 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4538 * 2 ULPs
4539 * </td></tr><tr><td><i>
4540 * Execution time relative to add:
4541 * </i></td><td>
4542 * 70
4543 * </td></tr></table>
4544 */
4554 }
4555 /**
4556 * Calculates the hyperbolic arc sine of this <code>Real</code>.
4557 * Replaces the contents of this <code>Real</code> with the result.
4558 *
4559 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4560 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4561 * Equivalent </i><code>double</code><i> code:</i></td><td>
4562 * <i>none</i>
4563 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4564 * 2 ULPs
4565 * </td></tr><tr><td><i>
4566 * Execution time relative to add:
4567 * </i></td><td>
4568 * 77
4569 * </td></tr></table>
4570 */
4574 // Use symmetry to prevent underflow error for very large negative
4575 // values
4586 }
4587 /**
4588 * Calculates the hyperbolic arc cosine of this <code>Real</code>.
4589 * Replaces the contents of this <code>Real</code> with the result.
4590 *
4591 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4592 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4593 * Equivalent </i><code>double</code><i> code:</i></td><td>
4594 * <i>none</i>
4595 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4596 * 2 ULPs
4597 * </td></tr><tr><td><i>
4598 * Execution time relative to add:
4599 * </i></td><td>
4600 * 75
4601 * </td></tr></table>
4602 */
4610 }
4611 /**
4612 * Calculates the hyperbolic arc tangent of this <code>Real</code>.
4613 * Replaces the contents of this <code>Real</code> with the result.
4614 *
4615 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4616 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4617 * Equivalent </i><code>double</code><i> code:</i></td><td>
4618 * <i>none</i>
4619 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4620 * 2 ULPs
4621 * </td></tr><tr><td><i>
4622 * Execution time relative to add:
4623 * </i></td><td>
4624 * 57
4625 * </td></tr></table>
4626 */
4635 }
4636 /**
4637 * Calculates the factorial of this <code>Real</code>.
4638 * Replaces the contents of this <code>Real</code> with the result.
4639 * The definition is generalized to all real numbers (not only integers),
4640 * by using the fact that <code>(n!)={@link #gamma() gamma}(n+1)</code>.
4641 *
4642 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4643 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4644 * Equivalent </i><code>double</code><i> code:</i></td><td>
4645 * <i>none</i>
4646 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4647 * 15 ULPs
4648 * </td></tr><tr><td><i>
4649 * Execution time relative to add:
4650 * </i></td><td>
4651 * 8-190
4652 * </td></tr></table>
4653 */
4658 {
4659 // x<0, x>200 or not integer: fact(x) = gamma(x+1)
4663 }
4669 }
4670 }
4671 /**
4672 * Calculates the gamma function for this <code>Real</code>.
4673 * Replaces the contents of this <code>Real</code> with the result.
4674 *
4675 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4676 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4677 * Equivalent </i><code>double</code><i> code:</i></td><td>
4678 * <i>none</i>
4679 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4680 * 100+ ULPs
4681 * </td></tr><tr><td><i>
4682 * Execution time relative to add:
4683 * </i></td><td>
4684 * 190
4685 * </td></tr></table>
4686 */
4690 // x<0: gamma(-x) = -pi/(x*gamma(x)*sin(pi*x))
4694 // x<n: gamma(x) = gamma(x+m)/x*(x+1)*(x+2)*...*(x+m-1)
4695 // n=20
4702 }
4703 // x>n: gamma(x) = exp((x-1/2)*ln(x) - x + ln(2*pi)/2 + 1/12x - 1/360x³
4704 // + 1/1260x**5 - 1/1680x**7+1/1188x**9)
4708 // (x-1/2)*ln(x)-x
4709 ln(); { tmp5.mantissa = tmp3.mantissa; tmp5.exponent = tmp3.exponent; tmp5.sign = tmp3.sign; }; tmp5.sub(HALF); mul(tmp5); sub(tmp3);
4710 // + ln(2*pi)/2
4711 { tmp5.sign=(byte)0; tmp5.exponent=0x3fffffff; tmp5.mantissa=0x759fc72192fad29aL; }; add(tmp5);
4712 // + 1/12x
4714 // - 1/360x³
4716 // + 1/1260x**5
4718 // - 1/1680x**7
4720 // + 1/1188x**9
4726 { tmp5.mantissa = tmp1.mantissa; tmp5.exponent = tmp1.exponent; tmp5.sign = tmp1.sign; }; // sin() uses tmp1
4727 // -pi/(x*gamma(x)*sin(pi*x))
4731 }
4732 }
4734 // 3 5 7 9
4735 // 2 / x x x x // erfc(x) = 1 - ------ | x - --- + ---- - ---- + ---- - ... |
4736 // sqrt(pi)\ 3 2!*5 3!*7 4!*9 /
4737 //
4739 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; }; tmp1Extra = 0;
4743 { tmp3.mantissa = ONE.mantissa; tmp3.exponent = ONE.exponent; tmp3.sign = ONE.sign; }; tmp3Extra = 0;
4754 i++;
4756 { tmp1.sign=(byte)1; tmp1.exponent=0x40000000; tmp1.mantissa=0x48375d410a6db446L; }; // -2/sqrt(pi)
4760 }
4762 // -x² -1
4763 // e x / 1 3 3*5 3*5*7 // erfc(x) = -------- | 1 - --- + ------ - ------ + ------ - ... |
4764 // sqrt(pi) \ 2x² 2 3 4 /
4765 // (2x²) (2x²) (2x²)
4766 // Calculate iteration stop criteria
4769 { tmp2.sign=(byte)0; tmp2.exponent=0x40000000; tmp2.mantissa=0x5c3811b4bfd0c8abL; }; // 1/0.694
4791 i++;
4798 { tmp1.sign=(byte)0; tmp1.exponent=0x3fffffff; tmp1.mantissa=0x48375d410a6db447L; }; // 1/sqrt(pi)
4800 }
4801 /**
4802 * Calculates the complementary error function for this <code>Real</code>.
4803 * Replaces the contents of this <code>Real</code> with the result.
4804 *
4805 * <p>The complementary error function is defined as the integral from
4806 * x to infinity of 2/√<span style="text-decoration:
4807 * overline;">π</span> ·<i>e</i><sup>-t²</sup> dt. It is
4808 * related to the error function, <i>erf</i>, by the formula
4809 * erfc(x)=1-erf(x).
4810 *
4811 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4812 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4813 * Equivalent </i><code>double</code><i> code:</i></td><td>
4814 * <i>none</i>
4815 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4816 * 2<sup>19</sup> ULPs
4817 * </td></tr><tr><td><i>
4818 * Execution time relative to add:
4819 * </i></td><td>
4820 * 80-4900
4821 * </td></tr></table>
4822 */
4829 }
4836 }
4842 else
4847 }
4848 }
4849 /**
4850 * Calculates the inverse complementary error function for this
4851 * <code>Real</code>.
4852 * Replaces the contents of this <code>Real</code> with the result.
4853 *
4854 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4855 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4856 * Equivalent </i><code>double</code><i> code:</i></td><td>
4857 * <i>none</i>
4858 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4859 * 2<sup>19</sup> ULPs
4860 * </td></tr><tr><td><i>
4861 * Execution time relative to add:
4862 * </i></td><td>
4863 * 240-5100
4864 * </td></tr></table>
4865 */
4870 }
4874 }
4878 }
4883 }
4887 }
4888 // Using invphi to calculate inverfc, like this
4889 // inverfc(x) = -invphi(x/2)/(sqrt(2))
4891 // Inverse Phi Algorithm (phi(Z)=P, so invphi(P)=Z)
4892 // ------------------------------------------------
4893 // Part 1: Numerical Approximation Method for Inverse Phi
4894 // This accepts input of P and outputs approximate Z as Y
4895 // Source:Odeh & Evans. 1974. AS 70. Applied Statistics.
4896 // R = sqrt(Ln(1/(Q²)))
4901 // Y = -(R+((((P4*R+P3)*R+P2)*R+P1)*R+P0)/((((Q4*R+Q3)*R*Q2)*R+Q1)*R+Q0))
4902 { tmp2.sign=(byte)1; tmp2.exponent=0x3ffffff1; tmp2.mantissa=0x5f22bb0fb4698674L; }; // P4=-0.0000453642210148
4904 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffffa; tmp3.mantissa=0x53a731ce1ea0be15L; }; // P3=-0.0204231210245
4907 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffffe; tmp3.mantissa=0x579d2d719fc517f3L; }; // P2=-0.342242088547
4912 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffffe; tmp3.mantissa=0x527dd3193bc8dd4cL; }; // P0=-0.322232431088
4914 { tmp3.sign=(byte)0; tmp3.exponent=0x3ffffff7; tmp3.mantissa=0x7e5b0f681d161e7dL; }; // Q4=0.0038560700634
4916 { tmp4.sign=(byte)0; tmp4.exponent=0x3ffffffc; tmp4.mantissa=0x6a05ccf9917da0a8L; }; // Q3=0.103537752850
4919 { tmp4.sign=(byte)0; tmp4.exponent=0x3fffffff; tmp4.mantissa=0x43fb32c0d3c14ec4L; }; // Q2=0.531103462366
4922 { tmp4.sign=(byte)0; tmp4.exponent=0x3fffffff; tmp4.mantissa=0x4b56a41226f4ba95L; }; // Q1=0.588581570495
4925 { tmp4.sign=(byte)0; tmp4.exponent=0x3ffffffc; tmp4.mantissa=0x65bb9a7733dd5062L; }; // Q0=0.0993484626060
4930 { sqrtTmp.mantissa = tmp1.mantissa; sqrtTmp.exponent = tmp1.exponent; sqrtTmp.sign = tmp1.sign; }; // sqrtTmp and tmp5 not used by erfc() and exp()
4931 // Part 2: Refine to accuracy of erfc Function
4932 // This accepts inputs Y and P (from above) and outputs Z
4933 // (Using Halley's third order method for finding roots of equations)
4934 // Q = erfc(-Y/sqrt(2))/2-P
4935 { tmp5.mantissa = sqrtTmp.mantissa; tmp5.exponent = sqrtTmp.exponent; tmp5.sign = sqrtTmp.sign; };
4941 // R = Q*sqrt(2*pi)*e^(Y²/2)
4942 { tmp3.mantissa = sqrtTmp.mantissa; tmp3.exponent = sqrtTmp.exponent; tmp3.sign = sqrtTmp.sign; };
4947 { tmp3.sign=(byte)0; tmp3.exponent=0x40000001; tmp3.mantissa=0x50364c7fd89c1659L; }; // sqrt(2*pi)
4949 // Z = Y-R/(1+R*Y/2)
4950 { this.mantissa = sqrtTmp.mantissa; this.exponent = sqrtTmp.exponent; this.sign = sqrtTmp.sign; };
4957 // calculate inverfc(x) = -invphi(x/2)/(sqrt(2))
4961 }
4962 //*************************************************************************
4963 // Calendar conversions taken from
4964 // http://www.fourmilab.ch/documents/calendar/
4969 }
4974 }
4978 }
4979 // GREGORIAN_TO_JD -- Determine Julian day number from Gregorian
4980 // calendar date -- Except that we use 1/1-0 as day 0
4989 }
4990 // JD_TO_GREGORIAN -- Calculate Gregorian calendar date from Julian
4991 // day -- Except that we use 1/1-0 as day 0
5006 year++;
5013 }
5014 /**
5015 * Converts this <code>Real</code> from "hours" to "days, hours,
5016 * minutes and seconds".
5017 * Replaces the contents of this <code>Real</code> with the result.
5018 *
5019 * <p>The format converted to is encoded into the digits of the
5020 * number (in decimal form):
5021 * "<code>DDDDhh.mmss</code>". Here "<code>DDDD</code>," is number
5022 * of days, "<code>hh</code>" is hours (0-23), "<code>mm</code>" is
5023 * minutes (0-59) and "<code>ss</code>" is seconds
5024 * (0-59). Additional digits represent fractions of a second.
5025 *
5026 * <p>If the number of hours of the input is greater or equal to
5027 * 8784 (number of hours in year <code>0</code>), the format
5028 * converted to is instead "<code>YYYYMMDDhh.mmss</code>". Here
5029 * "<code>YYYY</code>" is the number of years since the imaginary
5030 * year <code>0</code> in the Gregorian calendar, extrapolated back
5031 * from year 1582. "<code>MM</code>" is the month (1-12) and
5032 * "<code>DD</code>" is the day of the month (1-31). See a thorough
5033 * discussion of date calculations <a
5034 * href="http://midp-calc.sourceforge.net/Calc.html#DateNote">here</a>.
5035 *
5036 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5037 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5038 * Equivalent </i><code>double</code><i> code:</i></td><td>
5039 * <i>none</i>
5040 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
5041 * ?
5042 * </td></tr><tr><td><i>
5043 * Execution time relative to add:
5044 * </i></td><td>
5045 * 19
5046 * </td></tr></table>
5047 */
5062 // MAGIC ROUNDING: Check if we are 2**-16 sec short of a whole minute
5063 // i.e. "seconds" > 59.999985
5068 // Yes. So set zero secs instead and carry over to mins and hours
5070 m++;
5073 h++;
5074 }
5075 // Phew! That was close. From now on it is integer arithmetic...
5077 // Nope. So try to undo the damage...
5079 }
5090 }
5091 /**
5092 * Converts this <code>Real</code> from "days, hours, minutes and
5093 * seconds" to "hours".
5094 * Replaces the contents of this <code>Real</code> with the result.
5095 *
5096 * <p>The format converted from is encoded into the digits of the
5097 * number (in decimal form):
5098 * "<code>DDDDhh.mmss</code>". Here "<code>DDDD</code>" is number of
5099 * days, "<code>hh</code>" is hours (0-23), "<code>mm</code>" is
5100 * minutes (0-59) and "<code>ss</code>" is seconds
5101 * (0-59). Additional digits represent fractions of a second.
5102 *
5103 * <p>If the number of days in the input is greater than or equal to
5104 * 10000, the format converted from is instead
5105 * "<code>YYYYMMDDhh.mmss</code>". Here "<code>YYYY</code>" is the
5106 * number of years since the imaginary year <code>0</code> in the
5107 * Gregorian calendar, extrapolated back from year
5108 * 1582. "<code>MM</code>" is the month (1-12) and
5109 * "<code>DD</code>" is the day of the month (1-31). If month or day
5110 * is 0 it is treated as 1. See a thorough discussion of date
5111 * calculations <a
5112 * href="http://midp-calc.sourceforge.net/Calc.html#DateNote">here</a>.
5113 *
5114 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5115 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5116 * Equivalent </i><code>double</code><i> code:</i></td><td>
5117 * <i>none</i>
5118 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
5119 * ?
5120 * </td></tr><tr><td><i>
5121 * Execution time relative to add:
5122 * </i></td><td>
5123 * 19
5124 * </td></tr></table>
5125 */
5140 // MAGIC ROUNDING: Check if we are 2**-10 second short of 100 seconds
5141 // i.e. "seconds" > 99.999
5146 // Yes. So set zero secs instead and carry over to mins and hours
5148 m++;
5151 h++;
5152 }
5153 // Phew! That was close. From now on it is integer arithmetic...
5155 // Nope. So try to undo the damage...
5157 }
5168 }
5175 }
5176 /**
5177 * Assigns this <code>Real</code> the current time. The time is
5178 * encoded into the digits of the number (in decimal form), using the
5179 * format "<code>hh.mmss</code>", where "<code>hh</code>" is hours,
5180 * "<code>mm</code>" is minutes and "code>ss</code>" is seconds.
5181 *
5182 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5183 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5184 * Equivalent </i><code>double</code><i> code:</i></td><td>
5185 * <i>none</i>
5186 * </td></tr><tr><td><i>Error bound:</i></td><td>
5187 * ½ ULPs
5188 * </td></tr><tr><td><i>
5189 * Execution time relative to add:
5190 * </i></td><td>
5191 * 8.9
5192 * </td></tr></table>
5193 */
5205 }
5206 /**
5207 * Assigns this <code>Real</code> the current date. The date is
5208 * encoded into the digits of the number (in decimal form), using
5209 * the format "<code>YYYYMMDD00</code>", where "<code>YYYY</code>"
5210 * is the year, "<code>MM</code>" is the month (1-12) and
5211 * "<code>DD</code>" is the day of the month (1-31). The
5212 * "<code>00</code>" in this format is a sort of padding to make it
5213 * compatible with the format used by {@link #toDHMS()} and {@link
5214 * #fromDHMS()}.
5215 *
5216 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5217 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5218 * Equivalent </i><code>double</code><i> code:</i></td><td>
5219 * <i>none</i>
5220 * </td></tr><tr><td><i>Error bound:</i></td><td>
5221 * 0 ULPs
5222 * </td></tr><tr><td><i>
5223 * Execution time relative to add:
5224 * </i></td><td>
5225 * 30
5226 * </td></tr></table>
5227 */
5235 }
5236 //*************************************************************************
5237 /**
5238 * The seed of the first 64-bit CRC generator of the random
5239 * routine. Set this value to control the generated sequence of random
5240 * numbers. Should never be set to 0. See {@link #random()}.
5241 * Initialized to mantissa of pi.
5242 */
5244 /**
5245 * The seed of the second 64-bit CRC generator of the random
5246 * routine. Set this value to control the generated sequence of random
5247 * numbers. Should never be set to 0. See {@link #random()}.
5248 * Initialized to mantissa of e.
5249 */
5251 // 64 Bit CRC Generators
5252 //
5253 // The generators used here are not cryptographically secure, but
5254 // two weak generators are combined into one strong generator by
5255 // skipping bits from one generator whenever the other generator
5256 // produces a 0-bit.
5260 }
5261 // Get next bits from the pseudo-random sequence
5269 }
5271 }
5272 /**
5273 * Accumulate more randomness into the random number generator, to
5274 * decrease the predictability of the output from {@link
5275 * #random()}. The input should contain data with some form of
5276 * inherent randomness e.g. System.currentTimeMillis().
5277 *
5278 * @param seed some extra randomness for the random number generator.
5279 */
5284 }
5285 /**
5286 * Calculates a pseudorandom number in the range [0, 1).
5287 * Replaces the contents of this <code>Real</code> with the result.
5288 *
5289 * <p>The algorithm used is believed to be cryptographically secure,
5290 * combining two relatively weak 64-bit CRC generators into a strong
5291 * generator by skipping bits from one generator whenever the other
5292 * generator produces a 0-bit. The algorithm passes the <a
5293 * href="http://www.fourmilab.ch/random/">ent</a> test.
5294 *
5295 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5296 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5297 * Equivalent </i><code>double</code><i> code:</i></td><td>
5298 * <code>this = Math.{@link Math#random() random}();</code>
5299 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
5300 * -
5301 * </td></tr><tr><td><i>
5302 * Execution time relative to add:
5303 * </i></td><td>
5304 * 81
5305 * </td></tr></table>
5306 */
5311 exponent--;
5313 }
5314 //*************************************************************************
5326 }
5334 else
5336 }
5344 index++;
5347 index++;
5349 index++;
5351 // Input is twos complemented negative number
5354 index++;
5355 }
5360 index++;
5361 }
5364 index++;
5368 exp--;
5369 index++;
5370 }
5371 }
5375 index++;
5377 index++;
5382 index++;
5384 index++;
5385 }
5388 {
5389 // This takes care of overflows and makes inf or 0
5392 index++;
5393 }
5397 }
5406 // Kludge to be able to enter very large and very small
5407 // numbers without causing over/underflows
5415 }
5417 }
5425 }
5426 }
5428 }
5429 //*************************************************************************
5441 }
5442 }
5446 }
5452 exponent++;
5454 // Oh, no, not again!
5456 }
5457 }
5461 exponent--;
5462 }
5463 }
5466 {
5475 // Kludge to be able to print very large and very small numbers
5476 // without causing over/underflows
5481 else
5488 }
5491 else
5499 exponent++;
5506 }
5512 }
5516 }
5519 }
5529 exponent--;
5531 }
5532 }
5538 // More accurate to shift up instead
5540 exponent--;
5541 accurateBits--;
5542 }
5546 // Need to fill in some 1's at the top
5547 // (">>", not ">>>")
5549 }
5550 }
5551 }
5556 }
5559 }
5566 exponent++;
5571 }
5576 }
5577 }
5578 /**
5579 * The number format used to convert <code>Real</code> values to
5580 * <code>String</code> using {@link Real#toString(Real.NumberFormat)
5581 * Real.toString()}. The default number format uses base-10, maximum
5582 * precision, removal of trailing zeros and '.' as radix point.
5583 *
5584 * <p>Note that the fields of <code>NumberFormat</code> are not
5585 * protected in any way, the user is responsible for setting the
5586 * correct values to get a correct result.
5587 */
5588 public static class NumberFormat
5589 {
5590 /**
5591 * The number base of the conversion. The default value is 10,
5592 * valid options are 2, 8, 10 and 16. See {@link Real#and(Real)
5593 * Real.and()} for an explanation of the interpretation of a
5594 * <code>Real</code> in base 2, 8 and 16.
5595 *
5596 * <p>Negative numbers output in base-2, base-8 and base-16 are
5597 * shown in two's complement form. This form guarantees that a
5598 * negative number starts with at least one digit that is the
5599 * maximum digit for that base, i.e. '1', '7', and 'F',
5600 * respectively. A positive number is guaranteed to start with at
5601 * least one '0'. Both positive and negative numbers are extended
5602 * to the left using this digit, until {@link #maxwidth} is
5603 * reached.
5604 */
5606 /**
5607 * Maximum width of the converted string. The default value is 30.
5608 * If the conversion of a <code>Real</code> with a given {@link
5609 * #precision} would produce a string wider than
5610 * <code>maxwidth</code>, <code>precision</code> is reduced until
5611 * the number fits within the given width. If
5612 * <code>maxwidth</code> is too small to hold the number with its
5613 * sign, exponent and a <code>precision</code> of 1 digit, the
5614 * string may become wider than <code>maxwidth</code>.
5615 *
5616 * <p>If <code>align</code> is set to anything but
5617 * <code>ALIGN_NONE</code> and the converted string is shorter
5618 * than <code>maxwidth</code>, the resulting string is padded with
5619 * spaces to the specified width according to the alignment.
5620 */
5622 /**
5623 * The precision, or number of digits after the radix point in the
5624 * converted string when using the <i>FIX</i>, <i>SCI</i> or
5625 * <i>ENG</i> format (see {@link #fse}). The default value is 16,
5626 * valid values are 0-16 for base-10 and base-16 conversion, 0-21
5627 * for base-8 conversion, and 0-63 for base-2 conversion.
5628 *
5629 * <p>The <code>precision</code> may be reduced to make the number
5630 * fit within {@link #maxwidth}. The <code>precision</code> is
5631 * also reduced if it is set higher than the actual numbers of
5632 * significant digits in a <code>Real</code>. When
5633 * <code>fse</code> is set to <code>FSE_NONE</code>, i.e. "normal"
5634 * output, the precision is always at maximum, but trailing zeros
5635 * are removed.
5636 */
5638 /**
5639 * The special output formats <i>FIX</i>, <i>SCI</i> or <i>ENG</i>
5640 * are enabled with this field. The default value is
5641 * <code>FSE_NONE</code>. Valid options are listed below.
5642 *
5643 * <p>Numbers are output in one of two main forms, according to
5644 * this setting. The normal form has an optional sign, one or more
5645 * digits before the radix point, and zero or more digits after the
5646 * radix point, for example like this:<br>
5647 * <code> 3.14159</code><br>
5648 * The exponent form is like the normal form, followed by an
5649 * exponent marker 'e', an optional sign and one or more exponent
5650 * digits, for example like this:<br>
5651 * <code> -3.4753e-13</code>
5652 *
5653 * <p><dl>
5654 * <dt>{@link #FSE_NONE}
5655 * <dd>Normal output. Numbers are output with maximum precision,
5656 * trailing zeros are removed. The format is changed to
5657 * exponent form if the number is larger than the number of
5658 * significant digits allows, or if the resulting string would
5659 * exceed <code>maxwidth</code> without the exponent form.
5660 *
5661 * <dt>{@link #FSE_FIX}
5662 * <dd>Like normal output, but the numbers are output with a
5663 * fixed number of digits after the radix point, according to
5664 * {@link #precision}. Trailing zeros are not removed.
5665 *
5666 * <dt>{@link #FSE_SCI}
5667 * <dd>The numbers are always output in the exponent form, with
5668 * one digit before the radix point, and a fixed number of
5669 * digits after the radix point, according to
5670 * <code>precision</code>. Trailing zeros are not removed.
5671 *
5672 * <dt>{@link #FSE_ENG}
5673 * <dd>Like the <i>SCI</i> format, but the output shows one to
5674 * three digits before the radix point, so that the exponent is
5675 * always divisible by 3.
5676 * </dl>
5677 */
5679 /**
5680 * The character used as the radix point. The default value is
5681 * <code>'.'</code>. Theoretcally any character that does not
5682 * otherwise occur in the output can be used, such as
5683 * <code>','</code>.
5684 *
5685 * <p>Note that setting this to anything but <code>'.'</code> and
5686 * <code>','</code> is not supported by any conversion method from
5687 * <code>String</code> back to <code>Real</code>.
5688 */
5690 /**
5691 * Set to <code>true</code> to remove the radix point if this is
5692 * the last character in the converted string. This is the
5693 * default.
5694 */
5696 /**
5697 * The character used as the thousands separator. The default
5698 * value is the character code <code>0</code>, which disables
5699 * thousands-separation. Theoretcally any character that does not
5700 * otherwise occur in the output can be used, such as
5701 * <code>','</code> or <code>' '</code>.
5702 *
5703 * <p>When <code>thousand!=0</code>, this character is inserted
5704 * between every 3rd digit to the left of the radix point in
5705 * base-10 conversion. In base-16 conversion, the separator is
5706 * inserted between every 4th digit, and in base-2 conversion the
5707 * separator is inserted between every 8th digit. In base-8
5708 * conversion, no separator is ever inserted.
5709 *
5710 * <p>Note that tousands separators are not supported by any
5711 * conversion method from <code>String</code> back to
5712 * <code>Real</code>, so use of a thousands separator is meant
5713 * only for the presentation of numbers.
5714 */
5716 /**
5717 * The alignment of the output string within a field of {@link
5718 * #maxwidth} characters. The default value is
5719 * <code>ALIGN_NONE</code>. Valid options are defined as follows:
5720 *
5721 * <p><dl>
5722 * <dt>{@link #ALIGN_NONE}
5723 * <dd>The resulting string is not padded with spaces.
5724 *
5725 * <dt>{@link #ALIGN_LEFT}
5726 * <dd>The resulting string is padded with spaces on the right side
5727 * until a width of <code>maxwidth</code> is reached, making the
5728 * number left-aligned within the field.
5729 *
5730 * <dt>{@link #ALIGN_RIGHT}
5731 * <dd>The resulting string is padded with spaces on the left side
5732 * until a width of <code>maxwidth</code> is reached, making the
5733 * number right-aligned within the field.
5734 *
5735 * <dt>{@link #ALIGN_CENTER}
5736 * <dd>The resulting string is padded with spaces on both sides
5737 * until a width of <code>maxwidth</code> is reached, making the
5738 * number center-aligned within the field.
5739 * </dl>
5740 */
5742 /** Normal output {@linkplain #fse format} */
5744 /** <i>FIX</i> output {@linkplain #fse format} */
5746 /** <i>SCI</i> output {@linkplain #fse format} */
5748 /** <i>ENG</i> output {@linkplain #fse format} */
5750 /** No {@linkplain #align alignment} */
5752 /** Left {@linkplain #align alignment} */
5754 /** Right {@linkplain #align alignment} */
5756 /** Center {@linkplain #align alignment} */
5758 }
5771 }
5772 }
5774 }
5778 /**
5779 * This string holds the only valid characters to use in hexadecimal
5780 * numbers. Equals <code>"0123456789ABCDEF"</code>.
5781 * See {@link #assign(String,int)}.
5782 */
5789 }
5793 }
5809 }
5818 do
5819 {
5849 {
5857 // Add 1 for the decimal point that will be removed
5858 width++;
5859 }
5860 }
5862 }
5870 }
5879 }
5882 // Start generating the string. First the sign
5885 // Save pointPos for hex/oct/bin prefixing with thousands-sep
5887 // Add leading zeros (or f/7/1)
5895 pointPos++;
5896 }
5897 }
5898 // Add fractional part
5905 pointPos--;
5906 }
5908 // Remove trailing zeros
5911 }
5913 // Remove trailing point
5916 }
5917 // Add exponent
5919 // In case hex/oct/bin number, prefix with 0's or f/7/1's
5922 pointPos2++;
5927 }
5930 }
5932 }
5934 /**
5935 * Converts this <code>Real</code> to a <code>String</code> using
5936 * the default <code>NumberFormat</code>.
5937 *
5938 * <p>See {@link Real.NumberFormat NumberFormat} for a description
5939 * of the default way that numbers are formatted.
5940 *
5941 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5942 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5943 * Equivalent </i><code>double</code><i> code:</i></td><td>
5944 * <code>this.toString()
5945 * </td></tr><tr><td><i>
5946 * Execution time relative to add:
5947 * </i></td><td>
5948 * 130
5949 * </td></tr></table>
5950 *
5951 * @return a <code>String</code> representation of this <code>Real</code>.
5952 */
5956 }
5957 /**
5958 * Converts this <code>Real</code> to a <code>String</code> using
5959 * the default <code>NumberFormat</code> with <code>base</code> set
5960 * according to the argument.
5961 *
5962 * <p>See {@link Real.NumberFormat NumberFormat} for a description
5963 * of the default way that numbers are formatted.
5964 *
5965 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5966 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5967 * Equivalent </i><code>double</code><i> code:</i></td>
5968 * <td colspan="2">
5969 * <code>this.toString() // Works only for base-10</code>
5970 * </td></tr><tr><td rowspan="4" valign="top"><i>
5971 * Execution time relative to add: </i>
5972 * </td><td width="1%">base-2</td><td>
5973 * 120
5974 * </td></tr><tr><td>base-8</td><td>
5975 * 110
5976 * </td></tr><tr><td>base-10</td><td>
5977 * 130
5978 * </td></tr><tr><td>base-16 </td><td>
5979 * 120
5980 * </td></tr></table>
5981 *
5982 * @param base the base for the conversion. Valid base values are
5983 * 2, 8, 10 and 16.
5984 * @return a <code>String</code> representation of this <code>Real</code>.
5985 */
5989 }
5990 /**
5991 * Converts this <code>Real</code> to a <code>String</code> using
5992 * the given <code>NumberFormat</code>.
5993 *
5994 * <p>See {@link Real.NumberFormat NumberFormat} for a description of the
5995 * various ways that numbers may be formatted.
5996 *
5997 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5998 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5999 * Equivalent </i><code>double</code><i> code:</i></td>
6000 * <td colspan="2">
6001 * <code>String.format("%...g",this); // Works only for base-10</code>
6002 * </td></tr><tr><td rowspan="4" valign="top"><i>
6003 * Execution time relative to add: </i>
6004 * </td><td width="1%">base-2</td><td>
6005 * 120
6006 * </td></tr><tr><td>base-8</td><td>
6007 * 110
6008 * </td></tr><tr><td>base-10</td><td>
6009 * 130
6010 * </td></tr><tr><td>base-16 </td><td>
6011 * 120
6012 * </td></tr></table>
6013 *
6014 * @param format the number format to use in the conversion.
6015 * @return a <code>String</code> representation of this <code>Real</code>.
6016 */
6019 }
6020 }