8 * <b>Java integer implementation of 63-bit precision floating point.</b>
9 * <br><i>Version 1.13</i>
11 * <p>Copyright 2003-2009 Roar Lauritzsen <roarl@pvv.org>
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)
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
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
34 * <p><b>General notes</b>
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.
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>
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.
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.
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
88 public final class Real
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>
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>
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>.
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.
110 public long mantissa
;
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>
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>.
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
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>
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
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
143 * Set to <code>false</code> during numerical algorithms to favor accuracy
144 * over prettyness. This flag is initially set to <code>true</code>.
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.
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.
159 public static boolean magicRounding
= true;
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.
164 public static final Real ZERO
= new Real(0,0x00000000,0x0000000000000000L
);
166 * A <code>Real</code> constant holding the exact value of 1.
168 public static final Real ONE
= new Real(0,0x40000000,0x4000000000000000L
);
170 * A <code>Real</code> constant holding the exact value of 2.
172 public static final Real TWO
= new Real(0,0x40000001,0x4000000000000000L
);
174 * A <code>Real</code> constant holding the exact value of 3.
176 public static final Real THREE
= new Real(0,0x40000001,0x6000000000000000L
);
178 * A <code>Real</code> constant holding the exact value of 5.
180 public static final Real FIVE
= new Real(0,0x40000002,0x5000000000000000L
);
182 * A <code>Real</code> constant holding the exact value of 10.
184 public static final Real TEN
= new Real(0,0x40000003,0x5000000000000000L
);
186 * A <code>Real</code> constant holding the exact value of 100.
188 public static final Real HUNDRED
=new Real(0,0x40000006,0x6400000000000000L
);
190 * A <code>Real</code> constant holding the exact value of 1/2.
192 public static final Real HALF
= new Real(0,0x3fffffff,0x4000000000000000L
);
194 * A <code>Real</code> constant that is closer than any other to 1/3.
196 public static final Real THIRD
= new Real(0,0x3ffffffe,0x5555555555555555L
);
198 * A <code>Real</code> constant that is closer than any other to 1/10.
200 public static final Real TENTH
= new Real(0,0x3ffffffc,0x6666666666666666L
);
202 * A <code>Real</code> constant that is closer than any other to 1/100.
204 public static final Real PERCENT
=new Real(0,0x3ffffff9,0x51eb851eb851eb85L
);
206 * A <code>Real</code> constant that is closer than any other to the
209 public static final Real SQRT2
= new Real(0,0x40000000,0x5a827999fcef3242L
);
211 * A <code>Real</code> constant that is closer than any other to the
212 * square root of 1/2.
214 public static final Real SQRT1_2
=new Real(0,0x3fffffff,0x5a827999fcef3242L
);
216 * A <code>Real</code> constant that is closer than any other to 2π.
218 public static final Real PI2
= new Real(0,0x40000002,0x6487ed5110b4611aL
);
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.
223 public static final Real PI
= new Real(0,0x40000001,0x6487ed5110b4611aL
);
225 * A <code>Real</code> constant that is closer than any other to π/2.
227 public static final Real PI_2
= new Real(0,0x40000000,0x6487ed5110b4611aL
);
229 * A <code>Real</code> constant that is closer than any other to π/4.
231 public static final Real PI_4
= new Real(0,0x3fffffff,0x6487ed5110b4611aL
);
233 * A <code>Real</code> constant that is closer than any other to π/8.
235 public static final Real PI_8
= new Real(0,0x3ffffffe,0x6487ed5110b4611aL
);
237 * A <code>Real</code> constant that is closer than any other to <i>e</i>,
238 * the base of the natural logarithms.
240 public static final Real E
= new Real(0,0x40000001,0x56fc2a2c515da54dL
);
242 * A <code>Real</code> constant that is closer than any other to the
243 * natural logarithm of 2.
245 public static final Real LN2
= new Real(0,0x3fffffff,0x58b90bfbe8e7bcd6L
);
247 * A <code>Real</code> constant that is closer than any other to the
248 * natural logarithm of 10.
250 public static final Real LN10
= new Real(0,0x40000001,0x49aec6eed554560bL
);
252 * A <code>Real</code> constant that is closer than any other to the
253 * base-2 logarithm of <i>e</i>.
255 public static final Real LOG2E
= new Real(0,0x40000000,0x5c551d94ae0bf85eL
);
257 * A <code>Real</code> constant that is closer than any other to the
258 * base-10 logarithm of <i>e</i>.
260 public static final Real LOG10E
=new Real(0,0x3ffffffe,0x6f2dec549b9438cbL
);
262 * A <code>Real</code> constant holding the maximum non-infinite positive
263 * number = 4.197e323228496.
265 public static final Real MAX
= new Real(0,0x7fffffff,0x7fffffffffffffffL
);
267 * A <code>Real</code> constant holding the minimum non-zero positive
268 * number = 2.383e-323228497.
270 public static final Real MIN
= new Real(0,0x00000000,0x4000000000000000L
);
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.
275 public static final Real NAN
= new Real(0,0x80000000,0x4000000000000000L
);
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.
280 public static final Real INF
= new Real(0,0x80000000,0x0000000000000000L
);
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.
285 public static final Real INF_N
= new Real(1,0x80000000,0x0000000000000000L
);
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.
290 public static final Real ZERO_N
=new Real(1,0x00000000,0x0000000000000000L
);
292 * A <code>Real</code> constant holding the exact value of -1.
294 public static final Real ONE_N
= new Real(1,0x40000000,0x4000000000000000L
);
295 private static final int clz_magic
= 0x7c4acdd;
296 private static final byte[] clz_tab
=
297 { 31,22,30,21,18,10,29, 2,20,17,15,13, 9, 6,28, 1,
298 23,19,11, 3,16,14, 7,24,12, 4, 8,25, 5,26,27, 0 };
300 * Creates a new <code>Real</code> with a value of zero.
305 * Creates a new <code>Real</code>, assigning the value of another
306 * <code>Real</code>. See {@link #assign(Real)}.
308 * @param a the <code>Real</code> to assign.
310 public Real(Real a
) {
311 { this.mantissa
= a
.mantissa
; this.exponent
= a
.exponent
; this.sign
= a
.sign
; };
314 * Creates a new <code>Real</code>, assigning the value of an integer. See
315 * {@link #assign(int)}.
317 * @param a the <code>int</code> to assign.
323 * Creates a new <code>Real</code>, assigning the value of a long
324 * integer. See {@link #assign(long)}.
326 * @param a the <code>long</code> to assign.
328 public Real(long a
) {
332 * Creates a new <code>Real</code>, assigning the value encoded in a
333 * <code>String</code> using base-10. See {@link #assign(String)}.
335 * @param a the <code>String</code> to assign.
337 public Real(String a
) {
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)}.
345 * @param a the <code>String</code> to assign.
346 * @param base the number base of <code>a</code>. Valid base values are 2,
349 public Real(String a
, int base
) {
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)}.
358 * @param s {@link #sign} bit, 0 for positive sign, 1 for negative sign
359 * @param e {@link #exponent}
360 * @param m {@link #mantissa}
362 public Real(int s
, int e
, long m
) {
363 { this.sign
=(byte)s
; this.exponent
=e
; this.mantissa
=m
; };
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)}.
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
375 public Real(byte [] data
, int offset
) {
379 * Assigns this <code>Real</code> the value of another <code>Real</code>.
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>
387 * </td></tr><tr><td><i>
388 * Execution time relative to add:
393 * @param a the <code>Real</code> to assign.
395 public void assign(Real a
) {
401 exponent
= a
.exponent
;
402 mantissa
= a
.mantissa
;
405 * Assigns this <code>Real</code> the value of an integer.
406 * All integer values can be represented without loss of accuracy.
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>
414 * </td></tr><tr><td><i>
415 * Execution time relative to add:
420 * @param a the <code>int</code> to assign.
422 public void assign(int a
) {
430 a
= -a
; // Also works for 0x80000000
433 int t
=a
; t
|=t
>>1; t
|=t
>>2; t
|=t
>>4; t
|=t
>>8; t
|=t
>>16;
434 t
= clz_tab
[(t
*clz_magic
)>>>27]-1;
435 exponent
= 0x4000001E-t
;
436 mantissa
= ((long)a
)<<(32+t
);
439 * Assigns this <code>Real</code> the value of a signed long integer.
440 * All long values can be represented without loss of accuracy.
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>
448 * </td></tr><tr><td><i>
449 * Execution time relative to add:
454 * @param a the <code>long</code> to assign.
456 public void assign(long a
) {
460 a
= -a
; // Also works for 0x8000000000000000
462 exponent
= 0x4000003E;
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)}.
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>
476 * </td></tr><tr><td><i>
477 * Execution time relative to add:
482 * @param a the <code>String</code> to assign.
484 public void assign(String a
) {
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:
492 * <li>If the string is <code>null</code> or an empty string, zero is
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.
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'.
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>.
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>
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.
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>
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>
536 * </td></tr><tr><td>2/8/16</td><td>
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>
542 * </td></tr><tr><td>base-8</td><td>
544 * </td></tr><tr><td>base-10</td><td>
546 * </td></tr><tr><td>base-16 </td><td>
550 * @param a the <code>String</code> to assign.
551 * @param base the number base of <code>a</code>. Valid base values are
554 public void assign(String a
, int base
) {
555 if (a
==null || a
.length()==0) {
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
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>
575 * </td></tr><tr><td><i>
576 * Execution time relative to add:
581 * @param s {@link #sign} bit, 0 for positive sign, 1 for negative sign
582 * @param e {@link #exponent}
583 * @param m {@link #mantissa}
585 public void assign(int s
, int e
, long m
) {
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}.
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>
599 * </td></tr><tr><td><i>
600 * Execution time relative to add:
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
610 public void assign(byte [] data
, int offset
) {
611 sign
= (byte)((data
[offset
+4]>>7)&1);
612 exponent
= (((data
[offset
]&0xff)<<24)+
613 ((data
[offset
+1]&0xff)<<16)+
614 ((data
[offset
+2]&0xff)<<8)+
615 ((data
[offset
+3]&0xff)));
616 mantissa
= (((long)(data
[offset
+ 4]&0x7f)<<56)+
617 ((long)(data
[offset
+ 5]&0xff)<<48)+
618 ((long)(data
[offset
+ 6]&0xff)<<40)+
619 ((long)(data
[offset
+ 7]&0xff)<<32)+
620 ((long)(data
[offset
+ 8]&0xff)<<24)+
621 ((long)(data
[offset
+ 9]&0xff)<<16)+
622 ((long)(data
[offset
+10]&0xff)<< 8)+
623 ( (data
[offset
+11]&0xff)));
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)}.
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:
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
642 public void toBytes(byte [] data
, int offset
) {
643 data
[offset
] = (byte)(exponent
>>24);
644 data
[offset
+ 1] = (byte)(exponent
>>16);
645 data
[offset
+ 2] = (byte)(exponent
>>8);
646 data
[offset
+ 3] = (byte)(exponent
);
647 data
[offset
+ 4] = (byte)((sign
<<7)+(mantissa
>>56));
648 data
[offset
+ 5] = (byte)(mantissa
>>48);
649 data
[offset
+ 6] = (byte)(mantissa
>>40);
650 data
[offset
+ 7] = (byte)(mantissa
>>32);
651 data
[offset
+ 8] = (byte)(mantissa
>>24);
652 data
[offset
+ 9] = (byte)(mantissa
>>16);
653 data
[offset
+10] = (byte)(mantissa
>>8);
654 data
[offset
+11] = (byte)(mantissa
);
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.
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>
669 * </td></tr><tr><td><i>
670 * Execution time relative to add:
675 * @param bits a <code>float</code> value encoded in an <code>int</code>.
677 public void assignFloatBits(int bits
) {
678 sign
= (byte)(bits
>>>31);
679 exponent
= (bits
>>23)&0xff;
680 mantissa
= (long)(bits
&0x007fffff)<<39;
681 if (exponent
== 0 && mantissa
== 0)
682 return; // Valid zero
683 if (exponent
== 0 && mantissa
!= 0) {
684 // Degenerate small float
685 exponent
= 0x40000000-126;
689 if (exponent
<= 254) {
690 // Normal IEEE 754 float
691 exponent
+= 0x40000000-127;
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.
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>
713 * </td></tr><tr><td><i>
714 * Execution time relative to add:
719 * @param bits a <code>double</code> value encoded in a <code>long</code>.
721 public void assignDoubleBits(long bits
) {
722 sign
= (byte)((bits
>>63)&1);
723 exponent
= (int)((bits
>>52)&0x7ff);
724 mantissa
= (bits
&0x000fffffffffffffL
)<<10;
725 if (exponent
== 0 && mantissa
== 0)
726 return; // Valid zero
727 if (exponent
== 0 && mantissa
!= 0) {
728 // Degenerate small float
729 exponent
= 0x40000000-1022;
733 if (exponent
<= 2046) {
734 // Normal IEEE 754 float
735 exponent
+= 0x40000000-1023;
745 * Returns a representation of this <code>Real</code> according to the
746 * IEEE 754 floating-point "single format" bit layout.
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:
759 * @return the bits that represent the floating-point number.
761 public int toFloatBits() {
762 if ((this.exponent
< 0 && this.mantissa
!= 0))
763 return 0x7fffffff; // nan
764 int e
= exponent
-0x40000000+127;
771 if (exponent
< 0) // Overflow
772 return (sign
<<31)|0x7f800000; // inf
774 if ((this.exponent
< 0 && this.mantissa
== 0) || e
> 254)
775 return (sign
<<31)|0x7f800000; // inf
776 if ((this.exponent
== 0 && this.mantissa
== 0) || e
< -22)
777 return (sign
<<31); // zero
778 if (e
<= 0) // Degenerate small float
779 return (sign
<<31)|((int)(m
>>>(40-e
))&0x007fffff);
780 // Normal IEEE 754 float
781 return (sign
<<31)|(e
<<23)|((int)(m
>>>39)&0x007fffff);
784 * Returns a representation of this <code>Real</code> according to the
785 * IEEE 754 floating-point "double format" bit layout.
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:
798 * @return the bits that represent the floating-point number.
800 public long toDoubleBits() {
801 if ((this.exponent
< 0 && this.mantissa
!= 0))
802 return 0x7fffffffffffffffL
; // nan
803 int e
= exponent
-0x40000000+1023;
811 return ((long)sign
<<63)|0x7ff0000000000000L
; // inf
813 if ((this.exponent
< 0 && this.mantissa
== 0) || e
> 2046)
814 return ((long)sign
<<63)|0x7ff0000000000000L
; // inf
815 if ((this.exponent
== 0 && this.mantissa
== 0) || e
< -51)
816 return ((long)sign
<<63); // zero
817 if (e
<= 0) // Degenerate small double
818 return ((long)sign
<<63)|((m
>>>(11-e
))&0x000fffffffffffffL
);
819 // Normal IEEE 754 double
820 return ((long)sign
<<63)|((long)e
<<52)|((m
>>>10)&0x000fffffffffffffL
);
823 * Makes this <code>Real</code> the value of positive zero.
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:
835 public void makeZero() {
841 * Makes this <code>Real</code> the value of zero with the specified sign.
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:
853 * @param s sign bit, 0 to make a positive zero, 1 to make a negative zero
855 public void makeZero(int s
) {
861 * Makes this <code>Real</code> the value of infinity with the specified
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}
869 * </td></tr><tr><td><i>
870 * Execution time relative to add:
875 * @param s sign bit, 0 to make positive infinity, 1 to make negative
878 public void makeInfinity(int s
) {
881 exponent
= 0x80000000;
884 * Makes this <code>Real</code> the value of Not-a-Number (NaN).
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:
896 public void makeNan() {
898 mantissa
= 0x4000000000000000L
;
899 exponent
= 0x80000000;
902 * Returns <code>true</code> if the value of this <code>Real</code> is
903 * zero, <code>false</code> otherwise.
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:
915 * @return <code>true</code> if the value represented by this object is
916 * zero, <code>false</code> otherwise.
918 public boolean isZero() {
919 return (exponent
== 0 && mantissa
== 0);
922 * Returns <code>true</code> if the value of this <code>Real</code> is
923 * infinite, <code>false</code> otherwise.
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:
935 * @return <code>true</code> if the value represented by this object is
936 * infinite, <code>false</code> if it is finite or NaN.
938 public boolean isInfinity() {
939 return (exponent
< 0 && mantissa
== 0);
942 * Returns <code>true</code> if the value of this <code>Real</code> is
943 * Not-a-Number (NaN), <code>false</code> otherwise.
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:
955 * @return <code>true</code> if the value represented by this object is
956 * NaN, <code>false</code> otherwise.
958 public boolean isNan() {
959 return (exponent
< 0 && mantissa
!= 0);
962 * Returns <code>true</code> if the value of this <code>Real</code> is
963 * finite, <code>false</code> otherwise.
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:
977 * @return <code>true</code> if the value represented by this object is
978 * finite, <code>false</code> if it is infinite or NaN.
980 public boolean isFinite() {
981 // That is, non-infinite and non-nan
982 return (exponent
>= 0);
985 * Returns <code>true</code> if the value of this <code>Real</code> is
986 * finite and nonzero, <code>false</code> otherwise.
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) &&
994 * </td></tr><tr><td><i>
995 * Execution time relative to add:
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
1004 public boolean isFiniteNonZero() {
1005 // That is, non-infinite and non-nan and non-zero
1006 return (exponent
>= 0 && mantissa
!= 0);
1009 * Returns <code>true</code> if the value of this <code>Real</code> is
1010 * negative, <code>false</code> otherwise.
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:
1020 * </td></tr></table>
1022 * @return <code>true</code> if the value represented by this object
1023 * is negative, <code>false</code> if it is positive or NaN.
1025 public boolean isNegative() {
1029 * Calculates the absolute value.
1030 * Replaces the contents of this <code>Real</code> with the result.
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>
1038 * </td></tr><tr><td><i>
1039 * Execution time relative to add:
1042 * </td></tr></table>
1048 * Negates this <code>Real</code>.
1049 * Replaces the contents of this <code>Real</code> with the result.
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>
1057 * </td></tr><tr><td><i>
1058 * Execution time relative to add:
1061 * </td></tr></table>
1064 if (!(this.exponent
< 0 && this.mantissa
!= 0))
1068 * Copies the sign from <code>a</code>.
1069 * Replaces the contents of this <code>Real</code> with the result.
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>
1078 * </td></tr><tr><td><i>
1079 * Execution time relative to add:
1082 * </td></tr></table>
1084 * @param a the <code>Real</code> to copy the sign from.
1086 public void copysign(Real a
) {
1087 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
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
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>
1106 * </td></tr><tr><td><i>
1107 * Execution time relative to add:
1110 * </td></tr></table>
1112 public void normalize() {
1113 if ((this.exponent
>= 0)) {
1117 int t
= (int)(mantissa
>>>32);
1118 if (t
== 0) { clz
= 32; t
= (int)mantissa
; }
1119 t
|=t
>>1; t
|=t
>>2; t
|=t
>>4; t
|=t
>>8; t
|=t
>>16;
1120 clz
+= clz_tab
[(t
*clz_magic
)>>>27]-1;
1123 if (exponent
< 0) // Underflow
1126 else if (mantissa
< 0)
1128 mantissa
= (mantissa
+1)>>>1;
1130 if (mantissa
== 0) { // Ooops, it was 0xffffffffffffffffL
1131 mantissa
= 0x4000000000000000L
;
1134 if (exponent
< 0) // Overflow
1137 else // mantissa == 0
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.
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:
1160 * </td></tr></table>
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>.
1167 public long normalize128(long extra
) {
1168 if (!(this.exponent
>= 0))
1170 if (mantissa
== 0) {
1178 if (exponent
< 0) { // Underflow
1184 extra
= (mantissa
<<63)+(extra
>>>1);
1187 if (exponent
< 0) { // Overflow
1194 int t
= (int)(mantissa
>>>32);
1195 if (t
== 0) { clz
= 32; t
= (int)mantissa
; }
1196 t
|=t
>>1; t
|=t
>>2; t
|=t
>>4; t
|=t
>>8; t
|=t
>>16;
1197 clz
+= clz_tab
[(t
*clz_magic
)>>>27]-1;
1200 mantissa
= (mantissa
<<clz
)+(extra
>>>(64-clz
));
1203 if (exponent
< 0) { // Underflow
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.
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>
1218 * </td></tr><tr><td><i>
1219 * Execution time relative to add:
1222 * </td></tr></table>
1224 * @param extra the extra 64 bits of mantissa of this extended precision
1225 * <code>Real</code>.
1227 public void roundFrom128(long extra
) {
1228 mantissa
+= (extra
>>63)&1;
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)}.
1238 * @param a the object to compare to this.
1239 * @return <code>true</code> if this object is the same as <code>a</code>.
1241 public boolean equals(Object a
) {
1244 private int compare(Real a
) {
1245 // Compare of normal floats, zeros, but not nan or equal-signed inf
1246 if ((this.exponent
== 0 && this.mantissa
== 0) && (a
.exponent
== 0 && a
.mantissa
== 0))
1250 int s
= (this.sign
==0) ?
1 : -1;
1251 if ((this.exponent
< 0 && this.mantissa
== 0))
1253 if ((a
.exponent
< 0 && a
.mantissa
== 0))
1255 if (exponent
!= a
.exponent
)
1256 return exponent
<a
.exponent ?
-s
: s
;
1257 if (mantissa
!= a
.mantissa
)
1258 return mantissa
<a
.mantissa ?
-s
: s
;
1261 private boolean invalidCompare(Real a
) {
1262 return ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0) ||
1263 ((this.exponent
< 0 && this.mantissa
== 0) && (a
.exponent
< 0 && a
.mantissa
== 0) && sign
== a
.sign
));
1266 * Returns <code>true</code> if this <code>Real</code> is equal to
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)}.
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:
1280 * </td></tr></table>
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.
1287 public boolean equalTo(Real a
) {
1288 if (invalidCompare(a
))
1290 return compare(a
) == 0;
1293 * Returns <code>true</code> if this <code>Real</code> is equal to
1294 * the integer <code>a</code>.
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:
1304 * </td></tr></table>
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>
1311 public boolean equalTo(int a
) {
1313 return equalTo(tmp0
);
1316 * Returns <code>true</code> if this <code>Real</code> is not equal to
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
1321 * This distinguishes <code>notEqualTo(a)</code> from the expression
1322 * <code>!equalTo(a)</code>.
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:
1332 * </td></tr></table>
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.
1339 public boolean notEqualTo(Real a
) {
1340 if (invalidCompare(a
))
1342 return compare(a
) != 0;
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
1349 * This distinguishes <code>notEqualTo(a)</code> from the expression
1350 * <code>!equalTo(a)</code>.
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:
1360 * </td></tr></table>
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.
1367 public boolean notEqualTo(int a
) {
1369 return notEqualTo(tmp0
);
1372 * Returns <code>true</code> if this <code>Real</code> is less than
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
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:
1386 * </td></tr></table>
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.
1393 public boolean lessThan(Real a
) {
1394 if (invalidCompare(a
))
1396 return compare(a
) < 0;
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
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:
1412 * </td></tr></table>
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.
1419 public boolean lessThan(int a
) {
1421 return lessThan(tmp0
);
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
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:
1438 * </td></tr></table>
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.
1445 public boolean lessEqual(Real a
) {
1446 if (invalidCompare(a
))
1448 return compare(a
) <= 0;
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
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:
1464 * </td></tr></table>
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.
1471 public boolean lessEqual(int a
) {
1473 return lessEqual(tmp0
);
1476 * Returns <code>true</code> if this <code>Real</code> is greater than
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
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:
1490 * </td></tr></table>
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.
1497 public boolean greaterThan(Real a
) {
1498 if (invalidCompare(a
))
1500 return compare(a
) > 0;
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
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:
1516 * </td></tr></table>
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.
1523 public boolean greaterThan(int a
) {
1525 return greaterThan(tmp0
);
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
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:
1542 * </td></tr></table>
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.
1549 public boolean greaterEqual(Real a
) {
1550 if (invalidCompare(a
))
1552 return compare(a
) >= 0;
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
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:
1568 * </td></tr></table>
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.
1575 public boolean greaterEqual(int a
) {
1577 return greaterEqual(tmp0
);
1580 * Returns <code>true</code> if the absolute value of this
1581 * <code>Real</code> is less than the absolute value of
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.
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:
1595 * </td></tr></table>
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
1601 * <code>false</code> otherwise, or if the numbers are incomparable.
1603 public boolean absLessThan(Real a
) {
1604 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0) || (this.exponent
< 0 && this.mantissa
== 0))
1606 if ((a
.exponent
< 0 && a
.mantissa
== 0))
1608 if (exponent
!= a
.exponent
)
1609 return exponent
<a
.exponent
;
1610 return mantissa
<a
.mantissa
;
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.
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>
1624 * </td></tr><tr><td><i>
1625 * Execution time relative to add:
1628 * </td></tr></table>
1630 * @param n the integer argument.
1632 public void scalbn(int n
) {
1633 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
1638 makeZero(sign
); // Underflow
1640 makeInfinity(sign
); // Overflow
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.
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>
1656 * </td></tr><tr><td><i>
1657 * Execution time relative to add:
1660 * </td></tr></table>
1662 * @param a the <code>Real</code> argument.
1664 public void nextafter(Real a
) {
1665 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
1669 if ((this.exponent
< 0 && this.mantissa
== 0) && (a
.exponent
< 0 && a
.mantissa
== 0) && sign
== a
.sign
)
1671 int dir
= -compare(a
);
1674 if ((this.exponent
== 0 && this.mantissa
== 0)) {
1675 { this.mantissa
= MIN
.mantissa
; this.exponent
= MIN
.exponent
; this.sign
= MIN
.sign
; };
1676 sign
= (byte)(dir
<0 ?
1 : 0);
1679 if ((this.exponent
< 0 && this.mantissa
== 0)) {
1680 { this.mantissa
= MAX
.mantissa
; this.exponent
= MAX
.exponent
; this.sign
= MAX
.sign
; };
1681 sign
= (byte)(dir
<0 ?
0 : 1);
1684 if ((this.sign
==0) ^ dir
<0) {
1687 if (mantissa
== 0x4000000000000000L
) {
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.
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>
1707 * </td></tr><tr><td><i>
1708 * Execution time relative to add:
1711 * </td></tr></table>
1713 public void floor() {
1714 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
1716 if (exponent
< 0x40000000) {
1720 exponent
= ONE
.exponent
;
1721 mantissa
= ONE
.mantissa
;
1726 int shift
= 0x4000003e-exponent
;
1730 mantissa
+= ((1L<<shift
)-1);
1731 mantissa
&= ~
((1L<<shift
)-1);
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.
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>
1747 * </td></tr><tr><td><i>
1748 * Execution time relative to add:
1751 * </td></tr></table>
1753 public void ceil() {
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.
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>
1771 * </td></tr><tr><td><i>
1772 * Execution time relative to add:
1775 * </td></tr></table>
1777 public void round() {
1778 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
1780 if (exponent
< 0x3fffffff) {
1784 int shift
= 0x4000003e-exponent
;
1787 mantissa
+= 1L<<(shift
-1); // Bla-bla, this works almost
1788 mantissa
&= ~
((1L<<shift
)-1);
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.
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>
1802 * </td></tr><tr><td><i>
1803 * Execution time relative to add:
1806 * </td></tr></table>
1808 public void trunc() {
1809 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
1811 if (exponent
< 0x40000000) {
1815 int shift
= 0x4000003e-exponent
;
1818 mantissa
&= ~
((1L<<shift
)-1);
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.
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>
1832 * </td></tr><tr><td><i>
1833 * Execution time relative to add:
1836 * </td></tr></table>
1838 public void frac() {
1839 if (!(this.exponent
>= 0 && this.mantissa
!= 0) || exponent
< 0x40000000)
1841 int shift
= 0x4000003e-exponent
;
1846 mantissa
&= ((1L<<shift
)-1);
1850 * Converts this <code>Real</code> value to the closest <code>int</code>
1851 * value towards zero.
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.
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>
1866 * </td></tr><tr><td><i>
1867 * Execution time relative to add:
1870 * </td></tr></table>
1872 * @return an <code>int</code> representation of this <code>Real</code>.
1874 public int toInteger() {
1875 if ((this.exponent
== 0 && this.mantissa
== 0) || (this.exponent
< 0 && this.mantissa
!= 0))
1877 if ((this.exponent
< 0 && this.mantissa
== 0)) {
1878 return ((this.sign
==0)) ?
0x7fffffff : 0x80000001;
1879 // 0x80000001, so that you can take -x.toInteger()
1881 if (exponent
< 0x40000000)
1883 int shift
= 0x4000003e-exponent
;
1885 return ((this.sign
==0)) ?
0x7fffffff : 0x80000001;
1886 // 0x80000001, so that you can take -x.toInteger()
1888 return (this.sign
==0) ?
1889 (int)(mantissa
>>>shift
) : -(int)(mantissa
>>>shift
);
1892 * Converts this <code>Real</code> value to the closest <code>long</code>
1893 * value towards zero.
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.
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>
1908 * </td></tr><tr><td><i>
1909 * Execution time relative to add:
1912 * </td></tr></table>
1914 * @return a <code>long</code> representation of this <code>Real</code>.
1916 public long toLong() {
1917 if ((this.exponent
== 0 && this.mantissa
== 0) || (this.exponent
< 0 && this.mantissa
!= 0))
1919 if ((this.exponent
< 0 && this.mantissa
== 0)) {
1920 return ((this.sign
==0))?
0x7fffffffffffffffL
:0x8000000000000001L
;
1921 // 0x8000000000000001L, so that you can take -x.toLong()
1923 if (exponent
< 0x40000000)
1925 int shift
= 0x4000003e-exponent
;
1927 return ((this.sign
==0))?
0x7fffffffffffffffL
:0x8000000000000001L
;
1928 // 0x8000000000000001L, so that you can take -x.toLong()
1930 return (this.sign
==0) ?
(mantissa
>>>shift
) : -(mantissa
>>>shift
);
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.
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:
1945 * </td></tr></table>
1947 * @return <code>true</code> if the value represented by this object
1948 * represents a mathematical integer, <code>false</code> otherwise.
1950 public boolean isIntegral() {
1951 if ((this.exponent
< 0 && this.mantissa
!= 0))
1953 if ((this.exponent
== 0 && this.mantissa
== 0) || (this.exponent
< 0 && this.mantissa
== 0))
1955 if (exponent
< 0x40000000)
1957 int shift
= 0x4000003e-exponent
;
1960 return (mantissa
&((1L<<shift
)-1)) == 0;
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.
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:
1977 * </td></tr></table>
1979 * @return <code>true</code> if the mathematical integer represented by
1980 * this <code>Real</code> is odd, <code>false</code> otherwise.
1982 public boolean isOdd() {
1983 if (!(this.exponent
>= 0 && this.mantissa
!= 0) ||
1984 exponent
< 0x40000000 || exponent
> 0x4000003e)
1986 int shift
= 0x4000003e-exponent
;
1987 return ((mantissa
>>>shift
)&1) != 0;
1990 * Exchanges the contents of this <code>Real</code> and <code>a</code>.
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>
1998 * </td></tr><tr><td><i>
1999 * Execution time relative to add:
2002 * </td></tr></table>
2004 * @param a the <code>Real</code> to exchange with this.
2006 public void swap(Real a
) {
2007 long tmpMantissa
=mantissa
; mantissa
=a
.mantissa
; a
.mantissa
=tmpMantissa
;
2008 int tmpExponent
=exponent
; exponent
=a
.exponent
; a
.exponent
=tmpExponent
;
2009 byte tmpSign
=sign
; sign
=a
.sign
; a
.sign
=tmpSign
;
2011 // Temporary values used by functions (to avoid "new" inside functions)
2012 private static Real tmp0
= new Real(); // tmp for basic functions
2013 private static Real recipTmp
= new Real();
2014 private static Real recipTmp2
= new Real();
2015 private static Real sqrtTmp
= new Real();
2016 private static Real expTmp
= new Real();
2017 private static Real expTmp2
= new Real();
2018 private static Real expTmp3
= new Real();
2019 private static Real tmp1
= new Real();
2020 private static Real tmp2
= new Real();
2021 private static Real tmp3
= new Real();
2022 private static Real tmp4
= new Real();
2023 private static Real tmp5
= new Real();
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.
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>
2034 * </td></tr><tr><td><i>
2035 * Execution time relative to add:
2038 * </td></tr></table>
2040 * @param a the <code>Real</code> to add to this.
2042 public void add(Real a
) {
2043 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
2047 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0)) {
2048 if ((this.exponent
< 0 && this.mantissa
== 0) && (a
.exponent
< 0 && a
.mantissa
== 0) && sign
!= a
.sign
)
2051 makeInfinity((this.exponent
< 0 && this.mantissa
== 0) ? sign
: a
.sign
);
2054 if ((this.exponent
== 0 && this.mantissa
== 0) || (a
.exponent
== 0 && a
.mantissa
== 0)) {
2055 if ((this.exponent
== 0 && this.mantissa
== 0))
2056 { this.mantissa
= a
.mantissa
; this.exponent
= a
.exponent
; this.sign
= a
.sign
; };
2057 if ((this.exponent
== 0 && this.mantissa
== 0))
2064 if (exponent
> a
.exponent
||
2065 (exponent
== a
.exponent
&& mantissa
>=a
.mantissa
))
2075 exponent
= a
.exponent
;
2076 mantissa
= a
.mantissa
;
2078 int shift
= exponent
-e
;
2082 mantissa
+= m
>>>shift
;
2083 if (mantissa
>= 0 && shift
>0 && ((m
>>>(shift
-1))&1) != 0)
2084 mantissa
++; // We don't need normalization, so round now
2086 // Simplified normalize()
2087 mantissa
= (mantissa
+1)>>>1;
2089 if (exponent
< 0) { // Overflow
2096 // Shift mantissa up to increase accuracy
2102 mantissa
+= m
>>shift
;
2103 if (mantissa
>= 0 && shift
>0 && ((m
>>>(shift
-1))&1) != 0)
2104 mantissa
++; // We don't need to shift down, so round now
2106 // Simplified normalize()
2107 mantissa
= (mantissa
+1)>>>1;
2108 exponent
++; // Can't overflow
2109 } else if (shift
==0) {
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...
2113 if (magicRounding
&& mantissa
> 0 && mantissa
<= 7) {
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.
2119 if (exponent
== 0x4000003c || exponent
== 0x4000003d ||
2120 (exponent
== 0x4000003e && mantissa
+m
> 0)) {
2121 long mask
= (1<<(0x4000003e-exponent
))-1;
2122 if ((mantissa
& mask
) != 0 || (m
& mask
) != 0)
2128 } // else... if (shift>=1 && mantissa>=0) it should be a-ok
2130 if ((this.exponent
== 0 && this.mantissa
== 0))
2134 * Calculates the sum of this <code>Real</code> and the integer
2136 * Replaces the contents of this <code>Real</code> with the result.
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>
2144 * </td></tr><tr><td><i>
2145 * Execution time relative to add:
2148 * </td></tr></table>
2150 * @param a the <code>int</code> to add to this.
2152 public void add(int a
) {
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
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.
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)}.
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:
2182 * </td></tr></table>
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>.
2192 public long add128(long extra
, Real a
, long aExtra
) {
2193 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
2197 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0)) {
2198 if ((this.exponent
< 0 && this.mantissa
== 0) && (a
.exponent
< 0 && a
.mantissa
== 0) && sign
!= a
.sign
)
2201 makeInfinity((this.exponent
< 0 && this.mantissa
== 0) ? sign
: a
.sign
);
2204 if ((this.exponent
== 0 && this.mantissa
== 0) || (a
.exponent
== 0 && a
.mantissa
== 0)) {
2205 if ((this.exponent
== 0 && this.mantissa
== 0)) {
2206 { this.mantissa
= a
.mantissa
; this.exponent
= a
.exponent
; this.sign
= a
.sign
; };
2209 if ((this.exponent
== 0 && this.mantissa
== 0))
2217 if (exponent
> a
.exponent
||
2218 (exponent
== a
.exponent
&& mantissa
>a
.mantissa
) ||
2219 (exponent
== a
.exponent
&& mantissa
==a
.mantissa
&&
2220 (extra
>>>1)>=(aExtra
>>>1)))
2232 exponent
= a
.exponent
;
2233 mantissa
= a
.mantissa
;
2236 int shift
= exponent
-e
;
2242 } else if (shift
>0) {
2243 x
= (x
>>>shift
)+(m
<<(64-shift
));
2250 mantissa
+= (extra
>>63)&1;
2254 mantissa
-= (extra
>>63)&1;
2256 // Magic rounding: if result of subtract leaves only a few bits
2257 // standing, the result should most likely be 0...
2258 if (mantissa
== 0 && extra
> 0 && extra
<= 0x1f)
2262 extra
= normalize128(extra
);
2263 if ((this.exponent
== 0 && this.mantissa
== 0))
2268 * Calculates the difference between this <code>Real</code> and
2270 * Replaces the contents of this <code>Real</code> with the result.
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.)
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>
2283 * </td></tr><tr><td><i>
2284 * Execution time relative to add:
2287 * </td></tr></table>
2289 * @param a the <code>Real</code> to subtract from this.
2291 public void sub(Real a
) {
2292 tmp0
.mantissa
= a
.mantissa
;
2293 tmp0
.exponent
= a
.exponent
;
2294 tmp0
.sign
= (byte)(a
.sign^
1);
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.
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>
2308 * </td></tr><tr><td><i>
2309 * Execution time relative to add:
2312 * </td></tr></table>
2314 * @param a the <code>int</code> to subtract from this.
2316 public void sub(int a
) {
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.
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>
2330 * </td></tr><tr><td><i>
2331 * Execution time relative to add:
2334 * </td></tr></table>
2336 * @param a the <code>Real</code> to multiply to this.
2338 public void mul(Real a
) {
2339 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
2344 if ((this.exponent
== 0 && this.mantissa
== 0) || (a
.exponent
== 0 && a
.mantissa
== 0)) {
2345 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0))
2351 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0)) {
2355 long a0
= mantissa
& 0x7fffffff;
2356 long a1
= mantissa
>>> 31;
2357 long b0
= a
.mantissa
& 0x7fffffff;
2358 long b1
= a
.mantissa
>>> 31;
2360 // If we're going to need normalization, we don't want to round twice
2361 int round
= (mantissa
<0) ?
0 : 0x40000000;
2362 mantissa
+= ((a0
*b1
+ a1
*b0
+ ((a0
*b0
)>>>31) + round
)>>>31);
2363 int aExp
= a
.exponent
;
2364 exponent
+= aExp
-0x40000000;
2366 if (exponent
== -1 && aExp
< 0x40000000 && mantissa
< 0) {
2367 // Not underflow after all, it will be corrected in the
2368 // normalization below
2370 if (aExp
< 0x40000000)
2371 makeZero(sign
); // Underflow
2373 makeInfinity(sign
); // Overflow
2377 // Simplified normalize()
2379 mantissa
= (mantissa
+1)>>>1;
2381 if (exponent
< 0) // Overflow
2386 * Calculates the product of this <code>Real</code> and the integer
2388 * Replaces the contents of this <code>Real</code> with the result.
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>
2396 * </td></tr><tr><td><i>
2397 * Execution time relative to add:
2400 * </td></tr></table>
2402 * @param a the <code>int</code> to multiply to this.
2404 public void mul(int a
) {
2405 if ((this.exponent
< 0 && this.mantissa
!= 0))
2411 if ((this.exponent
== 0 && this.mantissa
== 0) || a
==0) {
2412 if ((this.exponent
< 0 && this.mantissa
== 0))
2418 if ((this.exponent
< 0 && this.mantissa
== 0))
2421 int t
=a
; t
|=t
>>1; t
|=t
>>2; t
|=t
>>4; t
|=t
>>8; t
|=t
>>16;
2422 t
= clz_tab
[(t
*clz_magic
)>>>27];
2426 makeInfinity(sign
); // Overflow
2429 long a0
= mantissa
& 0x7fffffff;
2430 long a1
= mantissa
>>> 31;
2431 long b0
= a
& 0xffffffffL
;
2433 // If we're going to need normalization, we don't want to round twice
2434 int round
= (mantissa
<0) ?
0 : 0x40000000;
2435 mantissa
+= ((a0
*b0
+ round
)>>>31);
2436 // Simplified normalize()
2438 mantissa
= (mantissa
+1)>>>1;
2440 if (exponent
< 0) // Overflow
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.
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)}.
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:
2465 * </td></tr></table>
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>.
2475 public long mul128(long extra
, Real a
, long aExtra
) {
2476 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
2481 if ((this.exponent
== 0 && this.mantissa
== 0) || (a
.exponent
== 0 && a
.mantissa
== 0)) {
2482 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0))
2488 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0)) {
2492 int aExp
= a
.exponent
;
2493 exponent
+= aExp
-0x40000000;
2495 if (aExp
< 0x40000000)
2496 makeZero(sign
); // Underflow
2498 makeInfinity(sign
); // Overflow
2501 long ffffffffL
= 0xffffffffL
;
2502 long a0
= extra
& ffffffffL
;
2503 long a1
= extra
>>> 32;
2504 long a2
= mantissa
& ffffffffL
;
2505 long a3
= mantissa
>>> 32;
2506 long b0
= aExtra
& ffffffffL
;
2507 long b1
= aExtra
>>> 32;
2508 long b2
= a
.mantissa
& ffffffffL
;
2509 long b3
= a
.mantissa
>>> 32;
2515 //(a2*b0>>>34)+(a1*b1>>>34)+(a0*b2>>>34)+0x08000000)>>>28;
2519 a0
+= ((a1
<<2)&ffffffffL
) + ((b0
<<2)&ffffffffL
) + ((b1
<<2)&ffffffffL
);
2520 a1
= (a0
>>>32) + (a1
>>>30) + (b0
>>>30) + (b1
>>>30);
2524 a1
+= ((a2
<<2)&ffffffffL
) + ((b2
<<2)&ffffffffL
);
2525 extra
= (a1
<<32) + a0
;
2526 mantissa
= ((a3
*b3
)<<2) + (a1
>>>32) + (a2
>>>30) + (b2
>>>30);
2527 extra
= normalize128(extra
);
2530 private void mul10() {
2531 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
2533 mantissa
+= (mantissa
+2)>>>2;
2536 mantissa
= (mantissa
+1)>>>1;
2540 makeInfinity(sign
); // Overflow
2543 * Calculates the square of this <code>Real</code>.
2544 * Replaces the contents of this <code>Real</code> with the result.
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>
2552 * </td></tr><tr><td><i>
2553 * Execution time relative to add:
2556 * </td></tr></table>
2560 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
2563 exponent
+= exponent
-0x40000000;
2566 makeZero(sign
); // Underflow
2568 makeInfinity(sign
); // Overflow
2571 long a0
= mantissa
&0x7fffffff;
2572 long a1
= mantissa
>>>31;
2574 // If we're going to need normalization, we don't want to round twice
2575 int round
= (mantissa
<0) ?
0 : 0x40000000;
2576 mantissa
+= ((((a0
*a1
)<<1) + ((a0
*a0
)>>>31) + round
)>>>31);
2577 // Simplified normalize()
2579 mantissa
= (mantissa
+1)>>>1;
2581 if (exponent
< 0) // Overflow
2585 private static long ldiv(long a
, long b
) {
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"
2592 int bInv24
; // Approximate 1/b, never more than 24 bits
2593 int aHi24
; // High 24 bits of a (sometimes 25 bits)
2594 int next21
; // The next 21 bits of result, possibly 1 less
2595 long q
; // Resulting quotient: round((a<<63)/b)
2597 bInv24
= (int)(0x400000000000L
/((b
>>>40)+1));
2598 aHi24
= (int)(a
>>32)>>>8;
2599 a
<<= 20; // aHi24 and a overlap by 4 bits
2600 // Now perform the division
2601 next21
= (int)(((long)aHi24
*(long)bInv24
)>>>26);
2602 a
-= next21
*b
; // Bits above 2**64 will always be cancelled
2603 // No need to remove remainder, this will be cared for in next block
2605 aHi24
= (int)(a
>>32)>>>7;
2607 // Two more almost identical blocks...
2608 next21
= (int)(((long)aHi24
*(long)bInv24
)>>>26);
2611 aHi24
= (int)(a
>>32)>>>7;
2613 next21
= (int)(((long)aHi24
*(long)bInv24
)>>>26);
2616 // Remove final remainder
2617 if (a
<0 || a
>=b
) { q
++; a
-= b
; }
2620 if (a
<0 || a
>=b
) q
++;
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.
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>.)
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>
2638 * </td></tr><tr><td><i>
2639 * Execution time relative to add:
2642 * </td></tr></table>
2644 * @param a the <code>Real</code> to divide this with.
2646 public void div(Real a
) {
2647 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
2652 if ((this.exponent
< 0 && this.mantissa
== 0)) {
2653 if ((a
.exponent
< 0 && a
.mantissa
== 0))
2657 if ((a
.exponent
< 0 && a
.mantissa
== 0)) {
2661 if ((this.exponent
== 0 && this.mantissa
== 0)) {
2662 if ((a
.exponent
== 0 && a
.mantissa
== 0))
2666 if ((a
.exponent
== 0 && a
.mantissa
== 0)) {
2670 exponent
+= 0x40000000-a
.exponent
;
2671 if (mantissa
< a
.mantissa
) {
2676 if (a
.exponent
>= 0x40000000)
2677 makeZero(sign
); // Underflow
2679 makeInfinity(sign
); // Overflow
2682 if (a
.mantissa
== 0x4000000000000000L
)
2684 mantissa
= ldiv(mantissa
,a
.mantissa
);
2687 * Calculates the quotient of this <code>Real</code> and the integer
2689 * Replaces the contents of this <code>Real</code> with the result.
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>
2697 * </td></tr><tr><td><i>
2698 * Execution time relative to add:
2701 * </td></tr></table>
2703 * @param a the <code>int</code> to divide this with.
2705 public void div(int a
) {
2706 if ((this.exponent
< 0 && this.mantissa
!= 0))
2712 if ((this.exponent
< 0 && this.mantissa
== 0))
2714 if ((this.exponent
== 0 && this.mantissa
== 0)) {
2723 long denom
= a
& 0xffffffffL
;
2724 long remainder
= mantissa
%denom
;
2726 // Normalizing mantissa and scaling remainder accordingly
2728 int t
= (int)(mantissa
>>>32);
2729 if (t
== 0) { clz
= 32; t
= (int)mantissa
; }
2730 t
|=t
>>1; t
|=t
>>2; t
|=t
>>4; t
|=t
>>8; t
|=t
>>16;
2731 clz
+= clz_tab
[(t
*clz_magic
)>>>27]-1;
2735 // Final division, correctly rounded
2736 remainder
= (remainder
+denom
/2)/denom
;
2737 mantissa
+= remainder
;
2738 if (exponent
< 0) // Underflow
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.
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>
2751 * </td></tr><tr><td><i>
2752 * Execution time relative to add:
2755 * </td></tr></table>
2757 * @param a the <code>Real</code> to be divided by this.
2759 public void rdiv(Real a
) {
2760 { recipTmp
.mantissa
= a
.mantissa
; recipTmp
.exponent
= a
.exponent
; recipTmp
.sign
= a
.sign
; };
2762 { this.mantissa
= recipTmp
.mantissa
; this.exponent
= recipTmp
.exponent
; this.sign
= recipTmp
.sign
; };
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.
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>
2775 * </td></tr><tr><td><i>
2776 * Execution time relative to add:
2779 * </td></tr></table>
2781 * @param a the <code>int</code> to be divided by this.
2783 public void rdiv(int a
) {
2788 * Calculates the reciprocal of this <code>Real</code>.
2789 * Replaces the contents of this <code>Real</code> with the result.
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>
2797 * </td></tr><tr><td><i>
2798 * Execution time relative to add:
2801 * </td></tr></table>
2803 public void recip() {
2804 if ((this.exponent
< 0 && this.mantissa
!= 0))
2806 if ((this.exponent
< 0 && this.mantissa
== 0)) {
2810 if ((this.exponent
== 0 && this.mantissa
== 0)) {
2814 exponent
= 0x80000000-exponent
;
2815 if (mantissa
== 0x4000000000000000L
) {
2817 makeInfinity(sign
); // Overflow
2821 mantissa
= ldiv(0x8000000000000000L
,mantissa
);
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.
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)}.
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:
2843 * </td></tr></table>
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>.
2850 public long recip128(long extra
) {
2851 if ((this.exponent
< 0 && this.mantissa
!= 0))
2853 if ((this.exponent
< 0 && this.mantissa
== 0)) {
2857 if ((this.exponent
== 0 && this.mantissa
== 0)) {
2863 // Special case, simple power of 2
2864 if (mantissa
== 0x4000000000000000L
&& extra
== 0) {
2865 exponent
= 0x80000000-exponent
;
2866 if (exponent
<0) // Overflow
2870 // Normalize exponent
2871 int exp
= 0x40000000-exponent
;
2872 exponent
= 0x40000000;
2874 { recipTmp
.mantissa
= this.mantissa
; recipTmp
.exponent
= this.exponent
; recipTmp
.sign
= this.sign
; };
2875 long recipTmpExtra
= extra
;
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
; };
2882 extra
= mul128(0,recipTmp
,recipTmpExtra
);
2883 extra
= add128(extra
,ONE
,0);
2884 extra
= mul128(extra
,recipTmp2
,0);
2885 extra
= add128(extra
,recipTmp2
,0);
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.
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>
2904 * </td></tr><tr><td><i>
2905 * Execution time relative to add:
2908 * </td></tr></table>
2910 * @param a the <code>Real</code> argument.
2912 public void divf(Real a
) {
2913 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
2917 if ((this.exponent
< 0 && this.mantissa
== 0)) {
2918 if ((a
.exponent
< 0 && a
.mantissa
== 0))
2922 if ((a
.exponent
< 0 && a
.mantissa
== 0)) {
2926 if ((this.exponent
== 0 && this.mantissa
== 0)) {
2927 if ((a
.exponent
== 0 && a
.mantissa
== 0))
2931 if ((a
.exponent
== 0 && a
.mantissa
== 0)) {
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
2937 long extra
= tmp0
.recip128(0);
2938 extra
= mul128(0,tmp0
,extra
);
2939 if (((tmp0
.sign
!=0) && (extra
< 0 || extra
> 0x1f)) ||
2940 (!(tmp0
.sign
!=0) && extra
< 0 && extra
> 0xffffffe0))
2942 // For accurate floor()
2948 private void modInternal(/*long thisExtra,*/ Real a
, long aExtra
) {
2949 { tmp0
.mantissa
= a
.mantissa
; tmp0
.exponent
= a
.exponent
; tmp0
.sign
= a
.sign
; }; // tmp0 should be free
2950 long extra
= tmp0
.recip128(aExtra
);
2951 extra
= tmp0
.mul128(extra
,this,0/*thisExtra*/); // tmp0 == this/a
2952 if (tmp0
.exponent
> 0x4000003e) {
2953 // floor() will be inaccurate
2954 makeZero(a
.sign
); // What else can be done? makeNan?
2957 if (((tmp0
.sign
!=0) && (extra
< 0 || extra
> 0x1f)) ||
2958 (!(tmp0
.sign
!=0) && extra
< 0 && extra
> 0xffffffe0))
2960 // For accurate floor() with a bit of "magical rounding"
2965 tmp0
.neg(); // tmp0 == -floor(this/a)
2966 extra
= tmp0
.mul128(0,a
,aExtra
);
2967 extra
= add128(0/*thisExtra*/,tmp0
,extra
);
2968 roundFrom128(extra
);
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>.
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>
2984 * </td></tr><tr><td><i>
2985 * Execution time relative to add:
2988 * </td></tr></table>
2990 * @param a the <code>Real</code> argument.
2992 public void mod(Real a
) {
2993 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
2997 if ((this.exponent
< 0 && this.mantissa
== 0)) {
3001 if ((this.exponent
== 0 && this.mantissa
== 0)) {
3002 if ((a
.exponent
== 0 && a
.mantissa
== 0))
3008 if ((a
.exponent
< 0 && a
.mantissa
== 0)) {
3010 makeInfinity(a
.sign
);
3013 if ((a
.exponent
== 0 && a
.mantissa
== 0)) {
3020 * Calculates the logical <i>AND</i> of this <code>Real</code> and
3022 * Replaces the contents of this <code>Real</code> with the result.
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.
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.
3044 * <p>This method calculates a negative value if and only
3045 * if this and <code>a</code> are both negative.
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>
3053 * </td></tr><tr><td><i>
3054 * Execution time relative to add:
3057 * </td></tr></table>
3059 * @param a the <code>Real</code> argument
3061 public void and(Real a
) {
3062 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
3066 if ((this.exponent
== 0 && this.mantissa
== 0) || (a
.exponent
== 0 && a
.mantissa
== 0)) {
3070 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0)) {
3071 if (!(this.exponent
< 0 && this.mantissa
== 0) && (this.sign
!=0)) {
3072 { this.mantissa
= a
.mantissa
; this.exponent
= a
.exponent
; this.sign
= a
.sign
; };
3073 } else if (!(a
.exponent
< 0 && a
.mantissa
== 0) && (a
.sign
!=0))
3074 ; // ASSIGN(this,this)
3075 else if ((this.exponent
< 0 && this.mantissa
== 0) && (a
.exponent
< 0 && a
.mantissa
== 0) &&
3076 (this.sign
!=0) && (a
.sign
!=0))
3077 ; // makeInfinity(1)
3085 if (exponent
>= a
.exponent
) {
3094 exponent
= a
.exponent
;
3095 mantissa
= a
.mantissa
;
3097 int shift
= exponent
-e
;
3106 mantissa
= -mantissa
;
3107 mantissa
&= m
>>shift
;
3110 mantissa
= -mantissa
;
3116 * Calculates the logical <i>OR</i> of this <code>Real</code> and
3118 * Replaces the contents of this <code>Real</code> with the result.
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.
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>
3131 * </td></tr><tr><td><i>
3132 * Execution time relative to add:
3135 * </td></tr></table>
3137 * @param a the <code>Real</code> argument
3139 public void or(Real a
) {
3140 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
3144 if ((this.exponent
== 0 && this.mantissa
== 0) || (a
.exponent
== 0 && a
.mantissa
== 0)) {
3145 if ((this.exponent
== 0 && this.mantissa
== 0))
3146 { this.mantissa
= a
.mantissa
; this.exponent
= a
.exponent
; this.sign
= a
.sign
; };
3149 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0)) {
3150 if (!(this.exponent
< 0 && this.mantissa
== 0) && (this.sign
!=0))
3151 ; // ASSIGN(this,this);
3152 else if (!(a
.exponent
< 0 && a
.mantissa
== 0) && (a
.sign
!=0)) {
3153 { this.mantissa
= a
.mantissa
; this.exponent
= a
.exponent
; this.sign
= a
.sign
; };
3155 makeInfinity(sign
| a
.sign
);
3161 if (((this.sign
!=0) && exponent
<= a
.exponent
) ||
3162 ((a
.sign
==0) && exponent
>= a
.exponent
))
3172 exponent
= a
.exponent
;
3173 mantissa
= a
.mantissa
;
3175 int shift
= exponent
-e
;
3176 if (shift
>=64 || shift
<=-64)
3181 mantissa
= -mantissa
;
3183 mantissa
|= m
>>shift
;
3185 mantissa
|= m
<<(-shift
);
3188 mantissa
= -mantissa
;
3194 * Calculates the logical <i>XOR</i> of this <code>Real</code> and
3196 * Replaces the contents of this <code>Real</code> with the result.
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.
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>".
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>
3216 * </td></tr><tr><td><i>
3217 * Execution time relative to add:
3220 * </td></tr></table>
3222 * @param a the <code>Real</code> argument
3224 public void xor(Real a
) {
3225 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
3229 if ((this.exponent
== 0 && this.mantissa
== 0) || (a
.exponent
== 0 && a
.mantissa
== 0)) {
3230 if ((this.exponent
== 0 && this.mantissa
== 0))
3231 { this.mantissa
= a
.mantissa
; this.exponent
= a
.exponent
; this.sign
= a
.sign
; };
3234 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0)) {
3235 makeInfinity(sign ^ a
.sign
);
3241 if (exponent
>= a
.exponent
) {
3250 exponent
= a
.exponent
;
3251 mantissa
= a
.mantissa
;
3253 int shift
= exponent
-e
;
3259 mantissa
= -mantissa
;
3260 mantissa ^
= m
>>shift
;
3263 mantissa
= -mantissa
;
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.
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.
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>
3284 * </td></tr><tr><td><i>
3285 * Execution time relative to add:
3288 * </td></tr></table>
3290 * @param a the <code>Real</code> argument
3292 public void bic(Real a
) {
3293 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
3297 if ((this.exponent
== 0 && this.mantissa
== 0) || (a
.exponent
== 0 && a
.mantissa
== 0))
3299 if ((this.exponent
< 0 && this.mantissa
== 0) || (a
.exponent
< 0 && a
.mantissa
== 0)) {
3300 if (!(this.exponent
< 0 && this.mantissa
== 0)) {
3306 } else if ((a
.sign
!=0)) {
3307 if ((a
.exponent
< 0 && a
.mantissa
== 0))
3314 int shift
= exponent
-a
.exponent
;
3315 if (shift
>=64 || (shift
<=-64 && (this.sign
==0)))
3317 long m
= a
.mantissa
;
3321 mantissa
= -mantissa
;
3323 if ((this.sign
!=0)) {
3327 mantissa
= (mantissa
>>(-shift
)) & ~m
;
3328 exponent
= a
.exponent
;
3330 mantissa
&= ~
(m
<<(-shift
));
3332 mantissa
&= ~
(m
>>shift
);
3335 mantissa
= -mantissa
;
3340 private int compare(int a
) {
3342 return compare(tmp0
);
3345 * Calculates the square root of this <code>Real</code>.
3346 * Replaces the contents of this <code>Real</code> with the result.
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>
3354 * </td></tr><tr><td><i>
3355 * Execution time relative to add:
3358 * </td></tr></table>
3360 public void sqrt() {
3363 * Cephes Math Library Release 2.2: December, 1990
3364 * Copyright 1984, 1990 by Stephen L. Moshier
3368 * long double sqrtl(long double x);
3370 if ((this.exponent
< 0 && this.mantissa
!= 0))
3372 if ((this.exponent
== 0 && this.mantissa
== 0)) {
3376 if ((this.sign
!=0)) {
3380 if ((this.exponent
< 0 && this.mantissa
== 0))
3383 { recipTmp
.mantissa
= this.mantissa
; recipTmp
.exponent
= this.exponent
; recipTmp
.sign
= this.sign
; };
3384 // normalize to range [0.5, 1)
3385 int e
= exponent
-0x3fffffff;
3386 exponent
= 0x3fffffff;
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
3403 for (int i
=0; i
<3; i
++) {
3404 { recipTmp2
.mantissa
= recipTmp
.mantissa
; recipTmp2
.exponent
= recipTmp
.exponent
; recipTmp2
.sign
= recipTmp
.sign
; };
3405 recipTmp2
.div(this);
3411 * Calculates the reciprocal square root of this <code>Real</code>.
3412 * Replaces the contents of this <code>Real</code> with the result.
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>
3420 * </td></tr><tr><td><i>
3421 * Execution time relative to add:
3424 * </td></tr></table>
3426 public void rsqrt() {
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.
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>
3442 * </td></tr><tr><td><i>
3443 * Execution time relative to add:
3446 * </td></tr></table>
3448 public void cbrt() {
3449 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
3453 // Calculates recipocal cube root of normalized Real,
3454 // not zero, nan or infinity
3455 final long start
= 0x5120000000000000L
;
3457 { recipTmp
.mantissa
= this.mantissa
; recipTmp
.exponent
= this.exponent
; recipTmp
.sign
= this.sign
; };
3459 // First establish approximate result
3460 mantissa
= start
-(mantissa
>>>2);
3461 int expRmd
= exponent
==0 ?
2 : (exponent
-1)%3;
3462 exponent
= 0x40000000-(exponent
-0x40000000-expRmd
)/3;
3465 { recipTmp2
.sign
=(byte)0; recipTmp2
.exponent
=0x3fffffff; recipTmp2
.mantissa
=0x6597fa94f5b8f20bL
; }; // cbrt(1/2)
3470 // Now perform Newton-Raphson iteration
3471 // Xn+1 = (4*Xn - A*Xn**4)/3
3472 for (int i
=0; i
<4; i
++) {
3473 { recipTmp2
.mantissa
= this.mantissa
; recipTmp2
.exponent
= this.exponent
; recipTmp2
.sign
= this.sign
; };
3477 recipTmp2
.scalbn(2);
3482 if (!(this.exponent
< 0 && this.mantissa
!= 0))
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.
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>
3498 * </td></tr><tr><td><i>
3499 * Execution time relative to add:
3502 * </td></tr></table>
3504 * @param n the <code>Real</code> argument.
3506 public void nroot(Real n
) {
3507 if ((n
.exponent
< 0 && n
.mantissa
!= 0)) {
3511 if (n
.compare(THREE
)==0) {
3512 cbrt(); // Most probable application of nroot...
3514 } else if (n
.compare(TWO
)==0) {
3515 sqrt(); // Also possible, should be optimized like this
3518 boolean negative
= false;
3519 if ((this.sign
!=0) && n
.isIntegral() && n
.isOdd()) {
3523 { tmp2
.mantissa
= n
.mantissa
; tmp2
.exponent
= n
.exponent
; tmp2
.sign
= n
.sign
; }; // Copy to temporary location in case of x.nroot(x)
3530 * Calculates <code>sqrt(this*this+a*a)</code>.
3531 * Replaces the contents of this <code>Real</code> with the result.
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>
3540 * </td></tr><tr><td><i>
3541 * Execution time relative to add:
3544 * </td></tr></table>
3546 * @param a the <code>Real</code> argument.
3548 public void hypot(Real a
) {
3549 { tmp1
.mantissa
= a
.mantissa
; tmp1
.exponent
= a
.exponent
; tmp1
.sign
= a
.sign
; }; // Copy to temporary location in case of x.hypot(x)
3555 private void exp2Internal(long extra
) {
3556 if ((this.exponent
< 0 && this.mantissa
!= 0))
3558 if ((this.exponent
< 0 && this.mantissa
== 0)) {
3563 if ((this.exponent
== 0 && this.mantissa
== 0)) {
3564 { this.mantissa
= ONE
.mantissa
; this.exponent
= ONE
.exponent
; this.sign
= ONE
.sign
; };
3567 // Extract integer part
3568 { expTmp
.mantissa
= this.mantissa
; expTmp
.exponent
= this.exponent
; expTmp
.sign
= this.sign
; };
3571 int exp
= expTmp
.toInteger();
3572 if (exp
> 0x40000000) {
3576 if (exp
< -0x40000000) {
3580 // Subtract integer part (this is where we need the extra accuracy)
3582 add128(extra
,expTmp
,0);
3585 * Cephes Math Library Release 2.7: May, 1998
3586 * Copyright 1984, 1991, 1998 by Stephen L. Moshier
3590 * long double exp2l(long double x);
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
; };
3598 { expTmp
.sign
=(byte)0; expTmp
.exponent
=0x40000005; expTmp
.mantissa
=0x793ace15b56b7fecL
; };//60.614853552242266094567
3599 expTmp
.mul(expTmp2
);
3600 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x4000000e; expTmp3
.mantissa
=0x764ef8cf96e29a13L
; };//30286.971917562792508623
3601 expTmp
.add(expTmp3
);
3602 expTmp
.mul(expTmp2
);
3603 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x40000014; expTmp3
.mantissa
=0x7efa0173e820bf60L
; };//2080384.3631901852422887
3604 expTmp
.add(expTmp3
);
3607 expTmp
.assign(expTmp2
);
3608 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x4000000a; expTmp3
.mantissa
=0x6d549a6b4dc9abadL
; };//1749.2876999891839021063
3609 expTmp
.add(expTmp3
);
3610 expTmp
.mul(expTmp2
);
3611 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x40000012; expTmp3
.mantissa
=0x5002d27836ba71c6L
; };//327725.15434906797273099
3612 expTmp
.add(expTmp3
);
3613 expTmp
.mul(expTmp2
);
3614 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x40000016; expTmp3
.mantissa
=0x5b98206867dd59bfL
; };//6002720.4078348487957118
3615 expTmp
.add(expTmp3
);
3620 // Scale by power of 2
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.
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>
3633 * </td></tr><tr><td><i>
3634 * Execution time relative to add:
3637 * </td></tr></table>
3640 { expTmp
.sign
=(byte)0; expTmp
.exponent
=0x40000000; expTmp
.mantissa
=0x5c551d94ae0bf85dL
; }; // log2(e)
3641 long extra
= mul128(0,expTmp
,0xdf43ff68348e9f44L
);
3642 exp2Internal(extra
);
3645 * Calculates 2 raised to the power of this <code>Real</code>.
3646 * Replaces the contents of this <code>Real</code> with the result.
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>
3655 * </td></tr><tr><td><i>
3656 * Execution time relative to add:
3659 * </td></tr></table>
3661 public void exp2() {
3665 * Calculates 10 raised to the power of this <code>Real</code>.
3666 * Replaces the contents of this <code>Real</code> with the result.
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>
3675 * </td></tr><tr><td><i>
3676 * Execution time relative to add:
3679 * </td></tr></table>
3681 public void exp10() {
3682 { expTmp
.sign
=(byte)0; expTmp
.exponent
=0x40000001; expTmp
.mantissa
=0x6a4d3c25e68dc57fL
; }; // log2(10)
3683 long extra
= mul128(0,expTmp
,0x2495fb7fa6d7eda6L
);
3684 exp2Internal(extra
);
3686 private int lnInternal()
3688 if ((this.exponent
< 0 && this.mantissa
!= 0))
3690 if ((this.sign
!=0)) {
3694 if ((this.exponent
== 0 && this.mantissa
== 0)) {
3698 if ((this.exponent
< 0 && this.mantissa
== 0))
3702 * Cephes Math Library Release 2.7: May, 1998
3703 * Copyright 1984, 1990, 1998 by Stephen L. Moshier
3707 * long double logl(long double x);
3709 // normalize to range [0.5, 1)
3710 int e
= exponent
-0x3fffffff;
3711 exponent
= 0x3fffffff;
3712 // rational appriximation
3713 // log(1+x) = x - x²/2 + x³ P(x)/Q(x)
3714 if (this.compare(SQRT1_2
) < 0) {
3719 { expTmp2
.mantissa
= this.mantissa
; expTmp2
.exponent
= this.exponent
; expTmp2
.sign
= this.sign
; };
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
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
3743 expTmp
.add(expTmp3
);
3744 expTmp
.mul(expTmp2
);
3745 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x40000006; expTmp3
.mantissa
=0x530c2d4884d25e18L
; };//83.047565967967209469434
3746 expTmp
.add(expTmp3
);
3747 expTmp
.mul(expTmp2
);
3748 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x40000007; expTmp3
.mantissa
=0x6ee19643f3ed5776L
; };//221.76239823732856465394
3749 expTmp
.add(expTmp3
);
3750 expTmp
.mul(expTmp2
);
3751 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x40000008; expTmp3
.mantissa
=0x4d465177242295efL
; };//309.09872225312059774938
3752 expTmp
.add(expTmp3
);
3753 expTmp
.mul(expTmp2
);
3754 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x40000007; expTmp3
.mantissa
=0x6c36c4f923819890L
; };//216.42788614495947685003
3755 expTmp
.add(expTmp3
);
3756 expTmp
.mul(expTmp2
);
3757 { expTmp3
.sign
=(byte)0; expTmp3
.exponent
=0x40000005; expTmp3
.mantissa
=0x783cc111991239a3L
; };//60.118660497603843919306
3758 expTmp
.add(expTmp3
);
3760 { expTmp3
.mantissa
= expTmp2
.mantissa
; expTmp3
.exponent
= expTmp2
.exponent
; expTmp3
.sign
= expTmp2
.sign
; };
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.
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>
3780 * </td></tr><tr><td><i>
3781 * Execution time relative to add:
3784 * </td></tr></table>
3787 int exp
= lnInternal();
3793 * Calculates the base-2 logarithm of this <code>Real</code>.
3794 * Replaces the contents of this <code>Real</code> with the result.
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>
3803 * </td></tr><tr><td><i>
3804 * Execution time relative to add:
3807 * </td></tr></table>
3809 public void log2() {
3810 int exp
= lnInternal();
3815 * Calculates the base-10 logarithm of this <code>Real</code>.
3816 * Replaces the contents of this <code>Real</code> with the result.
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>
3824 * </td></tr><tr><td><i>
3825 * Execution time relative to add:
3828 * </td></tr></table>
3830 public void log10() {
3831 int exp
= lnInternal();
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.
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>
3852 * </td></tr><tr><td><i>
3853 * Execution time relative to add:
3856 * </td></tr></table>
3858 * @return the base-10 exponent
3860 public int lowPow10() {
3861 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
3863 { tmp2
.mantissa
= this.mantissa
; tmp2
.exponent
= this.exponent
; tmp2
.sign
= this.sign
; };
3864 // Approximate log10 using exponent only
3865 int e
= exponent
- 0x40000000;
3866 if (e
<0) // it's important to achieve floor(exponent*ln2/ln10)
3867 e
= -(int)(((-e
)*0x4d104d43L
+((1L<<32)-1)) >> 32);
3869 e
= (int)(e
*0x4d104d43L
>> 32);
3870 // Now, e < log10(this) < e+1
3871 { this.mantissa
= TEN
.mantissa
; this.exponent
= TEN
.exponent
; this.sign
= TEN
.sign
; };
3873 if ((this.exponent
== 0 && this.mantissa
== 0)) { // A *really* small number, then
3874 { tmp3
.mantissa
= TEN
.mantissa
; tmp3
.exponent
= TEN
.exponent
; tmp3
.sign
= TEN
.sign
; };
3877 { tmp3
.mantissa
= this.mantissa
; tmp3
.exponent
= this.exponent
; tmp3
.sign
= this.sign
; };
3880 if (tmp3
.compare(tmp2
) <= 0) {
3881 // First estimate of log10 was too low
3883 { this.mantissa
= tmp3
.mantissa
; this.exponent
= tmp3
.exponent
; this.sign
= tmp3
.sign
; };
3888 * Calculates the value of this <code>Real</code> raised to the power of
3890 * Replaces the contents of this <code>Real</code> with the result.
3892 * <p> Special cases:
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)
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>
3928 * </td></tr><tr><td><i>
3929 * Execution time relative to add:
3932 * </td></tr></table>
3934 * @param a the <code>Real</code> argument.
3936 public void pow(Real a
) {
3937 if ((a
.exponent
== 0 && a
.mantissa
== 0)) {
3938 { this.mantissa
= ONE
.mantissa
; this.exponent
= ONE
.exponent
; this.sign
= ONE
.sign
; };
3941 if ((this.exponent
< 0 && this.mantissa
!= 0) || (a
.exponent
< 0 && a
.mantissa
!= 0)) {
3945 if (a
.compare(ONE
)==0)
3947 if ((a
.exponent
< 0 && a
.mantissa
== 0)) {
3948 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
3950 int test
= tmp1
.compare(ONE
);
3956 } else if (test
<0) {
3966 if ((this.exponent
== 0 && this.mantissa
== 0)) {
3967 if ((this.sign
==0)) {
3973 if (a
.isIntegral() && a
.isOdd()) {
3987 if ((this.exponent
< 0 && this.mantissa
== 0)) {
3988 if ((this.sign
==0)) {
3994 if (a
.isIntegral()) {
4012 if (a
.isIntegral() && a
.exponent
<= 0x4000001e) {
4017 if ((this.sign
!=0)) {
4018 if (a
.isIntegral()) {
4027 { tmp1
.mantissa
= a
.mantissa
; tmp1
.exponent
= a
.exponent
; tmp1
.sign
= a
.sign
; };
4028 if (tmp1
.exponent
<= 0x4000001e) {
4029 // For increased accuracy, exponentiate with integer part of
4030 // exponent by successive squaring
4031 // (I really don't know why this works)
4032 { tmp2
.mantissa
= tmp1
.mantissa
; tmp2
.exponent
= tmp1
.exponent
; tmp2
.sign
= tmp1
.sign
; };
4034 { tmp3
.mantissa
= this.mantissa
; tmp3
.exponent
= this.exponent
; tmp3
.sign
= this.sign
; };
4035 tmp3
.pow(tmp2
.toInteger());
4038 { tmp3
.mantissa
= ONE
.mantissa
; tmp3
.exponent
= ONE
.exponent
; tmp3
.sign
= ONE
.sign
; };
4040 // Do log2 and maintain accuracy
4041 int e
= lnInternal();
4042 { tmp2
.sign
=(byte)0; tmp2
.exponent
=0x40000000; tmp2
.mantissa
=0x5c551d94ae0bf85dL
; }; // log2(e)
4043 long extra
= mul128(0,tmp2
,0xdf43ff68348e9f44L
);
4045 extra
= add128(extra
,tmp2
,0);
4046 // Do exp2 of this multiplied by (fractional part of) exponent
4047 extra
= tmp1
.mul128(0,this,extra
);
4048 tmp1
.exp2Internal(extra
);
4049 { this.mantissa
= tmp1
.mantissa
; this.exponent
= tmp1
.exponent
; this.sign
= tmp1
.sign
; };
4051 if (!(this.exponent
< 0 && this.mantissa
!= 0))
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.
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>
4065 * </td></tr><tr><td><i>
4066 * Execution time relative to add:
4069 * </td></tr></table>
4071 * @param a the integer argument.
4073 public void pow(int a
) {
4074 // Calculate power of integer by successive squaring
4077 a
= -a
; // Also works for 0x80000000
4080 long extra
= 0, expTmpExtra
= 0;
4081 { expTmp
.mantissa
= this.mantissa
; expTmp
.exponent
= this.exponent
; expTmp
.sign
= this.sign
; };
4082 { this.mantissa
= ONE
.mantissa
; this.exponent
= ONE
.exponent
; this.sign
= ONE
.sign
; };
4083 for (; a
!=0; a
>>>=1) {
4085 extra
= mul128(extra
,expTmp
,expTmpExtra
);
4086 expTmpExtra
= expTmp
.mul128(expTmpExtra
,expTmp
,expTmpExtra
);
4089 extra
= recip128(extra
);
4090 roundFrom128(extra
);
4092 private void sinInternal() {
4095 * Cephes Math Library Release 2.7: May, 1998
4096 * Copyright 1985, 1990, 1998 by Stephen L. Moshier
4100 * long double sinl(long double x);
4103 // polynomial approximation
4104 // sin(x) = x + x³ P(x²)
4105 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4106 { tmp2
.mantissa
= this.mantissa
; tmp2
.exponent
= this.exponent
; tmp2
.sign
= this.sign
; };
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
4131 private void cosInternal() {
4134 * Cephes Math Library Release 2.7: May, 1998
4135 * Copyright 1985, 1990, 1998 by Stephen L. Moshier
4139 * long double cosl(long double x);
4142 // polynomial approximation
4143 // cos(x) = 1 - x²/2 + x**4 Q(x²)
4144 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4145 { tmp2
.mantissa
= this.mantissa
; tmp2
.exponent
= this.exponent
; tmp2
.sign
= this.sign
; };
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
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.
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>
4182 * </td></tr><tr><td><i>
4183 * Execution time relative to add:
4186 * </td></tr></table>
4189 if (!(this.exponent
>= 0 && this.mantissa
!= 0)) {
4190 if (!(this.exponent
== 0 && this.mantissa
== 0))
4194 // Since sin(-x) = -sin(x) we can make sure that x > 0
4195 boolean negative
= false;
4196 if ((this.sign
!=0)) {
4200 // Then reduce the argument to the range of 0 < x < pi*2
4201 if (this.compare(PI2
) > 0)
4202 modInternal(PI2
,0x62633145c06e0e69L
);
4203 // Since sin(pi*2 - x) = -sin(x) we can reduce the range 0 < x < pi
4204 if (this.compare(PI
) > 0) {
4207 negative
= !negative
;
4209 // Since sin(x) = sin(pi - x) we can reduce the range to 0 < x < pi/2
4210 if (this.compare(PI_2
) > 0) {
4214 // Since sin(x) = cos(pi/2 - x) we can reduce the range to 0 < x < pi/4
4215 if (this.compare(PI_4
) > 0) {
4224 if ((this.exponent
== 0 && this.mantissa
== 0))
4225 abs(); // Remove confusing "-"
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.
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>
4238 * </td></tr><tr><td><i>
4239 * Execution time relative to add:
4242 * </td></tr></table>
4245 if ((this.exponent
== 0 && this.mantissa
== 0)) {
4246 { this.mantissa
= ONE
.mantissa
; this.exponent
= ONE
.exponent
; this.sign
= ONE
.sign
; };
4251 if (this.compare(PI_4
) < 0) {
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.
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>
4269 * </td></tr><tr><td><i>
4270 * Execution time relative to add:
4273 * </td></tr></table>
4276 { tmp4
.mantissa
= this.mantissa
; tmp4
.exponent
= this.exponent
; tmp4
.sign
= this.sign
; };
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.
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>
4292 * </td></tr><tr><td><i>
4293 * Execution time relative to add:
4296 * </td></tr></table>
4298 public void asin() {
4299 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
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.
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>
4318 * </td></tr><tr><td><i>
4319 * Execution time relative to add:
4322 * </td></tr></table>
4324 public void acos() {
4325 boolean negative
= (this.sign
!=0);
4327 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
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.
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>
4350 * </td></tr><tr><td><i>
4351 * Execution time relative to add:
4354 * </td></tr></table>
4356 public void atan() {
4359 * Cephes Math Library Release 2.7: May, 1998
4360 * Copyright 1984, 1990, 1998 by Stephen L. Moshier
4364 * long double atanl(long double x);
4366 if ((this.exponent
== 0 && this.mantissa
== 0) || (this.exponent
< 0 && this.mantissa
!= 0))
4368 if ((this.exponent
< 0 && this.mantissa
== 0)) {
4370 { this.mantissa
= PI_2
.mantissa
; this.exponent
= PI_2
.exponent
; this.sign
= PI_2
.sign
; };
4377 boolean addPI_2
= false;
4378 boolean addPI_4
= false;
4379 { tmp1
.mantissa
= SQRT2
.mantissa
; tmp1
.exponent
= SQRT2
.exponent
; tmp1
.sign
= SQRT2
.sign
; };
4381 if (this.compare(tmp1
) > 0) {
4387 if (this.compare(tmp1
) > 0) {
4389 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4395 // Now |X|<sqrt(2)-1
4396 // rational approximation
4397 // atan(x) = x + x³ P(x²)/Q(x²)
4398 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4399 { tmp2
.mantissa
= this.mantissa
; tmp2
.exponent
= this.exponent
; tmp2
.sign
= this.sign
; };
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
4416 { tmp3
.mantissa
= tmp2
.mantissa
; tmp3
.exponent
= tmp2
.exponent
; tmp3
.sign
= tmp2
.sign
; };
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
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.
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>
4454 * </td></tr><tr><td><i>
4455 * Execution time relative to add:
4458 * </td></tr></table>
4460 * @param x the <code>Real</code> argument.
4462 public void atan2(Real x
) {
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))) {
4467 if ((this.exponent
== 0 && this.mantissa
== 0) && (x
.exponent
== 0 && x
.mantissa
== 0))
4482 * Calculates the hyperbolic sine of this <code>Real</code>.
4483 * Replaces the contents of this <code>Real</code> with the result.
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>
4491 * </td></tr><tr><td><i>
4492 * Execution time relative to add:
4495 * </td></tr></table>
4497 public void sinh() {
4498 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4506 * Calculates the hyperbolic cosine of this <code>Real</code>.
4507 * Replaces the contents of this <code>Real</code> with the result.
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>
4515 * </td></tr><tr><td><i>
4516 * Execution time relative to add:
4519 * </td></tr></table>
4521 public void cosh() {
4522 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4530 * Calculates the hyperbolic tangent of this <code>Real</code>.
4531 * Replaces the contents of this <code>Real</code> with the result.
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>
4539 * </td></tr><tr><td><i>
4540 * Execution time relative to add:
4543 * </td></tr></table>
4545 public void tanh() {
4546 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4550 { tmp2
.mantissa
= this.mantissa
; tmp2
.exponent
= this.exponent
; tmp2
.sign
= this.sign
; };
4556 * Calculates the hyperbolic arc sine of this <code>Real</code>.
4557 * Replaces the contents of this <code>Real</code> with the result.
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>
4563 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4565 * </td></tr><tr><td><i>
4566 * Execution time relative to add:
4569 * </td></tr></table>
4571 public void asinh() {
4572 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
4574 // Use symmetry to prevent underflow error for very large negative
4578 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4584 if (!(this.exponent
< 0 && this.mantissa
!= 0))
4588 * Calculates the hyperbolic arc cosine of this <code>Real</code>.
4589 * Replaces the contents of this <code>Real</code> with the result.
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>
4595 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4597 * </td></tr><tr><td><i>
4598 * Execution time relative to add:
4601 * </td></tr></table>
4603 public void acosh() {
4604 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4612 * Calculates the hyperbolic arc tangent of this <code>Real</code>.
4613 * Replaces the contents of this <code>Real</code> with the result.
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>
4619 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4621 * </td></tr><tr><td><i>
4622 * Execution time relative to add:
4625 * </td></tr></table>
4627 public void atanh() {
4628 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
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>.
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>
4646 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4648 * </td></tr><tr><td><i>
4649 * Execution time relative to add:
4652 * </td></tr></table>
4654 public void fact() {
4655 if (!(this.exponent
>= 0))
4657 if (!this.isIntegral() || this.compare(ZERO
)<0 || this.compare(200)>0)
4659 // x<0, x>200 or not integer: fact(x) = gamma(x+1)
4664 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4665 { this.mantissa
= ONE
.mantissa
; this.exponent
= ONE
.exponent
; this.sign
= ONE
.sign
; };
4666 while (tmp1
.compare(ONE
) > 0) {
4672 * Calculates the gamma function for this <code>Real</code>.
4673 * Replaces the contents of this <code>Real</code> with the result.
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>
4679 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
4681 * </td></tr><tr><td><i>
4682 * Execution time relative to add:
4685 * </td></tr></table>
4687 public void gamma() {
4688 if (!(this.exponent
>= 0))
4690 // x<0: gamma(-x) = -pi/(x*gamma(x)*sin(pi*x))
4691 boolean negative
= (this.sign
!=0);
4693 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4694 // x<n: gamma(x) = gamma(x+m)/x*(x+1)*(x+2)*...*(x+m-1)
4696 { tmp2
.mantissa
= ONE
.mantissa
; tmp2
.exponent
= ONE
.exponent
; tmp2
.sign
= ONE
.sign
; };
4697 boolean divide
= false;
4698 while (this.compare(20) < 0) {
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)
4705 { tmp3
.mantissa
= this.mantissa
; tmp3
.exponent
= this.exponent
; tmp3
.sign
= this.sign
; }; // x
4706 { tmp4
.mantissa
= this.mantissa
; tmp4
.exponent
= this.exponent
; tmp4
.sign
= this.sign
; };
4709 ln(); { tmp5
.mantissa
= tmp3
.mantissa
; tmp5
.exponent
= tmp3
.exponent
; tmp5
.sign
= tmp3
.sign
; }; tmp5
.sub(HALF
); mul(tmp5
); sub(tmp3
);
4711 { tmp5
.sign
=(byte)0; tmp5
.exponent
=0x3fffffff; tmp5
.mantissa
=0x759fc72192fad29aL
; }; add(tmp5
);
4713 tmp5
.assign( 12); tmp5
.mul(tmp3
); tmp5
.recip(); add(tmp5
); tmp3
.mul(tmp4
);
4715 tmp5
.assign( 360); tmp5
.mul(tmp3
); tmp5
.recip(); sub(tmp5
); tmp3
.mul(tmp4
);
4717 tmp5
.assign(1260); tmp5
.mul(tmp3
); tmp5
.recip(); add(tmp5
); tmp3
.mul(tmp4
);
4719 tmp5
.assign(1680); tmp5
.mul(tmp3
); tmp5
.recip(); sub(tmp5
); tmp3
.mul(tmp4
);
4721 tmp5
.assign(1188); tmp5
.mul(tmp3
); tmp5
.recip(); add(tmp5
);
4726 { tmp5
.mantissa
= tmp1
.mantissa
; tmp5
.exponent
= tmp1
.exponent
; tmp5
.sign
= tmp1
.sign
; }; // sin() uses tmp1
4727 // -pi/(x*gamma(x)*sin(pi*x))
4729 tmp5
.scalbn(-1); tmp5
.frac(); tmp5
.mul(PI2
); // Fixes integer inaccuracy
4730 tmp5
.sin(); mul(tmp5
); recip(); mul(PI
); neg();
4733 private void erfc1Internal() {
4735 // 2 / x x x x // erfc(x) = 1 - ------ | x - --- + ---- - ---- + ---- - ... |
4736 // sqrt(pi)\ 3 2!*5 3!*7 4!*9 /
4738 long extra
=0,tmp1Extra
,tmp2Extra
,tmp3Extra
,tmp4Extra
;
4739 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; }; tmp1Extra
= 0;
4740 { tmp2
.mantissa
= this.mantissa
; tmp2
.exponent
= this.exponent
; tmp2
.sign
= this.sign
; };
4741 tmp2Extra
= tmp2
.mul128(0,tmp2
,0);
4743 { tmp3
.mantissa
= ONE
.mantissa
; tmp3
.exponent
= ONE
.exponent
; tmp3
.sign
= ONE
.sign
; }; tmp3Extra
= 0;
4746 tmp1Extra
= tmp1
.mul128(tmp1Extra
,tmp2
,tmp2Extra
);
4748 tmp3Extra
= tmp3
.mul128(tmp3Extra
,tmp4
,0);
4750 tmp4Extra
= tmp4
.mul128(0,tmp3
,tmp3Extra
);
4751 tmp4Extra
= tmp4
.recip128(tmp4Extra
);
4752 tmp4Extra
= tmp4
.mul128(tmp4Extra
,tmp1
,tmp1Extra
);
4753 extra
= add128(extra
,tmp4
,tmp4Extra
);
4755 } while (exponent
- tmp4
.exponent
< 128);
4756 { tmp1
.sign
=(byte)1; tmp1
.exponent
=0x40000000; tmp1
.mantissa
=0x48375d410a6db446L
; }; // -2/sqrt(pi)
4757 extra
= mul128(extra
,tmp1
,0xb8ea453fb5ff61a2L
);
4758 extra
= add128(extra
,ONE
,0);
4759 roundFrom128(extra
);
4761 private void erfc2Internal() {
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
4767 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4769 { tmp2
.sign
=(byte)0; tmp2
.exponent
=0x40000000; tmp2
.mantissa
=0x5c3811b4bfd0c8abL
; }; // 1/0.694
4772 int digits
= tmp2
.toInteger(); // number of accurate digits = x*x/0.694-0.5
4776 int dxq
= tmp1
.toInteger()+1;
4777 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
4779 { tmp2
.mantissa
= this.mantissa
; tmp2
.exponent
= this.exponent
; tmp2
.sign
= this.sign
; };
4780 { tmp3
.mantissa
= this.mantissa
; tmp3
.exponent
= this.exponent
; tmp3
.sign
= this.sign
; };
4784 { this.mantissa
= ONE
.mantissa
; this.exponent
= ONE
.exponent
; this.sign
= ONE
.sign
; };
4785 { tmp4
.mantissa
= ONE
.mantissa
; tmp4
.exponent
= ONE
.exponent
; tmp4
.sign
= ONE
.sign
; };
4792 } while (tmp4
.exponent
-0x40000000>-(digits
+2) && 2*i
-1<dxq
);
4798 { tmp1
.sign
=(byte)0; tmp1
.exponent
=0x3fffffff; tmp1
.mantissa
=0x48375d410a6db447L
; }; // 1/sqrt(pi)
4802 * Calculates the complementary error function for this <code>Real</code>.
4803 * Replaces the contents of this <code>Real</code> with the result.
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
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>
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:
4821 * </td></tr></table>
4823 public void erfc() {
4824 if ((this.exponent
< 0 && this.mantissa
!= 0))
4826 if ((this.exponent
== 0 && this.mantissa
== 0)) {
4827 { this.mantissa
= ONE
.mantissa
; this.exponent
= ONE
.exponent
; this.sign
= ONE
.sign
; };
4830 if ((this.exponent
< 0 && this.mantissa
== 0) || toInteger()>27281) {
4831 if ((this.sign
!=0)) {
4832 { this.mantissa
= TWO
.mantissa
; this.exponent
= TWO
.exponent
; this.sign
= TWO
.sign
; };
4839 { tmp1
.sign
=(byte)0; tmp1
.exponent
=0x40000002; tmp1
.mantissa
=0x570a3d70a3d70a3dL
; }; // 5.44
4840 if (this.lessThan(tmp1
))
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.
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>
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:
4864 * </td></tr></table>
4866 public void inverfc() {
4867 if ((this.exponent
< 0 && this.mantissa
!= 0) || (this.sign
!=0) || this.greaterThan(TWO
)) {
4871 if ((this.exponent
== 0 && this.mantissa
== 0)) {
4875 if (this.equalTo(TWO
)) {
4879 int sign
= ONE
.compare(this);
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²)))
4897 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
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
4910 tmp2
.add(-1); // P1=-1
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))
4962 //*************************************************************************
4963 // Calendar conversions taken from
4964 // http://www.fourmilab.ch/documents/calendar/
4965 private static int floorDiv(int a
, int b
) {
4968 return -((-a
+b
-1)/b
);
4970 private static int floorMod(int a
, int b
) {
4973 return a
+((-a
+b
-1)/b
)*b
;
4975 private static boolean leap_gregorian(int year
) {
4976 return ((year
% 4) == 0) &&
4977 (!(((year
% 100) == 0) && ((year
% 400) != 0)));
4979 // GREGORIAN_TO_JD -- Determine Julian day number from Gregorian
4980 // calendar date -- Except that we use 1/1-0 as day 0
4981 private static int gregorian_to_jd(int year
, int month
, int day
) {
4983 (365 * (year
- 1)) +
4984 (floorDiv(year
- 1, 4)) +
4985 (-floorDiv(year
- 1, 100)) +
4986 (floorDiv(year
- 1, 400)) +
4987 ((((367 * month
) - 362) / 12) +
4988 ((month
<= 2) ?
0 : (leap_gregorian(year
) ?
-1 : -2)) + day
));
4990 // JD_TO_GREGORIAN -- Calculate Gregorian calendar date from Julian
4991 // day -- Except that we use 1/1-0 as day 0
4992 private static int jd_to_gregorian(int jd
) {
4993 int wjd
, depoch
, quadricent
, dqc
, cent
, dcent
, quad
, dquad
,
4994 yindex
, year
, yearday
, leapadj
, month
, day
;
4997 quadricent
= floorDiv(depoch
, 146097);
4998 dqc
= floorMod(depoch
, 146097);
4999 cent
= floorDiv(dqc
, 36524);
5000 dcent
= floorMod(dqc
, 36524);
5001 quad
= floorDiv(dcent
, 1461);
5002 dquad
= floorMod(dcent
, 1461);
5003 yindex
= floorDiv(dquad
, 365);
5004 year
= (quadricent
* 400) + (cent
* 100) + (quad
* 4) + yindex
;
5005 if (!((cent
== 4) || (yindex
== 4)))
5007 yearday
= wjd
- gregorian_to_jd(year
, 1, 1);
5008 leapadj
= ((wjd
< gregorian_to_jd(year
, 3, 1)) ?
0
5009 : (leap_gregorian(year
) ?
1 : 2));
5010 month
= floorDiv(((yearday
+ leapadj
) * 12) + 373, 367);
5011 day
= (wjd
- gregorian_to_jd(year
, month
, 1)) + 1;
5012 return (year
*100+month
)*100+day
;
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.
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.
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>.
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>
5040 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
5042 * </td></tr><tr><td><i>
5043 * Execution time relative to add:
5046 * </td></tr></table>
5048 public void toDHMS() {
5049 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
5051 boolean negative
= (this.sign
!=0);
5062 // MAGIC ROUNDING: Check if we are 2**-16 sec short of a whole minute
5063 // i.e. "seconds" > 59.999985
5064 { tmp2
.mantissa
= ONE
.mantissa
; tmp2
.exponent
= ONE
.exponent
; tmp2
.sign
= ONE
.sign
; };
5067 if (this.compare(tmp1
) >= 0) {
5068 // Yes. So set zero secs instead and carry over to mins and hours
5069 { this.mantissa
= ZERO
.mantissa
; this.exponent
= ZERO
.exponent
; this.sign
= ZERO
.sign
; };
5075 // Phew! That was close. From now on it is integer arithmetic...
5077 // Nope. So try to undo the damage...
5083 D
= jd_to_gregorian(D
);
5086 tmp1
.assign(D
*100L+h
);
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.
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.
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
5112 * href="http://midp-calc.sourceforge.net/Calc.html#DateNote">here</a>.
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>
5118 * </td></tr><tr><td><i>Approximate error bound:</i></td><td>
5120 * </td></tr><tr><td><i>
5121 * Execution time relative to add:
5124 * </td></tr></table>
5126 public void fromDHMS() {
5127 if (!(this.exponent
>= 0 && this.mantissa
!= 0))
5129 boolean negative
= (this.sign
!=0);
5140 // MAGIC ROUNDING: Check if we are 2**-10 second short of 100 seconds
5141 // i.e. "seconds" > 99.999
5142 { tmp2
.mantissa
= ONE
.mantissa
; tmp2
.exponent
= ONE
.exponent
; tmp2
.sign
= ONE
.sign
; };
5145 if (this.compare(tmp1
) >= 0) {
5146 // Yes. So set zero secs instead and carry over to mins and hours
5147 { this.mantissa
= ZERO
.mantissa
; this.exponent
= ZERO
.exponent
; this.sign
= ZERO
.sign
; };
5153 // Phew! That was close. From now on it is integer arithmetic...
5155 // Nope. So try to undo the damage...
5167 D
= gregorian_to_jd(Y
,M
,D
);
5171 tmp1
.assign(D
*24L+h
);
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.
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>
5186 * </td></tr><tr><td><i>Error bound:</i></td><td>
5188 * </td></tr><tr><td><i>
5189 * Execution time relative to add:
5192 * </td></tr></table>
5194 public void time() {
5195 long now
= System
.currentTimeMillis();
5198 s
= (int)(now
% 60);
5200 m
= (int)(now
% 60);
5202 h
= (int)(now
% 24);
5203 assign((h
*100+m
)*100+s
);
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
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>
5220 * </td></tr><tr><td><i>Error bound:</i></td><td>
5222 * </td></tr><tr><td><i>
5223 * Execution time relative to add:
5226 * </td></tr></table>
5228 public void date() {
5229 long now
= System
.currentTimeMillis();
5230 now
/= 86400000; // days
5233 add(719528*24); // 1970-01-01 era
5236 //*************************************************************************
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.
5243 public static long randSeedA
= 0x6487ed5110b4611aL
;
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.
5250 public static long randSeedB
= 0x56fc2a2c515da54dL
;
5251 // 64 Bit CRC Generators
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.
5257 private static void advanceBit() {
5258 randSeedA
= (randSeedA
<<1)^
(randSeedA
<0?
0x1b:0);
5259 randSeedB
= (randSeedB
<<1)^
(randSeedB
<0?
0xb000000000000001L
:0);
5261 // Get next bits from the pseudo-random sequence
5262 private static long nextBits(int bits
) {
5264 while (bits
-- > 0) {
5265 while (randSeedA
>= 0)
5267 answer
= (answer
<<1) + (randSeedB
< 0 ?
1 : 0);
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().
5278 * @param seed some extra randomness for the random number generator.
5280 public static void accumulateRandomness(long seed
) {
5281 randSeedA ^
= seed
& 0x5555555555555555L
;
5282 randSeedB ^
= seed
& 0xaaaaaaaaaaaaaaaaL
;
5286 * Calculates a pseudorandom number in the range [0, 1).
5287 * Replaces the contents of this <code>Real</code> with the result.
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.
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>
5301 * </td></tr><tr><td><i>
5302 * Execution time relative to add:
5305 * </td></tr></table>
5307 public void random() {
5309 exponent
= 0x3fffffff;
5310 while (nextBits(1) == 0)
5312 mantissa
= 0x4000000000000000L
+nextBits(62);
5314 //*************************************************************************
5315 private int digit(char a
, int base
, boolean twosComplement
) {
5317 if (a
>='0' && a
<='9')
5319 else if (a
>='A' && a
<='F')
5327 private void shiftUp(int base
) {
5337 private void atof(String a
, int base
) {
5339 int length
= a
.length();
5342 boolean compl
= false;
5343 while (index
<length
&& a
.charAt(index
)==' ')
5345 if (index
<length
&& a
.charAt(index
)=='-') {
5348 } else if (index
<length
&& a
.charAt(index
)=='+') {
5350 } else if (index
<length
&& a
.charAt(index
)=='/') {
5351 // Input is twos complemented negative number
5357 while (index
<length
&& (d
=digit(a
.charAt(index
),base
,compl
))>=0) {
5363 if (index
<length
&& (a
.charAt(index
)=='.' || a
.charAt(index
)==',')) {
5365 while (index
<length
&& (d
=digit(a
.charAt(index
),base
,compl
))>=0) {
5374 while (index
<length
&& a
.charAt(index
)==' ')
5376 if (index
<length
&& (a
.charAt(index
)=='e' || a
.charAt(index
)=='E')) {
5379 boolean expNeg
= false;
5380 if (index
<length
&& a
.charAt(index
)=='-') {
5383 } else if (index
<length
&& a
.charAt(index
)=='+') {
5386 while (index
<length
&& a
.charAt(index
)>='0' &&
5387 a
.charAt(index
)<='9')
5389 // This takes care of overflows and makes inf or 0
5390 if (exp2
< 400000000)
5391 exp2
= exp2
*10 + a
.charAt(index
) - '0';
5405 if (exp
> 300000000 || exp
< -300000000) {
5406 // Kludge to be able to enter very large and very small
5407 // numbers without causing over/underflows
5408 { tmp1
.mantissa
= TEN
.mantissa
; tmp1
.exponent
= TEN
.exponent
; tmp1
.sign
= TEN
.sign
; };
5418 { tmp1
.mantissa
= TEN
.mantissa
; tmp1
.exponent
= TEN
.exponent
; tmp1
.sign
= TEN
.sign
; };
5429 //*************************************************************************
5430 private void normalizeDigits(byte[] digits
, int nDigits
, int base
) {
5432 boolean isZero
= true;
5433 for (int i
=nDigits
-1; i
>=0; i
--) {
5438 if (digits
[i
] >= base
) {
5448 if (digits
[nDigits
-1] >= base
/2)
5449 digits
[nDigits
-2] ++; // Rounding, may be inaccurate
5450 System
.arraycopy(digits
, 0, digits
, 1, nDigits
-1);
5453 if (digits
[nDigits
-1] >= base
) {
5454 // Oh, no, not again!
5455 normalizeDigits(digits
, nDigits
, base
);
5458 while (digits
[0] == 0) {
5459 System
.arraycopy(digits
, 1, digits
, 0, nDigits
-1);
5460 digits
[nDigits
-1] = 0;
5464 private int getDigits(byte[] digits
, int base
) {
5467 { tmp1
.mantissa
= this.mantissa
; tmp1
.exponent
= this.exponent
; tmp1
.sign
= this.sign
; };
5469 { tmp2
.mantissa
= tmp1
.mantissa
; tmp2
.exponent
= tmp1
.exponent
; tmp2
.sign
= tmp1
.sign
; };
5470 int exp
= exponent
= tmp1
.lowPow10();
5472 boolean exp_neg
= exp
<= 0;
5473 exp
= Math
.abs(exp
);
5474 if (exp
> 300000000) {
5475 // Kludge to be able to print very large and very small numbers
5476 // without causing over/underflows
5477 { tmp1
.mantissa
= TEN
.mantissa
; tmp1
.exponent
= TEN
.exponent
; tmp1
.sign
= TEN
.sign
; };
5478 tmp1
.pow(exp
/2); // So, divide twice by not-so-extreme numbers
5483 { tmp1
.mantissa
= TEN
.mantissa
; tmp1
.exponent
= TEN
.exponent
; tmp1
.sign
= TEN
.sign
; };
5484 tmp1
.pow(exp
-(exp
/2));
5486 { tmp1
.mantissa
= TEN
.mantissa
; tmp1
.exponent
= TEN
.exponent
; tmp1
.sign
= TEN
.sign
; };
5494 if (tmp2
.exponent
> 0x4000003e) {
5498 if (a
>= 5000000000000000000L) { // Rounding up gave 20 digits
5501 digits
[18] = (byte)(a
%10);
5504 digits
[18] = (byte)((a
%5)*2);
5510 digits
[18] = (byte)(a
%10);
5513 for (int i
=17; i
>=0; i
--) {
5514 digits
[i
] = (byte)(a
%10);
5520 int accurateBits
= 64;
5521 int bitsPerDigit
= base
== 2 ?
1 : base
== 8 ?
3 : 4;
5522 if ((this.exponent
== 0 && this.mantissa
== 0)) {
5523 sign
= 0; // Two's complement cannot display -0
5525 if ((this.sign
!=0)) {
5526 mantissa
= -mantissa
;
5527 if (((mantissa
>> 62)&3) == 3) {
5530 accurateBits
--; // ?
5533 exponent
-= 0x40000000-1;
5534 int shift
= bitsPerDigit
-1 -
5535 floorMod(exponent
, bitsPerDigit
);
5536 exponent
= floorDiv(exponent
, bitsPerDigit
);
5537 if (shift
== bitsPerDigit
-1) {
5538 // More accurate to shift up instead
5544 mantissa
= (mantissa
+(1L<<(shift
-1)))>>>shift
;
5545 if ((this.sign
!=0)) {
5546 // Need to fill in some 1's at the top
5547 // (">>", not ">>>")
5548 mantissa
|= 0x8000000000000000L
>>(shift
-1);
5552 int accurateDigits
= (accurateBits
+bitsPerDigit
-1)/bitsPerDigit
;
5553 for (int i
=0; i
<accurateDigits
; i
++) {
5554 digits
[i
] = (byte)(mantissa
>>>(64-bitsPerDigit
));
5555 mantissa
<<= bitsPerDigit
;
5557 digits
[accurateDigits
] = 0;
5558 return accurateDigits
;
5560 private boolean carryWhenRounded(byte[] digits
, int nDigits
, int base
) {
5561 if (digits
[nDigits
] < base
/2)
5562 return false; // no rounding up, no carry
5563 for (int i
=nDigits
-1; i
>=0; i
--)
5564 if (digits
[i
] < base
-1)
5565 return false; // carry would not propagate
5568 for (int i
=1; i
<digits
.length
; i
++)
5572 private void round(byte[] digits
, int nDigits
, int base
) {
5573 if (digits
[nDigits
] >= base
/2) {
5574 digits
[nDigits
-1]++;
5575 normalizeDigits(digits
, nDigits
, base
);
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.
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.
5588 public static class NumberFormat
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.
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
5605 public int base
= 10;
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>.
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.
5621 public int maxwidth
= 30;
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.
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
5637 public int precision
= 16;
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.
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>
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.
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.
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.
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.
5678 public int fse
= FSE_NONE
;
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
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>.
5689 public char point
= '.';
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
5695 public boolean removePoint
= true;
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>.
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.
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.
5715 public char thousand
= 0;
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:
5722 * <dt>{@link #ALIGN_NONE}
5723 * <dd>The resulting string is not padded with spaces.
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.
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.
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.
5741 public int align
= ALIGN_NONE
;
5742 /** Normal output {@linkplain #fse format} */
5743 public static final int FSE_NONE
= 0;
5744 /** <i>FIX</i> output {@linkplain #fse format} */
5745 public static final int FSE_FIX
= 1;
5746 /** <i>SCI</i> output {@linkplain #fse format} */
5747 public static final int FSE_SCI
= 2;
5748 /** <i>ENG</i> output {@linkplain #fse format} */
5749 public static final int FSE_ENG
= 3;
5750 /** No {@linkplain #align alignment} */
5751 public static final int ALIGN_NONE
= 0;
5752 /** Left {@linkplain #align alignment} */
5753 public static final int ALIGN_LEFT
= 1;
5754 /** Right {@linkplain #align alignment} */
5755 public static final int ALIGN_RIGHT
= 2;
5756 /** Center {@linkplain #align alignment} */
5757 public static final int ALIGN_CENTER
= 3;
5759 private String
align(StringBuffer s
, NumberFormat format
) {
5760 if (format
.align
== NumberFormat
.ALIGN_LEFT
) {
5761 while (s
.length()<format
.maxwidth
)
5763 } else if (format
.align
== NumberFormat
.ALIGN_RIGHT
) {
5764 while (s
.length()<format
.maxwidth
)
5766 } else if (format
.align
== NumberFormat
.ALIGN_CENTER
) {
5767 while (s
.length()<format
.maxwidth
) {
5769 if (s
.length()<format
.maxwidth
)
5773 return s
.toString();
5775 private static byte[] ftoaDigits
= new byte[65];
5776 private static StringBuffer ftoaBuf
= new StringBuffer(40);
5777 private static StringBuffer ftoaExp
= new StringBuffer(15);
5779 * This string holds the only valid characters to use in hexadecimal
5780 * numbers. Equals <code>"0123456789ABCDEF"</code>.
5781 * See {@link #assign(String,int)}.
5783 public static final String hexChar
= "0123456789ABCDEF";
5784 private String
ftoa(NumberFormat format
) {
5785 ftoaBuf
.setLength(0);
5786 if ((this.exponent
< 0 && this.mantissa
!= 0)) {
5787 ftoaBuf
.append("nan");
5788 return align(ftoaBuf
,format
);
5790 if ((this.exponent
< 0 && this.mantissa
== 0)) {
5791 ftoaBuf
.append((this.sign
!=0) ?
"-inf":"inf");
5792 return align(ftoaBuf
,format
);
5794 int digitsPerThousand
;
5795 switch (format
.base
) {
5797 digitsPerThousand
= 8;
5800 digitsPerThousand
= 1000; // Disable thousands separator
5803 digitsPerThousand
= 4;
5807 digitsPerThousand
= 3;
5810 if (format
.thousand
== 0)
5811 digitsPerThousand
= 1000; // Disable thousands separator
5812 { tmp4
.mantissa
= this.mantissa
; tmp4
.exponent
= this.exponent
; tmp4
.sign
= this.sign
; };
5813 int accurateDigits
= tmp4
.getDigits(ftoaDigits
, format
.base
);
5814 if (format
.base
== 10 && (exponent
> 0x4000003e || !isIntegral()))
5815 accurateDigits
= 16; // Only display 16 digits for non-integers
5820 int width
= format
.maxwidth
-1; // subtract 1 for decimal point
5822 if (format
.base
!= 10)
5823 prefix
= 1; // want room for at least one "0" or "f/7/1"
5824 else if ((tmp4
.sign
!=0))
5825 width
--; // subtract 1 for sign
5826 boolean useExp
= false;
5827 switch (format
.fse
) {
5828 case NumberFormat
.FSE_SCI
:
5829 precision
= format
.precision
+1;
5832 case NumberFormat
.FSE_ENG
:
5833 pointPos
= floorMod(tmp4
.exponent
,3);
5834 precision
= format
.precision
+1+pointPos
;
5837 case NumberFormat
.FSE_FIX
:
5838 case NumberFormat
.FSE_NONE
:
5841 if (format
.fse
== NumberFormat
.FSE_FIX
)
5842 precision
= format
.precision
+1;
5843 if (tmp4
.exponent
+1 >
5844 width
-(tmp4
.exponent
+prefix
)/digitsPerThousand
-prefix
+
5845 (format
.removePoint ?
1:0) ||
5846 tmp4
.exponent
+1 > accurateDigits
||
5847 -tmp4
.exponent
>= width
||
5848 -tmp4
.exponent
>= precision
)
5852 pointPos
= tmp4
.exponent
;
5853 precision
+= tmp4
.exponent
;
5854 if (tmp4
.exponent
> 0)
5855 width
-= (tmp4
.exponent
+prefix
)/digitsPerThousand
;
5856 if (format
.removePoint
&& tmp4
.exponent
==width
-prefix
){
5857 // Add 1 for the decimal point that will be removed
5863 if (prefix
!=0 && pointPos
>=0)
5865 ftoaExp
.setLength(0);
5867 ftoaExp
.append('e');
5868 ftoaExp
.append(tmp4
.exponent
-pointPos
);
5869 width
-= ftoaExp
.length();
5871 if (precision
> accurateDigits
)
5872 precision
= accurateDigits
;
5873 if (precision
> width
)
5875 if (precision
> width
+pointPos
) // In case of negative pointPos
5876 precision
= width
+pointPos
;
5880 while (tmp4
.carryWhenRounded(ftoaDigits
,precision
,format
.base
));
5881 tmp4
.round(ftoaDigits
,precision
,format
.base
);
5882 // Start generating the string. First the sign
5883 if ((tmp4
.sign
!=0) && format
.base
== 10)
5884 ftoaBuf
.append('-');
5885 // Save pointPos for hex/oct/bin prefixing with thousands-sep
5886 int pointPos2
= pointPos
< 0 ?
0 : pointPos
;
5887 // Add leading zeros (or f/7/1)
5888 char prefixChar
= (format
.base
==10 || (tmp4
.sign
==0)) ?
'0' :
5889 hexChar
.charAt(format
.base
-1);
5891 ftoaBuf
.append(prefixChar
);
5892 ftoaBuf
.append(format
.point
);
5893 while (pointPos
< -1) {
5894 ftoaBuf
.append(prefixChar
);
5898 // Add fractional part
5899 for (int i
=0; i
<precision
; i
++) {
5900 ftoaBuf
.append(hexChar
.charAt(ftoaDigits
[i
]));
5901 if (pointPos
>0 && pointPos
%digitsPerThousand
==0)
5902 ftoaBuf
.append(format
.thousand
);
5904 ftoaBuf
.append(format
.point
);
5907 if (format
.fse
== NumberFormat
.FSE_NONE
) {
5908 // Remove trailing zeros
5909 while (ftoaBuf
.charAt(ftoaBuf
.length()-1) == '0')
5910 ftoaBuf
.setLength(ftoaBuf
.length()-1);
5912 if (format
.removePoint
) {
5913 // Remove trailing point
5914 if (ftoaBuf
.charAt(ftoaBuf
.length()-1) == format
.point
)
5915 ftoaBuf
.setLength(ftoaBuf
.length()-1);
5918 ftoaBuf
.append(ftoaExp
.toString());
5919 // In case hex/oct/bin number, prefix with 0's or f/7/1's
5920 if (format
.base
!=10) {
5921 while (ftoaBuf
.length()<format
.maxwidth
) {
5923 if (pointPos2
>0 && pointPos2
%digitsPerThousand
==0)
5924 ftoaBuf
.insert(0,format
.thousand
);
5925 if (ftoaBuf
.length()<format
.maxwidth
)
5926 ftoaBuf
.insert(0,prefixChar
);
5928 if (ftoaBuf
.charAt(0) == format
.thousand
)
5929 ftoaBuf
.deleteCharAt(0);
5931 return align(ftoaBuf
,format
);
5933 private static NumberFormat tmpFormat
= new NumberFormat();
5935 * Converts this <code>Real</code> to a <code>String</code> using
5936 * the default <code>NumberFormat</code>.
5938 * <p>See {@link Real.NumberFormat NumberFormat} for a description
5939 * of the default way that numbers are formatted.
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:
5949 * </td></tr></table>
5951 * @return a <code>String</code> representation of this <code>Real</code>.
5953 public String
toString() {
5954 tmpFormat
.base
= 10;
5955 return ftoa(tmpFormat
);
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.
5962 * <p>See {@link Real.NumberFormat NumberFormat} for a description
5963 * of the default way that numbers are formatted.
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>
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>
5974 * </td></tr><tr><td>base-8</td><td>
5976 * </td></tr><tr><td>base-10</td><td>
5978 * </td></tr><tr><td>base-16 </td><td>
5980 * </td></tr></table>
5982 * @param base the base for the conversion. Valid base values are
5984 * @return a <code>String</code> representation of this <code>Real</code>.
5986 public String
toString(int base
) {
5987 tmpFormat
.base
= base
;
5988 return ftoa(tmpFormat
);
5991 * Converts this <code>Real</code> to a <code>String</code> using
5992 * the given <code>NumberFormat</code>.
5994 * <p>See {@link Real.NumberFormat NumberFormat} for a description of the
5995 * various ways that numbers may be formatted.
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>
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>
6006 * </td></tr><tr><td>base-8</td><td>
6008 * </td></tr><tr><td>base-10</td><td>
6010 * </td></tr><tr><td>base-16 </td><td>
6012 * </td></tr></table>
6014 * @param format the number format to use in the conversion.
6015 * @return a <code>String</code> representation of this <code>Real</code>.
6017 public String
toString(NumberFormat format
) {
6018 return ftoa(format
);