DEADSOFTWARE

Add script for build default stubs and libs
[mp3cc.git] / mps / src / Real.java
7 /**
8 * <b>Java integer implementation of 63-bit precision floating point.</b>
9 * <br><i>Version 1.13</i>
10 *
11 * <p>Copyright 2003-2009 Roar Lauritzsen <roarl@pvv.org>
12 *
13 * <blockquote>
14 *
15 * <p>This library is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at your option)
18 * any later version.
19 *
20 * <p>This library is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
24 *
25 * <p>The following link provides a copy of the GNU General Public License:
26 * <br>&nbsp;&nbsp;&nbsp;&nbsp;<a
27 * href="http://www.gnu.org/licenses/gpl.txt">http://www.gnu.org/licenses/gpl.txt</a>
28 * <br>If you are unable to obtain the copy from this address, write to the
29 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 * 02111-1307 USA
31 *
32 * </blockquote>
33 *
34 * <p><b>General notes</b>
35 * <ul>
36 *
37 * <li><code>Real</code> objects are not immutable, like Java
38 * <code>Double</code> or <code>BigDecimal</code>. This means that you
39 * should not think of a <code>Real</code> object as a "number", but rather
40 * as a "register holding a number". This design choice is done to encourage
41 * object reuse and limit garbage production for more efficient execution on
42 * e.g. a limited MIDP device. The design choice is reflected in the API,
43 * where an operation like {@link #add(Real) add} does not return a new
44 * object containing the result (as with {@link
45 * java.math.BigDecimal#add(java.math.BigDecimal) BigDecimal}), but rather
46 * adds the argument to the object itself, and returns nothing.
47 *
48 * <li>This library implements infinities and NaN (Not-a-Number) following
49 * the IEEE 754 logic. If an operation produces a result larger (in
50 * magnitude) than the largest representable number, a value representing
51 * positive or negative infinity is generated. If an operation produces a
52 * result smaller than the smallest representable number, a positive or
53 * negative zero is generated. If an operation is undefined, a NaN value is
54 * produced. Abnormal numbers are often fine to use in further
55 * calculations. In most cases where the final result would be meaningful,
56 * abnormal numbers accomplish this, e.g. atan(1/0)=&pi;/2. In most cases
57 * where the final result is not meaningful, a NaN will be produced.
58 * <i>No exception is ever (deliberately) thrown.</i>
59 *
60 * <li>Error bounds listed under <a href="#method_detail">Method Detail</a>
61 * are calculated using William Rossi's <a
62 * href="http://dfp.sourceforge.net/">rossi.dfp.dfp</a> at 40 decimal digits
63 * accuracy. Error bounds are for "typical arguments" and may increase when
64 * results approach zero or
65 * infinity. The abbreviation {@link Math#ulp(double) ULP} means Unit in the
66 * Last Place. An error bound of ½ ULP means that the result is correctly
67 * rounded. The relative execution time listed under each method is the
68 * average from running on SonyEricsson T610 (R3C), K700i, and Nokia 6230i.
69 *
70 * <li>The library is not thread-safe. Static <code>Real</code> objects are
71 * used extensively as temporary values to avoid garbage production and the
72 * overhead of <code>new</code>. To make the library thread-safe, references
73 * to all these static objects must be replaced with code that instead
74 * allocates new <code>Real</code> objects in their place.
75 *
76 * <li>There is one bug that occurs again and again and is really difficult
77 * to debug. Although the pre-calculated constants are declared <code>static
78 * final</code>, Java cannot really protect the contents of the objects in
79 * the same way as <code>const</code>s are protected in C/C++. Consequently,
80 * you can accidentally change these values if you send them into a function
81 * that modifies its arguments. If you were to modify {@link #ONE Real.ONE}
82 * for instance, many of the succeeding calculations would be wrong because
83 * the same variable is used extensively in the internal calculations of
84 * Real.java.
85 *
86 * </ul>
87 */
88 public final class Real
89 {
90 /**
91 * The mantissa of a <code>Real</code>. <i>To maintain numbers in a
92 * normalized state and to preserve the integrity of abnormal numbers, it
93 * is discouraged to modify the inner representation of a
94 * <code>Real</code> directly.</i>
95 *
96 * <p>The number represented by a <code>Real</code> equals:<br>
97 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-1<sup>sign</sup>&nbsp;·&nbsp;mantissa&nbsp;·&nbsp;2<sup>-62</sup>&nbsp;·&nbsp;2<sup>exponent-0x40000000</sup>
98 *
99 * <p>The normalized mantissa of a finite <code>Real</code> must be
100 * between <code>0x4000000000000000L</code> and
101 * <code>0x7fffffffffffffffL</code>. Using a denormalized
102 * <code>Real</code> in <u>any</u> operation other than {@link
103 * #normalize()} may produce undefined results. The mantissa of zero and
104 * of an infinite value is <code>0x0000000000000000L</code>.
106 * <p>The mantissa of a NaN is any nonzero value. However, it is
107 * recommended to use the value <code>0x4000000000000000L</code>. Any
108 * other values are reserved for future extensions.
109 */
110 public long mantissa;
111 /**
112 * The exponent of a <code>Real</code>. <i>To maintain numbers in a
113 * normalized state and to preserve the integrity of abnormal numbers, it
114 * is discouraged to modify the inner representation of a
115 * <code>Real</code> directly.</i>
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
124 * extensions.
125 */
126 public int exponent;
127 /**
128 * The sign of a <code>Real</code>. <i>To maintain numbers in a normalized
129 * state and to preserve the integrity of abnormal numbers, it is
130 * discouraged to modify the inner representation of a <code>Real</code>
131 * directly.</i>
133 * <p>The sign of a finite, zero or infinite <code>Real</code> is 0 for
134 * positive values and 1 for negative values. Any other values may produce
135 * undefined results.
137 * <p>The sign of a NaN is ignored. However, it is recommended to use the
138 * value <code>0</code>. Any other values are reserved for future
139 * extensions.
140 */
141 public byte sign;
142 /**
143 * Set to <code>false</code> during numerical algorithms to favor accuracy
144 * over prettyness. This flag is initially set to <code>true</code>.
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.
158 */
159 public static boolean magicRounding = true;
160 /**
161 * A <code>Real</code> constant holding the exact value of 0. Among other
162 * uses, this value is used as a result when a positive underflow occurs.
163 */
164 public static final Real ZERO = new Real(0,0x00000000,0x0000000000000000L);
165 /**
166 * A <code>Real</code> constant holding the exact value of 1.
167 */
168 public static final Real ONE = new Real(0,0x40000000,0x4000000000000000L);
169 /**
170 * A <code>Real</code> constant holding the exact value of 2.
171 */
172 public static final Real TWO = new Real(0,0x40000001,0x4000000000000000L);
173 /**
174 * A <code>Real</code> constant holding the exact value of 3.
175 */
176 public static final Real THREE= new Real(0,0x40000001,0x6000000000000000L);
177 /**
178 * A <code>Real</code> constant holding the exact value of 5.
179 */
180 public static final Real FIVE = new Real(0,0x40000002,0x5000000000000000L);
181 /**
182 * A <code>Real</code> constant holding the exact value of 10.
183 */
184 public static final Real TEN = new Real(0,0x40000003,0x5000000000000000L);
185 /**
186 * A <code>Real</code> constant holding the exact value of 100.
187 */
188 public static final Real HUNDRED=new Real(0,0x40000006,0x6400000000000000L);
189 /**
190 * A <code>Real</code> constant holding the exact value of 1/2.
191 */
192 public static final Real HALF = new Real(0,0x3fffffff,0x4000000000000000L);
193 /**
194 * A <code>Real</code> constant that is closer than any other to 1/3.
195 */
196 public static final Real THIRD= new Real(0,0x3ffffffe,0x5555555555555555L);
197 /**
198 * A <code>Real</code> constant that is closer than any other to 1/10.
199 */
200 public static final Real TENTH= new Real(0,0x3ffffffc,0x6666666666666666L);
201 /**
202 * A <code>Real</code> constant that is closer than any other to 1/100.
203 */
204 public static final Real PERCENT=new Real(0,0x3ffffff9,0x51eb851eb851eb85L);
205 /**
206 * A <code>Real</code> constant that is closer than any other to the
207 * square root of 2.
208 */
209 public static final Real SQRT2= new Real(0,0x40000000,0x5a827999fcef3242L);
210 /**
211 * A <code>Real</code> constant that is closer than any other to the
212 * square root of 1/2.
213 */
214 public static final Real SQRT1_2=new Real(0,0x3fffffff,0x5a827999fcef3242L);
215 /**
216 * A <code>Real</code> constant that is closer than any other to 2&pi;.
217 */
218 public static final Real PI2 = new Real(0,0x40000002,0x6487ed5110b4611aL);
219 /**
220 * A <code>Real</code> constant that is closer than any other to &pi;, the
221 * ratio of the circumference of a circle to its diameter.
222 */
223 public static final Real PI = new Real(0,0x40000001,0x6487ed5110b4611aL);
224 /**
225 * A <code>Real</code> constant that is closer than any other to &pi;/2.
226 */
227 public static final Real PI_2 = new Real(0,0x40000000,0x6487ed5110b4611aL);
228 /**
229 * A <code>Real</code> constant that is closer than any other to &pi;/4.
230 */
231 public static final Real PI_4 = new Real(0,0x3fffffff,0x6487ed5110b4611aL);
232 /**
233 * A <code>Real</code> constant that is closer than any other to &pi;/8.
234 */
235 public static final Real PI_8 = new Real(0,0x3ffffffe,0x6487ed5110b4611aL);
236 /**
237 * A <code>Real</code> constant that is closer than any other to <i>e</i>,
238 * the base of the natural logarithms.
239 */
240 public static final Real E = new Real(0,0x40000001,0x56fc2a2c515da54dL);
241 /**
242 * A <code>Real</code> constant that is closer than any other to the
243 * natural logarithm of 2.
244 */
245 public static final Real LN2 = new Real(0,0x3fffffff,0x58b90bfbe8e7bcd6L);
246 /**
247 * A <code>Real</code> constant that is closer than any other to the
248 * natural logarithm of 10.
249 */
250 public static final Real LN10 = new Real(0,0x40000001,0x49aec6eed554560bL);
251 /**
252 * A <code>Real</code> constant that is closer than any other to the
253 * base-2 logarithm of <i>e</i>.
254 */
255 public static final Real LOG2E= new Real(0,0x40000000,0x5c551d94ae0bf85eL);
256 /**
257 * A <code>Real</code> constant that is closer than any other to the
258 * base-10 logarithm of <i>e</i>.
259 */
260 public static final Real LOG10E=new Real(0,0x3ffffffe,0x6f2dec549b9438cbL);
261 /**
262 * A <code>Real</code> constant holding the maximum non-infinite positive
263 * number = 4.197e323228496.
264 */
265 public static final Real MAX = new Real(0,0x7fffffff,0x7fffffffffffffffL);
266 /**
267 * A <code>Real</code> constant holding the minimum non-zero positive
268 * number = 2.383e-323228497.
269 */
270 public static final Real MIN = new Real(0,0x00000000,0x4000000000000000L);
271 /**
272 * A <code>Real</code> constant holding the value of NaN (not-a-number).
273 * This value is always used as a result to signal an invalid operation.
274 */
275 public static final Real NAN = new Real(0,0x80000000,0x4000000000000000L);
276 /**
277 * A <code>Real</code> constant holding the value of positive infinity.
278 * This value is always used as a result to signal a positive overflow.
279 */
280 public static final Real INF = new Real(0,0x80000000,0x0000000000000000L);
281 /**
282 * A <code>Real</code> constant holding the value of negative infinity.
283 * This value is always used as a result to signal a negative overflow.
284 */
285 public static final Real INF_N= new Real(1,0x80000000,0x0000000000000000L);
286 /**
287 * A <code>Real</code> constant holding the value of negative zero. This
288 * value is used as a result e.g. when a negative underflow occurs.
289 */
290 public static final Real ZERO_N=new Real(1,0x00000000,0x0000000000000000L);
291 /**
292 * A <code>Real</code> constant holding the exact value of -1.
293 */
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 };
299 /**
300 * Creates a new <code>Real</code> with a value of zero.
301 */
302 public Real() {
304 /**
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.
309 */
310 public Real(Real a) {
311 { this.mantissa = a.mantissa; this.exponent = a.exponent; this.sign = a.sign; };
313 /**
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.
318 */
319 public Real(int a) {
320 assign(a);
322 /**
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.
327 */
328 public Real(long a) {
329 assign(a);
331 /**
332 * Creates a new <code>Real</code>, assigning the value encoded in a
333 * <code>String</code> using base-10. See {@link #assign(String)}.
335 * @param a the <code>String</code> to assign.
336 */
337 public Real(String a) {
338 assign(a,10);
340 /**
341 * Creates a new <code>Real</code>, assigning the value encoded in a
342 * <code>String</code> using the specified number base. See {@link
343 * #assign(String,int)}.
345 * @param a the <code>String</code> to assign.
346 * @param base the number base of <code>a</code>. Valid base values are 2,
347 * 8, 10 and 16.
348 */
349 public Real(String a, int base) {
350 assign(a,base);
352 /**
353 * Creates a new <code>Real</code>, assigning a value by directly setting
354 * the fields of the internal representation. The arguments must represent
355 * a valid, normalized <code>Real</code>. This is the fastest way of
356 * creating a constant value. See {@link #assign(int,int,long)}.
358 * @param s {@link #sign} bit, 0 for positive sign, 1 for negative sign
359 * @param e {@link #exponent}
360 * @param m {@link #mantissa}
361 */
362 public Real(int s, int e, long m) {
363 { this.sign=(byte)s; this.exponent=e; this.mantissa=m; };
365 /**
366 * Creates a new <code>Real</code>, assigning the value previously encoded
367 * into twelve consecutive bytes in a byte array using {@link
368 * #toBytes(byte[],int) toBytes}. See {@link #assign(byte[],int)}.
370 * @param data byte array to decode into this <code>Real</code>.
371 * @param offset offset to start encoding from. The bytes
372 * <code>data[offset]...data[offset+11]</code> will be
373 * read.
374 */
375 public Real(byte [] data, int offset) {
376 assign(data,offset);
378 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
384 * <code>this = a;</code>
385 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
386 * 0 ULPs
387 * </td></tr><tr><td><i>
388 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
389 * </i></td><td>
390 * 0.3
391 * </td></tr></table>
393 * @param a the <code>Real</code> to assign.
394 */
395 public void assign(Real a) {
396 if (a == null) {
397 makeZero();
398 return;
400 sign = a.sign;
401 exponent = a.exponent;
402 mantissa = a.mantissa;
404 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
411 * <code>this = (double)a;</code>
412 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
413 * 0 ULPs
414 * </td></tr><tr><td><i>
415 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
416 * </i></td><td>
417 * 0.6
418 * </td></tr></table>
420 * @param a the <code>int</code> to assign.
421 */
422 public void assign(int a) {
423 if (a==0) {
424 makeZero();
425 return;
427 sign = 0;
428 if (a<0) {
429 sign = 1;
430 a = -a; // Also works for 0x80000000
432 // Normalize int
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);
438 /**
439 * Assigns this <code>Real</code> the value of a signed long integer.
440 * All long values can be represented without loss of accuracy.
442 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
443 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
444 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
445 * <code>this = (double)a;</code>
446 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
447 * 0 ULPs
448 * </td></tr><tr><td><i>
449 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
450 * </i></td><td>
451 * 1.0
452 * </td></tr></table>
454 * @param a the <code>long</code> to assign.
455 */
456 public void assign(long a) {
457 sign = 0;
458 if (a<0) {
459 sign = 1;
460 a = -a; // Also works for 0x8000000000000000
462 exponent = 0x4000003E;
463 mantissa = a;
464 normalize();
466 /**
467 * Assigns this <code>Real</code> a value encoded in a <code>String</code>
468 * using base-10, as specified in {@link #assign(String,int)}.
470 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
471 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
472 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
473 * <code>this = Double.{@link Double#valueOf(String) valueOf}(a);</code>
474 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
475 * ½-1 ULPs
476 * </td></tr><tr><td><i>
477 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
478 * </i></td><td>
479 * 80
480 * </td></tr></table>
482 * @param a the <code>String</code> to assign.
483 */
484 public void assign(String a) {
485 assign(a,10);
487 /**
488 * Assigns this <code>Real</code> a value encoded in a <code>String</code>
489 * using the specified number base. The string is parsed as follows:
491 * <ul>
492 * <li>If the string is <code>null</code> or an empty string, zero is
493 * assigned.
494 * <li>Leading spaces are ignored.
495 * <li>An optional sign, '+', '-' or '/', where '/' precedes a negative
496 * two's-complement number, reading: "an infinite number of 1-bits
497 * preceding the number".
498 * <li>Optional digits preceding the radix, in the specified base.
499 * <ul>
500 * <li>In base-2, allowed digits are '01'.
501 * <li>In base-8, allowed digits are '01234567'.
502 * <li>In base-10, allowed digits are '0123456789'.
503 * <li>In base-16, allowed digits are '0123456789ABCDEF'.
504 </ul>
505 * <li>An optional radix character, '.' or ','.
506 * <li>Optional digits following the radix.
507 * <li>The following spaces are ignored.
508 * <li>An optional exponent indicator, 'e'. If not base-16, or after a
509 * space, 'E' is also accepted.
510 * <li>An optional sign, '+' or '-'.
511 * <li>Optional exponent digits <i><b>in base-10</b></i>.
512 * </ul>
514 * <p><i>Valid examples:</i><br>
515 * &nbsp;&nbsp;&nbsp;&nbsp;base-2: <code>"-.110010101e+5"</code><br>
516 * &nbsp;&nbsp;&nbsp;&nbsp;base-8: <code>"+5462E-99"</code><br>
517 * &nbsp;&nbsp;&nbsp;&nbsp;base-10: <code>"&nbsp;&nbsp;3,1415927"</code><br>
518 * &nbsp;&nbsp;&nbsp;&nbsp;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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td>
529 * <td colspan="2">
530 * <code>this = Double.{@link Double#valueOf(String) valueOf}(a);
531 * // Works only for base-10</code>
532 * </td></tr><tr><td valign="top" rowspan="2"><i>
533 * Approximate&nbsp;error&nbsp;bound:</i>
534 * </td><td width="1%">base-10</td><td>
535 * ½-1 ULPs
536 * </td></tr><tr><td>2/8/16</td><td>
537 * ½ ULPs
538 * </td></tr><tr><td valign="top" rowspan="4"><i>
539 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;</i>
540 * </td><td width="1%">base-2</td><td>
541 * 54
542 * </td></tr><tr><td>base-8</td><td>
543 * 60
544 * </td></tr><tr><td>base-10</td><td>
545 * 80
546 * </td></tr><tr><td>base-16&nbsp;&nbsp;</td><td>
547 * 60
548 * </td></tr></table>
550 * @param a the <code>String</code> to assign.
551 * @param base the number base of <code>a</code>. Valid base values are
552 * 2, 8, 10 and 16.
553 */
554 public void assign(String a, int base) {
555 if (a==null || a.length()==0) {
556 assign(ZERO);
557 return;
559 atof(a,base);
561 /**
562 * Assigns this <code>Real</code> a value by directly setting the fields
563 * of the internal representation. The arguments must represent a valid,
564 * normalized <code>Real</code>. This is the fastest way of assigning a
565 * constant value.
567 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
568 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
569 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;bound:</i></td><td>
574 * 0 ULPs
575 * </td></tr><tr><td><i>
576 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
577 * </i></td><td>
578 * 0.3
579 * </td></tr></table>
581 * @param s {@link #sign} bit, 0 for positive sign, 1 for negative sign
582 * @param e {@link #exponent}
583 * @param m {@link #mantissa}
584 */
585 public void assign(int s, int e, long m) {
586 sign = (byte)s;
587 exponent = e;
588 mantissa = m;
590 /**
591 * Assigns this <code>Real</code> a value previously encoded into into
592 * twelve consecutive bytes in a byte array using {@link
593 * #toBytes(byte[],int) toBytes}.
595 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
596 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
597 * Error&nbsp;bound:</i></td><td>
598 * 0 ULPs
599 * </td></tr><tr><td><i>
600 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
601 * </i></td><td>
602 * 1.2
603 * </td></tr></table>
605 * @param data byte array to decode into this <code>Real</code>.
606 * @param offset offset to start encoding from. The bytes
607 * <code>data[offset]...data[offset+11]</code> will be
608 * read.
609 */
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)));
625 /**
626 * Encodes an accurate representation of this <code>Real</code> value into
627 * twelve consecutive bytes in a byte array. Can be decoded using {@link
628 * #assign(byte[],int)}.
630 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
631 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
632 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
633 * </i></td><td>
634 * 1.2
635 * </td></tr></table>
637 * @param data byte array to save this <code>Real</code> in.
638 * @param offset offset to start encoding to. The bytes
639 * <code>data[offset]...data[offset+11]</code> will be
640 * written.
641 */
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);
656 /**
657 * Assigns this <code>Real</code> the value corresponding to a given bit
658 * representation. The argument is considered to be a representation of a
659 * floating-point value according to the IEEE 754 floating-point "single
660 * format" bit layout.
662 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
663 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
664 * Equivalent&nbsp;</i><code>float</code><i>&nbsp;code:</i></td><td>
665 * <code>this = Float.{@link Float#intBitsToFloat(int)
666 * intBitsToFloat}(bits);</code>
667 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
668 * 0 ULPs
669 * </td></tr><tr><td><i>
670 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
671 * </i></td><td>
672 * 0.6
673 * </td></tr></table>
675 * @param bits a <code>float</code> value encoded in an <code>int</code>.
676 */
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;
686 normalize();
687 return;
689 if (exponent <= 254) {
690 // Normal IEEE 754 float
691 exponent += 0x40000000-127;
692 mantissa |= 1L<<62;
693 return;
695 if (mantissa == 0)
696 makeInfinity(sign);
697 else
698 makeNan();
700 /**
701 * Assigns this <code>Real</code> the value corresponding to a given bit
702 * representation. The argument is considered to be a representation of a
703 * floating-point value according to the IEEE 754 floating-point "double
704 * format" bit layout.
706 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
707 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
708 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
709 * <code>this = Double.{@link Double#longBitsToDouble(long)
710 * longBitsToDouble}(bits);</code>
711 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
712 * 0 ULPs
713 * </td></tr><tr><td><i>
714 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
715 * </i></td><td>
716 * 0.6
717 * </td></tr></table>
719 * @param bits a <code>double</code> value encoded in a <code>long</code>.
720 */
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;
730 normalize();
731 return;
733 if (exponent <= 2046) {
734 // Normal IEEE 754 float
735 exponent += 0x40000000-1023;
736 mantissa |= 1L<<62;
737 return;
739 if (mantissa == 0)
740 makeInfinity(sign);
741 else
742 makeNan();
744 /**
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&nbsp;</i><code>float</code><i>&nbsp;code:</i></td><td>
751 * <code>Float.{@link Float#floatToIntBits(float)
752 * floatToIntBits}(this)</code>
753 * </td></tr><tr><td><i>
754 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
755 * </i></td><td>
756 * 0.7
757 * </td></tr></table>
759 * @return the bits that represent the floating-point number.
760 */
761 public int toFloatBits() {
762 if ((this.exponent < 0 && this.mantissa != 0))
763 return 0x7fffffff; // nan
764 int e = exponent-0x40000000+127;
765 long m = mantissa;
766 // Round properly!
767 m += 1L<<38;
768 if (m<0) {
769 m >>>= 1;
770 e++;
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);
783 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
790 * <code>Double.{@link Double#doubleToLongBits(double)
791 * doubleToLongBits}(this)</code>
792 * </td></tr><tr><td><i>
793 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
794 * </i></td><td>
795 * 0.7
796 * </td></tr></table>
798 * @return the bits that represent the floating-point number.
799 */
800 public long toDoubleBits() {
801 if ((this.exponent < 0 && this.mantissa != 0))
802 return 0x7fffffffffffffffL; // nan
803 int e = exponent-0x40000000+1023;
804 long m = mantissa;
805 // Round properly!
806 m += 1L<<9;
807 if (m<0) {
808 m >>>= 1;
809 e++;
810 if (exponent < 0)
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);
822 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
828 * <code>this = 0;</code>
829 * </td></tr><tr><td><i>
830 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
831 * </i></td><td>
832 * 0.2
833 * </td></tr></table>
834 */
835 public void makeZero() {
836 sign = 0;
837 mantissa = 0;
838 exponent = 0;
840 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
846 * <code>this = 0.0 * (1-2*s);</code>
847 * </td></tr><tr><td><i>
848 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
849 * </i></td><td>
850 * 0.2
851 * </td></tr></table>
853 * @param s sign bit, 0 to make a positive zero, 1 to make a negative zero
854 */
855 public void makeZero(int s) {
856 sign = (byte)s;
857 mantissa = 0;
858 exponent = 0;
860 /**
861 * Makes this <code>Real</code> the value of infinity with the specified
862 * sign.
864 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
865 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
866 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
867 * <code>this = Double.{@link Double#POSITIVE_INFINITY POSITIVE_INFINITY}
868 * * (1-2*s);</code>
869 * </td></tr><tr><td><i>
870 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
871 * </i></td><td>
872 * 0.3
873 * </td></tr></table>
875 * @param s sign bit, 0 to make positive infinity, 1 to make negative
876 * infinity
877 */
878 public void makeInfinity(int s) {
879 sign = (byte)s;
880 mantissa = 0;
881 exponent = 0x80000000;
883 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
889 * <code>this = Double.{@link Double#NaN NaN};</code>
890 * </td></tr><tr><td><i>
891 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
892 * </i></td><td>
893 * 0.3
894 * </td></tr></table>
895 */
896 public void makeNan() {
897 sign = 0;
898 mantissa = 0x4000000000000000L;
899 exponent = 0x80000000;
901 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
908 * <code>(this == 0)</code>
909 * </td></tr><tr><td><i>
910 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
911 * </i></td><td>
912 * 0.3
913 * </td></tr></table>
915 * @return <code>true</code> if the value represented by this object is
916 * zero, <code>false</code> otherwise.
917 */
918 public boolean isZero() {
919 return (exponent == 0 && mantissa == 0);
921 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
928 * <code>Double.{@link Double#isInfinite(double) isInfinite}(this)</code>
929 * </td></tr><tr><td><i>
930 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
931 * </i></td><td>
932 * 0.3
933 * </td></tr></table>
935 * @return <code>true</code> if the value represented by this object is
936 * infinite, <code>false</code> if it is finite or NaN.
937 */
938 public boolean isInfinity() {
939 return (exponent < 0 && mantissa == 0);
941 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
948 * <code>Double.{@link Double#isNaN(double) isNaN}(this)</code>
949 * </td></tr><tr><td><i>
950 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
951 * </i></td><td>
952 * 0.3
953 * </td></tr></table>
955 * @return <code>true</code> if the value represented by this object is
956 * NaN, <code>false</code> otherwise.
957 */
958 public boolean isNan() {
959 return (exponent < 0 && mantissa != 0);
961 /**
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&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
973 * </i></td><td>
974 * 0.3
975 * </td></tr></table>
977 * @return <code>true</code> if the value represented by this object is
978 * finite, <code>false</code> if it is infinite or NaN.
979 */
980 public boolean isFinite() {
981 // That is, non-infinite and non-nan
982 return (exponent >= 0);
984 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
991 * <code>(!Double.{@link Double#isNaN(double) isNaN}(this) &&
992 * !Double.{@link Double#isInfinite(double) isInfinite}(this) &&
993 * (this!=0))</code>
994 * </td></tr><tr><td><i>
995 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
996 * </i></td><td>
997 * 0.3
998 * </td></tr></table>
1000 * @return <code>true</code> if the value represented by this object is
1001 * finite and nonzero, <code>false</code> if it is infinite, NaN or
1002 * zero.
1003 */
1004 public boolean isFiniteNonZero() {
1005 // That is, non-infinite and non-nan and non-zero
1006 return (exponent >= 0 && mantissa != 0);
1008 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1015 * <code>(this < 0)</code>
1016 * </td></tr><tr><td><i>
1017 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1018 * </i></td><td>
1019 * 0.3
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.
1024 */
1025 public boolean isNegative() {
1026 return sign!=0;
1028 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1035 * <code>this = Math.{@link Math#abs(double) abs}(this);</code>
1036 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
1037 * 0 ULPs
1038 * </td></tr><tr><td><i>
1039 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1040 * </i></td><td>
1041 * 0.2
1042 * </td></tr></table>
1043 */
1044 public void abs() {
1045 sign = 0;
1047 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1054 * <code>this = -this;</code>
1055 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
1056 * 0 ULPs
1057 * </td></tr><tr><td><i>
1058 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1059 * </i></td><td>
1060 * 0.2
1061 * </td></tr></table>
1062 */
1063 public void neg() {
1064 if (!(this.exponent < 0 && this.mantissa != 0))
1065 sign ^= 1;
1067 /**
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&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;bound:</i></td><td>
1077 * 0 ULPs
1078 * </td></tr><tr><td><i>
1079 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1080 * </i></td><td>
1081 * 0.2
1082 * </td></tr></table>
1084 * @param a the <code>Real</code> to copy the sign from.
1085 */
1086 public void copysign(Real a) {
1087 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
1088 makeNan();
1089 return;
1091 sign = a.sign;
1093 /**
1094 * Readjusts the mantissa of this <code>Real</code>. The exponent is
1095 * adjusted accordingly. This is necessary when the mantissa has been
1096 * {@link #mantissa modified directly} for some purpose and may be
1097 * denormalized. The normalized mantissa of a finite <code>Real</code>
1098 * must have bit 63 cleared and bit 62 set. Using a denormalized
1099 * <code>Real</code> in <u>any</u> other operation may produce undefined
1100 * results.
1102 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1103 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1104 * Error&nbsp;bound:</i></td><td>
1105 * ½ ULPs
1106 * </td></tr><tr><td><i>
1107 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1108 * </i></td><td>
1109 * 0.7
1110 * </td></tr></table>
1111 */
1112 public void normalize() {
1113 if ((this.exponent >= 0)) {
1114 if (mantissa > 0)
1116 int clz = 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;
1121 mantissa <<= clz;
1122 exponent -= clz;
1123 if (exponent < 0) // Underflow
1124 makeZero(sign);
1126 else if (mantissa < 0)
1128 mantissa = (mantissa+1)>>>1;
1129 exponent ++;
1130 if (mantissa == 0) { // Ooops, it was 0xffffffffffffffffL
1131 mantissa = 0x4000000000000000L;
1132 exponent ++;
1134 if (exponent < 0) // Overflow
1135 makeInfinity(sign);
1137 else // mantissa == 0
1139 exponent = 0;
1143 /**
1144 * Readjusts the mantissa of a <code>Real</code> with extended
1145 * precision. The exponent is adjusted accordingly. This is necessary when
1146 * the mantissa has been {@link #mantissa modified directly} for some
1147 * purpose and may be denormalized. The normalized mantissa of a finite
1148 * <code>Real</code> must have bit 63 cleared and bit 62 set. Using a
1149 * denormalized <code>Real</code> in <u>any</u> other operation may
1150 * produce undefined results.
1152 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1153 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1154 * Approximate&nbsp;error&nbsp;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&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1158 * </i></td><td>
1159 * 0.7
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>.
1166 */
1167 public long normalize128(long extra) {
1168 if (!(this.exponent >= 0))
1169 return 0;
1170 if (mantissa == 0) {
1171 if (extra == 0) {
1172 exponent = 0;
1173 return 0;
1175 mantissa = extra;
1176 extra = 0;
1177 exponent -= 64;
1178 if (exponent < 0) { // Underflow
1179 makeZero(sign);
1180 return 0;
1183 if (mantissa < 0) {
1184 extra = (mantissa<<63)+(extra>>>1);
1185 mantissa >>>= 1;
1186 exponent ++;
1187 if (exponent < 0) { // Overflow
1188 makeInfinity(sign);
1189 return 0;
1191 return extra;
1193 int clz = 0;
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;
1198 if (clz == 0)
1199 return extra;
1200 mantissa = (mantissa<<clz)+(extra>>>(64-clz));
1201 extra <<= clz;
1202 exponent -= clz;
1203 if (exponent < 0) { // Underflow
1204 makeZero(sign);
1205 return 0;
1207 return extra;
1209 /**
1210 * Rounds an extended precision <code>Real</code> to the nearest
1211 * <code>Real</code> of normal precision. Replaces the contents of this
1212 * <code>Real</code> with the result.
1214 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1215 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1216 * Error&nbsp;bound:</i></td><td>
1217 * ½ ULPs
1218 * </td></tr><tr><td><i>
1219 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1220 * </i></td><td>
1221 * 1.0
1222 * </td></tr></table>
1224 * @param extra the extra 64 bits of mantissa of this extended precision
1225 * <code>Real</code>.
1226 */
1227 public void roundFrom128(long extra) {
1228 mantissa += (extra>>63)&1;
1229 normalize();
1231 /**
1232 * Returns <code>true</code> if this Java object is the same
1233 * object as <code>a</code>. Since a <code>Real</code> should be
1234 * thought of as a "register holding a number", this method compares the
1235 * object references, not the contents of the two objects.
1236 * This is very different from {@link #equalTo(Real)}.
1238 * @param a the object to compare to this.
1239 * @return <code>true</code> if this object is the same as <code>a</code>.
1240 */
1241 public boolean equals(Object a) {
1242 return this==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))
1247 return 0;
1248 if (sign != a.sign)
1249 return a.sign-sign;
1250 int s = (this.sign==0) ? 1 : -1;
1251 if ((this.exponent < 0 && this.mantissa == 0))
1252 return s;
1253 if ((a.exponent < 0 && a.mantissa == 0))
1254 return -s;
1255 if (exponent != a.exponent)
1256 return exponent<a.exponent ? -s : s;
1257 if (mantissa != a.mantissa)
1258 return mantissa<a.mantissa ? -s : s;
1259 return 0;
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));
1265 /**
1266 * Returns <code>true</code> if this <code>Real</code> is equal to
1267 * <code>a</code>.
1268 * If the numbers are incomparable, i.e. the values are infinities of
1269 * the same sign or any of them is NaN, <code>false</code> is always
1270 * returned. This method must not be confused with {@link #equals(Object)}.
1272 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1273 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1274 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1275 * <code>(this == a)</code>
1276 * </td></tr><tr><td><i>
1277 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1278 * </i></td><td>
1279 * 1.0
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.
1286 */
1287 public boolean equalTo(Real a) {
1288 if (invalidCompare(a))
1289 return false;
1290 return compare(a) == 0;
1292 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1299 * <code>(this == a)</code>
1300 * </td></tr><tr><td><i>
1301 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1302 * </i></td><td>
1303 * 1.7
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>
1309 * otherwise.
1310 */
1311 public boolean equalTo(int a) {
1312 tmp0.assign(a);
1313 return equalTo(tmp0);
1315 /**
1316 * Returns <code>true</code> if this <code>Real</code> is not equal to
1317 * <code>a</code>.
1318 * If the numbers are incomparable, i.e. the values are infinities of
1319 * the same sign or any of them is NaN, <code>false</code> is always
1320 * returned.
1321 * This distinguishes <code>notEqualTo(a)</code> from the expression
1322 * <code>!equalTo(a)</code>.
1324 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1325 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1326 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1327 * <code>(this != a)</code>
1328 * </td></tr><tr><td><i>
1329 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1330 * </i></td><td>
1331 * 1.0
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.
1338 */
1339 public boolean notEqualTo(Real a) {
1340 if (invalidCompare(a))
1341 return false;
1342 return compare(a) != 0;
1344 /**
1345 * Returns <code>true</code> if this <code>Real</code> is not equal to
1346 * the integer <code>a</code>.
1347 * If this <code>Real</code> is NaN, <code>false</code> is always
1348 * returned.
1349 * This distinguishes <code>notEqualTo(a)</code> from the expression
1350 * <code>!equalTo(a)</code>.
1352 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1353 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1354 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1355 * <code>(this != a)</code>
1356 * </td></tr><tr><td><i>
1357 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1358 * </i></td><td>
1359 * 1.7
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.
1366 */
1367 public boolean notEqualTo(int a) {
1368 tmp0.assign(a);
1369 return notEqualTo(tmp0);
1371 /**
1372 * Returns <code>true</code> if this <code>Real</code> is less than
1373 * <code>a</code>.
1374 * If the numbers are incomparable, i.e. the values are infinities of
1375 * the same sign or any of them is NaN, <code>false</code> is always
1376 * returned.
1378 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1379 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1380 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1381 * <code>(this &lt; a)</code>
1382 * </td></tr><tr><td><i>
1383 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1384 * </i></td><td>
1385 * 1.0
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.
1392 */
1393 public boolean lessThan(Real a) {
1394 if (invalidCompare(a))
1395 return false;
1396 return compare(a) < 0;
1398 /**
1399 * Returns <code>true</code> if this <code>Real</code> is less than
1400 * the integer <code>a</code>.
1401 * If this <code>Real</code> is NaN, <code>false</code> is always
1402 * returned.
1404 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1405 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1406 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1407 * <code>(this &lt; a)</code>
1408 * </td></tr><tr><td><i>
1409 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1410 * </i></td><td>
1411 * 1.7
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.
1418 */
1419 public boolean lessThan(int a) {
1420 tmp0.assign(a);
1421 return lessThan(tmp0);
1423 /**
1424 * Returns <code>true</code> if this <code>Real</code> is less than or
1425 * equal to <code>a</code>.
1426 * If the numbers are incomparable, i.e. the values are infinities of
1427 * the same sign or any of them is NaN, <code>false</code> is always
1428 * returned.
1430 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1431 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1432 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1433 * <code>(this &lt;= a)</code>
1434 * </td></tr><tr><td><i>
1435 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1436 * </i></td><td>
1437 * 1.0
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.
1444 */
1445 public boolean lessEqual(Real a) {
1446 if (invalidCompare(a))
1447 return false;
1448 return compare(a) <= 0;
1450 /**
1451 * Returns <code>true</code> if this <code>Real</code> is less than or
1452 * equal to the integer <code>a</code>.
1453 * If this <code>Real</code> is NaN, <code>false</code> is always
1454 * returned.
1456 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1457 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1458 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1459 * <code>(this &lt;= a)</code>
1460 * </td></tr><tr><td><i>
1461 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1462 * </i></td><td>
1463 * 1.7
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.
1470 */
1471 public boolean lessEqual(int a) {
1472 tmp0.assign(a);
1473 return lessEqual(tmp0);
1475 /**
1476 * Returns <code>true</code> if this <code>Real</code> is greater than
1477 * <code>a</code>.
1478 * If the numbers are incomparable, i.e. the values are infinities of
1479 * the same sign or any of them is NaN, <code>false</code> is always
1480 * returned.
1482 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1483 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1484 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1485 * <code>(this &gt; a)</code>
1486 * </td></tr><tr><td><i>
1487 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1488 * </i></td><td>
1489 * 1.0
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.
1496 */
1497 public boolean greaterThan(Real a) {
1498 if (invalidCompare(a))
1499 return false;
1500 return compare(a) > 0;
1502 /**
1503 * Returns <code>true</code> if this <code>Real</code> is greater than
1504 * the integer <code>a</code>.
1505 * If this <code>Real</code> is NaN, <code>false</code> is always
1506 * returned.
1508 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1509 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1510 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1511 * <code>(this &gt; a)</code>
1512 * </td></tr><tr><td><i>
1513 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1514 * </i></td><td>
1515 * 1.7
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.
1522 */
1523 public boolean greaterThan(int a) {
1524 tmp0.assign(a);
1525 return greaterThan(tmp0);
1527 /**
1528 * Returns <code>true</code> if this <code>Real</code> is greater than
1529 * or equal to <code>a</code>.
1530 * If the numbers are incomparable, i.e. the values are infinities of
1531 * the same sign or any of them is NaN, <code>false</code> is always
1532 * returned.
1534 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1535 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1536 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1537 * <code>(this &gt;= a)</code>
1538 * </td></tr><tr><td><i>
1539 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1540 * </i></td><td>
1541 * 1.0
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.
1548 */
1549 public boolean greaterEqual(Real a) {
1550 if (invalidCompare(a))
1551 return false;
1552 return compare(a) >= 0;
1554 /**
1555 * Returns <code>true</code> if this <code>Real</code> is greater than
1556 * or equal to the integer <code>a</code>.
1557 * If this <code>Real</code> is NaN, <code>false</code> is always
1558 * returned.
1560 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1561 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1562 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1563 * <code>(this &gt;= a)</code>
1564 * </td></tr><tr><td><i>
1565 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1566 * </i></td><td>
1567 * 1.7
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.
1574 */
1575 public boolean greaterEqual(int a) {
1576 tmp0.assign(a);
1577 return greaterEqual(tmp0);
1579 /**
1580 * Returns <code>true</code> if the absolute value of this
1581 * <code>Real</code> is less than the absolute value of
1582 * <code>a</code>.
1583 * If the numbers are incomparable, i.e. the values are both infinite
1584 * or any of them is NaN, <code>false</code> is always returned.
1586 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1587 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1588 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1589 * <code>(Math.{@link Math#abs(double) abs}(this) &lt;
1590 * Math.{@link Math#abs(double) abs}(a))</code>
1591 * </td></tr><tr><td><i>
1592 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1593 * </i></td><td>
1594 * 0.5
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
1600 * <code>a</code>.
1601 * <code>false</code> otherwise, or if the numbers are incomparable.
1602 */
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))
1605 return false;
1606 if ((a.exponent < 0 && a.mantissa == 0))
1607 return true;
1608 if (exponent != a.exponent)
1609 return exponent<a.exponent;
1610 return mantissa<a.mantissa;
1612 /**
1613 * Multiplies this <code>Real</code> by 2 to the power of <code>n</code>.
1614 * Replaces the contents of this <code>Real</code> with the result.
1615 * This operation is faster than normal multiplication since it only
1616 * involves adding to the exponent.
1618 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1619 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1620 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;bound:</i></td><td>
1623 * 0 ULPs
1624 * </td></tr><tr><td><i>
1625 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1626 * </i></td><td>
1627 * 0.3
1628 * </td></tr></table>
1630 * @param n the integer argument.
1631 */
1632 public void scalbn(int n) {
1633 if (!(this.exponent >= 0 && this.mantissa != 0))
1634 return;
1635 exponent += n;
1636 if (exponent < 0) {
1637 if (n<0)
1638 makeZero(sign); // Underflow
1639 else
1640 makeInfinity(sign); // Overflow
1643 /**
1644 * Calculates the next representable neighbour of this <code>Real</code>
1645 * in the direction towards <code>a</code>.
1646 * Replaces the contents of this <code>Real</code> with the result.
1647 * If the two values are equal, nothing happens.
1649 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1650 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1651 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;bound:</i></td><td>
1655 * 0 ULPs
1656 * </td></tr><tr><td><i>
1657 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1658 * </i></td><td>
1659 * 0.8
1660 * </td></tr></table>
1662 * @param a the <code>Real</code> argument.
1663 */
1664 public void nextafter(Real a) {
1665 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
1666 makeNan();
1667 return;
1669 if ((this.exponent < 0 && this.mantissa == 0) && (a.exponent < 0 && a.mantissa == 0) && sign == a.sign)
1670 return;
1671 int dir = -compare(a);
1672 if (dir == 0)
1673 return;
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);
1677 return;
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);
1682 return;
1684 if ((this.sign==0) ^ dir<0) {
1685 mantissa ++;
1686 } else {
1687 if (mantissa == 0x4000000000000000L) {
1688 mantissa <<= 1;
1689 exponent--;
1691 mantissa --;
1693 normalize();
1695 /**
1696 * Calculates the largest (closest to positive infinity)
1697 * <code>Real</code> value that is less than or equal to this
1698 * <code>Real</code> and is equal to a mathematical integer.
1699 * Replaces the contents of this <code>Real</code> with the result.
1701 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1702 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1703 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1704 * <code>this = Math.{@link Math#floor(double) floor}(this);</code>
1705 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
1706 * 0 ULPs
1707 * </td></tr><tr><td><i>
1708 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1709 * </i></td><td>
1710 * 0.5
1711 * </td></tr></table>
1712 */
1713 public void floor() {
1714 if (!(this.exponent >= 0 && this.mantissa != 0))
1715 return;
1716 if (exponent < 0x40000000) {
1717 if ((this.sign==0))
1718 makeZero(sign);
1719 else {
1720 exponent = ONE.exponent;
1721 mantissa = ONE.mantissa;
1722 // sign unchanged!
1724 return;
1726 int shift = 0x4000003e-exponent;
1727 if (shift <= 0)
1728 return;
1729 if ((this.sign!=0))
1730 mantissa += ((1L<<shift)-1);
1731 mantissa &= ~((1L<<shift)-1);
1732 if ((this.sign!=0))
1733 normalize();
1735 /**
1736 * Calculates the smallest (closest to negative infinity)
1737 * <code>Real</code> value that is greater than or equal to this
1738 * <code>Real</code> and is equal to a mathematical integer.
1739 * Replaces the contents of this <code>Real</code> with the result.
1741 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1742 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1743 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1744 * <code>this = Math.{@link Math#ceil(double) ceil}(this);</code>
1745 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
1746 * 0 ULPs
1747 * </td></tr><tr><td><i>
1748 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1749 * </i></td><td>
1750 * 1.8
1751 * </td></tr></table>
1752 */
1753 public void ceil() {
1754 neg();
1755 floor();
1756 neg();
1758 /**
1759 * Rounds this <code>Real</code> value to the closest value that is equal
1760 * to a mathematical integer. If two <code>Real</code> values that are
1761 * mathematical integers are equally close, the result is the integer
1762 * value with the largest magnitude (positive or negative). Replaces the
1763 * contents of this <code>Real</code> with the result.
1765 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1766 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1767 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1768 * <code>this = Math.{@link Math#rint(double) rint}(this);</code>
1769 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
1770 * 0 ULPs
1771 * </td></tr><tr><td><i>
1772 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1773 * </i></td><td>
1774 * 1.3
1775 * </td></tr></table>
1776 */
1777 public void round() {
1778 if (!(this.exponent >= 0 && this.mantissa != 0))
1779 return;
1780 if (exponent < 0x3fffffff) {
1781 makeZero(sign);
1782 return;
1784 int shift = 0x4000003e-exponent;
1785 if (shift <= 0)
1786 return;
1787 mantissa += 1L<<(shift-1); // Bla-bla, this works almost
1788 mantissa &= ~((1L<<shift)-1);
1789 normalize();
1791 /**
1792 * Truncates this <code>Real</code> value to the closest value towards
1793 * zero that is equal to a mathematical integer.
1794 * Replaces the contents of this <code>Real</code> with the result.
1796 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1797 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1798 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1799 * <code>this = (double)((long)this);</code>
1800 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
1801 * 0 ULPs
1802 * </td></tr><tr><td><i>
1803 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1804 * </i></td><td>
1805 * 1.2
1806 * </td></tr></table>
1807 */
1808 public void trunc() {
1809 if (!(this.exponent >= 0 && this.mantissa != 0))
1810 return;
1811 if (exponent < 0x40000000) {
1812 makeZero(sign);
1813 return;
1815 int shift = 0x4000003e-exponent;
1816 if (shift <= 0)
1817 return;
1818 mantissa &= ~((1L<<shift)-1);
1819 normalize();
1821 /**
1822 * Calculates the fractional part of this <code>Real</code> by subtracting
1823 * the closest value towards zero that is equal to a mathematical integer.
1824 * Replaces the contents of this <code>Real</code> with the result.
1826 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1827 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1828 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1829 * <code>this -= (double)((long)this);</code>
1830 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
1831 * 0 ULPs
1832 * </td></tr><tr><td><i>
1833 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1834 * </i></td><td>
1835 * 1.2
1836 * </td></tr></table>
1837 */
1838 public void frac() {
1839 if (!(this.exponent >= 0 && this.mantissa != 0) || exponent < 0x40000000)
1840 return;
1841 int shift = 0x4000003e-exponent;
1842 if (shift <= 0) {
1843 makeZero(sign);
1844 return;
1846 mantissa &= ((1L<<shift)-1);
1847 normalize();
1849 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1863 * <code>(int)this</code>
1864 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
1865 * 0 ULPs
1866 * </td></tr><tr><td><i>
1867 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1868 * </i></td><td>
1869 * 0.6
1870 * </td></tr></table>
1872 * @return an <code>int</code> representation of this <code>Real</code>.
1873 */
1874 public int toInteger() {
1875 if ((this.exponent == 0 && this.mantissa == 0) || (this.exponent < 0 && this.mantissa != 0))
1876 return 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)
1882 return 0;
1883 int shift = 0x4000003e-exponent;
1884 if (shift < 32) {
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);
1891 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1905 * <code>(long)this</code>
1906 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
1907 * 0 ULPs
1908 * </td></tr><tr><td><i>
1909 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1910 * </i></td><td>
1911 * 0.5
1912 * </td></tr></table>
1914 * @return a <code>long</code> representation of this <code>Real</code>.
1915 */
1916 public long toLong() {
1917 if ((this.exponent == 0 && this.mantissa == 0) || (this.exponent < 0 && this.mantissa != 0))
1918 return 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)
1924 return 0;
1925 int shift = 0x4000003e-exponent;
1926 if (shift < 0) {
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);
1932 /**
1933 * Returns <code>true</code> if the value of this <code>Real</code>
1934 * represents a mathematical integer. If the value is too large to
1935 * determine if it is an integer, <code>true</code> is returned.
1937 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1938 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1939 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1940 * <code>(this == (long)this)</code>
1941 * </td></tr><tr><td><i>
1942 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1943 * </i></td><td>
1944 * 0.6
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.
1949 */
1950 public boolean isIntegral() {
1951 if ((this.exponent < 0 && this.mantissa != 0))
1952 return false;
1953 if ((this.exponent == 0 && this.mantissa == 0) || (this.exponent < 0 && this.mantissa == 0))
1954 return true;
1955 if (exponent < 0x40000000)
1956 return false;
1957 int shift = 0x4000003e-exponent;
1958 if (shift <= 0)
1959 return true;
1960 return (mantissa&((1L<<shift)-1)) == 0;
1962 /**
1963 * Returns <code>true</code> if the mathematical integer represented
1964 * by this <code>Real</code> is odd. You <u>must</u> first determine
1965 * that the value is actually an integer using {@link
1966 * #isIntegral()}. If the value is too large to determine if the
1967 * integer is odd, <code>false</code> is returned.
1969 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
1970 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
1971 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1972 * <code>((((long)this)&1) == 1)</code>
1973 * </td></tr><tr><td><i>
1974 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
1975 * </i></td><td>
1976 * 0.6
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.
1981 */
1982 public boolean isOdd() {
1983 if (!(this.exponent >= 0 && this.mantissa != 0) ||
1984 exponent < 0x40000000 || exponent > 0x4000003e)
1985 return false;
1986 int shift = 0x4000003e-exponent;
1987 return ((mantissa>>>shift)&1) != 0;
1989 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
1995 * <code>tmp=this; this=a; a=tmp;</code>
1996 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
1997 * 0 ULPs
1998 * </td></tr><tr><td><i>
1999 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2000 * </i></td><td>
2001 * 0.5
2002 * </td></tr></table>
2004 * @param a the <code>Real</code> to exchange with this.
2005 */
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();
2024 /**
2025 * Calculates the sum of this <code>Real</code> and <code>a</code>.
2026 * Replaces the contents of this <code>Real</code> with the result.
2028 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2029 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2030 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2031 * <code>this += a;</code>
2032 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2033 * ½ ULPs
2034 * </td></tr><tr><td><i>
2035 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2036 * </i></td><td>
2037 * «« 1.0 »»
2038 * </td></tr></table>
2040 * @param a the <code>Real</code> to add to this.
2041 */
2042 public void add(Real a) {
2043 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
2044 makeNan();
2045 return;
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)
2049 makeNan();
2050 else
2051 makeInfinity((this.exponent < 0 && this.mantissa == 0) ? sign : a.sign);
2052 return;
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))
2058 sign=0;
2059 return;
2061 byte s;
2062 int e;
2063 long m;
2064 if (exponent > a.exponent ||
2065 (exponent == a.exponent && mantissa>=a.mantissa))
2067 s = a.sign;
2068 e = a.exponent;
2069 m = a.mantissa;
2070 } else {
2071 s = sign;
2072 e = exponent;
2073 m = mantissa;
2074 sign = a.sign;
2075 exponent = a.exponent;
2076 mantissa = a.mantissa;
2078 int shift = exponent-e;
2079 if (shift>=64)
2080 return;
2081 if (sign == s) {
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
2085 if (mantissa < 0) {
2086 // Simplified normalize()
2087 mantissa = (mantissa+1)>>>1;
2088 exponent ++;
2089 if (exponent < 0) { // Overflow
2090 makeInfinity(sign);
2091 return;
2094 } else {
2095 if (shift>0) {
2096 // Shift mantissa up to increase accuracy
2097 mantissa <<= 1;
2098 exponent --;
2099 shift --;
2101 m = -m;
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
2105 if (mantissa < 0) {
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.
2118 m = -m;
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)
2123 mantissa = 0;
2124 } else
2125 mantissa = 0;
2127 normalize();
2128 } // else... if (shift>=1 && mantissa>=0) it should be a-ok
2130 if ((this.exponent == 0 && this.mantissa == 0))
2131 sign=0;
2133 /**
2134 * Calculates the sum of this <code>Real</code> and the integer
2135 * <code>a</code>.
2136 * Replaces the contents of this <code>Real</code> with the result.
2138 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2139 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2140 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2141 * <code>this += a;</code>
2142 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2143 * ½ ULPs
2144 * </td></tr><tr><td><i>
2145 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2146 * </i></td><td>
2147 * 1.8
2148 * </td></tr></table>
2150 * @param a the <code>int</code> to add to this.
2151 */
2152 public void add(int a) {
2153 tmp0.assign(a);
2154 add(tmp0);
2156 /**
2157 * Calculates the sum of this <code>Real</code> and <code>a</code> with
2158 * extended precision. Replaces the contents of this <code>Real</code>
2159 * with the result. Returns the extra mantissa of the extended precision
2160 * result.
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2175 * <code>this += a;</code>
2176 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;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&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2180 * </i></td><td>
2181 * 2.0
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>.
2191 */
2192 public long add128(long extra, Real a, long aExtra) {
2193 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
2194 makeNan();
2195 return 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)
2199 makeNan();
2200 else
2201 makeInfinity((this.exponent < 0 && this.mantissa == 0) ? sign : a.sign);
2202 return 0;
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; };
2207 extra = aExtra;
2209 if ((this.exponent == 0 && this.mantissa == 0))
2210 sign=0;
2211 return extra;
2213 byte s;
2214 int e;
2215 long m;
2216 long x;
2217 if (exponent > a.exponent ||
2218 (exponent == a.exponent && mantissa>a.mantissa) ||
2219 (exponent == a.exponent && mantissa==a.mantissa &&
2220 (extra>>>1)>=(aExtra>>>1)))
2222 s = a.sign;
2223 e = a.exponent;
2224 m = a.mantissa;
2225 x = aExtra;
2226 } else {
2227 s = sign;
2228 e = exponent;
2229 m = mantissa;
2230 x = extra;
2231 sign = a.sign;
2232 exponent = a.exponent;
2233 mantissa = a.mantissa;
2234 extra = aExtra;
2236 int shift = exponent-e;
2237 if (shift>=127)
2238 return extra;
2239 if (shift>=64) {
2240 x = m>>>(shift-64);
2241 m = 0;
2242 } else if (shift>0) {
2243 x = (x>>>shift)+(m<<(64-shift));
2244 m >>>= shift;
2246 extra >>>= 1;
2247 x >>>= 1;
2248 if (sign == s) {
2249 extra += x;
2250 mantissa += (extra>>63)&1;
2251 mantissa += m;
2252 } else {
2253 extra -= x;
2254 mantissa -= (extra>>63)&1;
2255 mantissa -= m;
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)
2259 extra = 0;
2261 extra <<= 1;
2262 extra = normalize128(extra);
2263 if ((this.exponent == 0 && this.mantissa == 0))
2264 sign=0;
2265 return extra;
2267 /**
2268 * Calculates the difference between this <code>Real</code> and
2269 * <code>a</code>.
2270 * Replaces the contents of this <code>Real</code> with the result.
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2280 * <code>this -= a;</code>
2281 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2282 * ½ ULPs
2283 * </td></tr><tr><td><i>
2284 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2285 * </i></td><td>
2286 * 2.0
2287 * </td></tr></table>
2289 * @param a the <code>Real</code> to subtract from this.
2290 */
2291 public void sub(Real a) {
2292 tmp0.mantissa = a.mantissa;
2293 tmp0.exponent = a.exponent;
2294 tmp0.sign = (byte)(a.sign^1);
2295 add(tmp0);
2297 /**
2298 * Calculates the difference between this <code>Real</code> and the
2299 * integer <code>a</code>.
2300 * Replaces the contents of this <code>Real</code> with the result.
2302 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2303 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2304 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2305 * <code>this -= a;</code>
2306 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2307 * ½ ULPs
2308 * </td></tr><tr><td><i>
2309 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2310 * </i></td><td>
2311 * 2.4
2312 * </td></tr></table>
2314 * @param a the <code>int</code> to subtract from this.
2315 */
2316 public void sub(int a) {
2317 tmp0.assign(a);
2318 sub(tmp0);
2320 /**
2321 * Calculates the product of this <code>Real</code> and <code>a</code>.
2322 * Replaces the contents of this <code>Real</code> with the result.
2324 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2325 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2326 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2327 * <code>this *= a;</code>
2328 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2329 * ½ ULPs
2330 * </td></tr><tr><td><i>
2331 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2332 * </i></td><td>
2333 * 1.3
2334 * </td></tr></table>
2336 * @param a the <code>Real</code> to multiply to this.
2337 */
2338 public void mul(Real a) {
2339 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
2340 makeNan();
2341 return;
2343 sign ^= a.sign;
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))
2346 makeNan();
2347 else
2348 makeZero(sign);
2349 return;
2351 if ((this.exponent < 0 && this.mantissa == 0) || (a.exponent < 0 && a.mantissa == 0)) {
2352 makeInfinity(sign);
2353 return;
2355 long a0 = mantissa & 0x7fffffff;
2356 long a1 = mantissa >>> 31;
2357 long b0 = a.mantissa & 0x7fffffff;
2358 long b1 = a.mantissa >>> 31;
2359 mantissa = a1*b1;
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;
2365 if (exponent < 0) {
2366 if (exponent == -1 && aExp < 0x40000000 && mantissa < 0) {
2367 // Not underflow after all, it will be corrected in the
2368 // normalization below
2369 } else {
2370 if (aExp < 0x40000000)
2371 makeZero(sign); // Underflow
2372 else
2373 makeInfinity(sign); // Overflow
2374 return;
2377 // Simplified normalize()
2378 if (mantissa < 0) {
2379 mantissa = (mantissa+1)>>>1;
2380 exponent ++;
2381 if (exponent < 0) // Overflow
2382 makeInfinity(sign);
2385 /**
2386 * Calculates the product of this <code>Real</code> and the integer
2387 * <code>a</code>.
2388 * Replaces the contents of this <code>Real</code> with the result.
2390 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2391 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2392 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2393 * <code>this *= a;</code>
2394 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2395 * ½ ULPs
2396 * </td></tr><tr><td><i>
2397 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2398 * </i></td><td>
2399 * 1.3
2400 * </td></tr></table>
2402 * @param a the <code>int</code> to multiply to this.
2403 */
2404 public void mul(int a) {
2405 if ((this.exponent < 0 && this.mantissa != 0))
2406 return;
2407 if (a<0) {
2408 sign ^= 1;
2409 a = -a;
2411 if ((this.exponent == 0 && this.mantissa == 0) || a==0) {
2412 if ((this.exponent < 0 && this.mantissa == 0))
2413 makeNan();
2414 else
2415 makeZero(sign);
2416 return;
2418 if ((this.exponent < 0 && this.mantissa == 0))
2419 return;
2420 // Normalize int
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];
2423 exponent += 0x1F-t;
2424 a <<= t;
2425 if (exponent < 0) {
2426 makeInfinity(sign); // Overflow
2427 return;
2429 long a0 = mantissa & 0x7fffffff;
2430 long a1 = mantissa >>> 31;
2431 long b0 = a & 0xffffffffL;
2432 mantissa = a1*b0;
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()
2437 if (mantissa < 0) {
2438 mantissa = (mantissa+1)>>>1;
2439 exponent ++;
2440 if (exponent < 0) // Overflow
2441 makeInfinity(sign);
2444 /**
2445 * Calculates the product of this <code>Real</code> and <code>a</code> with
2446 * extended precision.
2447 * Replaces the contents of this <code>Real</code> with the result.
2448 * Returns the extra mantissa of the extended precision result.
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2458 * <code>this *= a;</code>
2459 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
2460 * 2<sup>-60</sup> ULPs
2461 * </td></tr><tr><td><i>
2462 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2463 * </i></td><td>
2464 * 3.1
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>.
2474 */
2475 public long mul128(long extra, Real a, long aExtra) {
2476 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
2477 makeNan();
2478 return 0;
2480 sign ^= a.sign;
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))
2483 makeNan();
2484 else
2485 makeZero(sign);
2486 return 0;
2488 if ((this.exponent < 0 && this.mantissa == 0) || (a.exponent < 0 && a.mantissa == 0)) {
2489 makeInfinity(sign);
2490 return 0;
2492 int aExp = a.exponent;
2493 exponent += aExp-0x40000000;
2494 if (exponent < 0) {
2495 if (aExp < 0x40000000)
2496 makeZero(sign); // Underflow
2497 else
2498 makeInfinity(sign); // Overflow
2499 return 0;
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;
2510 a0 = ((a3*b0>>>2)+
2511 (a2*b1>>>2)+
2512 (a1*b2>>>2)+
2513 (a0*b3>>>2)+
2514 0x60000000)>>>28;
2515 //(a2*b0>>>34)+(a1*b1>>>34)+(a0*b2>>>34)+0x08000000)>>>28;
2516 a1 *= b3;
2517 b0 = a2*b2;
2518 b1 *= a3;
2519 a0 += ((a1<<2)&ffffffffL) + ((b0<<2)&ffffffffL) + ((b1<<2)&ffffffffL);
2520 a1 = (a0>>>32) + (a1>>>30) + (b0>>>30) + (b1>>>30);
2521 a0 &= ffffffffL;
2522 a2 *= b3;
2523 b2 *= a3;
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);
2528 return extra;
2530 private void mul10() {
2531 if (!(this.exponent >= 0 && this.mantissa != 0))
2532 return;
2533 mantissa += (mantissa+2)>>>2;
2534 exponent += 3;
2535 if (mantissa < 0) {
2536 mantissa = (mantissa+1)>>>1;
2537 exponent++;
2539 if (exponent < 0)
2540 makeInfinity(sign); // Overflow
2542 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2549 * <code>this = this*this;</code>
2550 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2551 * ½ ULPs
2552 * </td></tr><tr><td><i>
2553 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2554 * </i></td><td>
2555 * 1.1
2556 * </td></tr></table>
2557 */
2558 public void sqr() {
2559 sign = 0;
2560 if (!(this.exponent >= 0 && this.mantissa != 0))
2561 return;
2562 int e = exponent;
2563 exponent += exponent-0x40000000;
2564 if (exponent < 0) {
2565 if (e < 0x40000000)
2566 makeZero(sign); // Underflow
2567 else
2568 makeInfinity(sign); // Overflow
2569 return;
2571 long a0 = mantissa&0x7fffffff;
2572 long a1 = mantissa>>>31;
2573 mantissa = a1*a1;
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()
2578 if (mantissa < 0) {
2579 mantissa = (mantissa+1)>>>1;
2580 exponent ++;
2581 if (exponent < 0) // Overflow
2582 makeInfinity(sign);
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)
2596 // Preparations
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
2604 q = next21;
2605 aHi24 = (int)(a>>32)>>>7;
2606 a <<= 21;
2607 // Two more almost identical blocks...
2608 next21 = (int)(((long)aHi24*(long)bInv24)>>>26);
2609 a -= next21*b;
2610 q = (q<<21)+next21;
2611 aHi24 = (int)(a>>32)>>>7;
2612 a <<= 21;
2613 next21 = (int)(((long)aHi24*(long)bInv24)>>>26);
2614 a -= next21*b;
2615 q = (q<<21)+next21;
2616 // Remove final remainder
2617 if (a<0 || a>=b) { q++; a -= b; }
2618 a <<= 1;
2619 // Round correctly
2620 if (a<0 || a>=b) q++;
2621 return q;
2623 /**
2624 * Calculates the quotient of this <code>Real</code> and <code>a</code>.
2625 * Replaces the contents of this <code>Real</code> with the result.
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2635 * <code>this /= a;</code>
2636 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2637 * ½ ULPs
2638 * </td></tr><tr><td><i>
2639 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2640 * </i></td><td>
2641 * 2.6
2642 * </td></tr></table>
2644 * @param a the <code>Real</code> to divide this with.
2645 */
2646 public void div(Real a) {
2647 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
2648 makeNan();
2649 return;
2651 sign ^= a.sign;
2652 if ((this.exponent < 0 && this.mantissa == 0)) {
2653 if ((a.exponent < 0 && a.mantissa == 0))
2654 makeNan();
2655 return;
2657 if ((a.exponent < 0 && a.mantissa == 0)) {
2658 makeZero(sign);
2659 return;
2661 if ((this.exponent == 0 && this.mantissa == 0)) {
2662 if ((a.exponent == 0 && a.mantissa == 0))
2663 makeNan();
2664 return;
2666 if ((a.exponent == 0 && a.mantissa == 0)) {
2667 makeInfinity(sign);
2668 return;
2670 exponent += 0x40000000-a.exponent;
2671 if (mantissa < a.mantissa) {
2672 mantissa <<= 1;
2673 exponent--;
2675 if (exponent < 0) {
2676 if (a.exponent >= 0x40000000)
2677 makeZero(sign); // Underflow
2678 else
2679 makeInfinity(sign); // Overflow
2680 return;
2682 if (a.mantissa == 0x4000000000000000L)
2683 return;
2684 mantissa = ldiv(mantissa,a.mantissa);
2686 /**
2687 * Calculates the quotient of this <code>Real</code> and the integer
2688 * <code>a</code>.
2689 * Replaces the contents of this <code>Real</code> with the result.
2691 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2692 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2693 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2694 * <code>this /= a;</code>
2695 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2696 * ½ ULPs
2697 * </td></tr><tr><td><i>
2698 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2699 * </i></td><td>
2700 * 2.6
2701 * </td></tr></table>
2703 * @param a the <code>int</code> to divide this with.
2704 */
2705 public void div(int a) {
2706 if ((this.exponent < 0 && this.mantissa != 0))
2707 return;
2708 if (a<0) {
2709 sign ^= 1;
2710 a = -a;
2712 if ((this.exponent < 0 && this.mantissa == 0))
2713 return;
2714 if ((this.exponent == 0 && this.mantissa == 0)) {
2715 if (a==0)
2716 makeNan();
2717 return;
2719 if (a==0) {
2720 makeInfinity(sign);
2721 return;
2723 long denom = a & 0xffffffffL;
2724 long remainder = mantissa%denom;
2725 mantissa /= denom;
2726 // Normalizing mantissa and scaling remainder accordingly
2727 int clz = 0;
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;
2732 mantissa <<= clz;
2733 remainder <<= clz;
2734 exponent -= clz;
2735 // Final division, correctly rounded
2736 remainder = (remainder+denom/2)/denom;
2737 mantissa += remainder;
2738 if (exponent < 0) // Underflow
2739 makeZero(sign);
2741 /**
2742 * Calculates the quotient of <code>a</code> and this <code>Real</code>.
2743 * Replaces the contents of this <code>Real</code> with the result.
2745 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2746 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2747 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2748 * <code>this = a/this;</code>
2749 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2750 * ½ ULPs
2751 * </td></tr><tr><td><i>
2752 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2753 * </i></td><td>
2754 * 3.1
2755 * </td></tr></table>
2757 * @param a the <code>Real</code> to be divided by this.
2758 */
2759 public void rdiv(Real a) {
2760 { recipTmp.mantissa = a.mantissa; recipTmp.exponent = a.exponent; recipTmp.sign = a.sign; };
2761 recipTmp.div(this);
2762 { this.mantissa = recipTmp.mantissa; this.exponent = recipTmp.exponent; this.sign = recipTmp.sign; };
2764 /**
2765 * Calculates the quotient of the integer <code>a</code> and this
2766 * <code>Real</code>.
2767 * Replaces the contents of this <code>Real</code> with the result.
2769 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2770 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2771 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2772 * <code>this = a/this;</code>
2773 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2774 * ½ ULPs
2775 * </td></tr><tr><td><i>
2776 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2777 * </i></td><td>
2778 * 3.9
2779 * </td></tr></table>
2781 * @param a the <code>int</code> to be divided by this.
2782 */
2783 public void rdiv(int a) {
2784 tmp0.assign(a);
2785 rdiv(tmp0);
2787 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2794 * <code>this = 1/this;</code>
2795 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2796 * ½ ULPs
2797 * </td></tr><tr><td><i>
2798 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2799 * </i></td><td>
2800 * 2.3
2801 * </td></tr></table>
2802 */
2803 public void recip() {
2804 if ((this.exponent < 0 && this.mantissa != 0))
2805 return;
2806 if ((this.exponent < 0 && this.mantissa == 0)) {
2807 makeZero(sign);
2808 return;
2810 if ((this.exponent == 0 && this.mantissa == 0)) {
2811 makeInfinity(sign);
2812 return;
2814 exponent = 0x80000000-exponent;
2815 if (mantissa == 0x4000000000000000L) {
2816 if (exponent < 0)
2817 makeInfinity(sign); // Overflow
2818 return;
2820 exponent--;
2821 mantissa = ldiv(0x8000000000000000L,mantissa);
2823 /**
2824 * Calculates the reciprocal of this <code>Real</code> with
2825 * extended precision.
2826 * Replaces the contents of this <code>Real</code> with the result.
2827 * Returns the extra mantissa of the extended precision result.
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2836 * <code>this = 1/this;</code>
2837 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
2838 * 2<sup>-60</sup> ULPs
2839 * </td></tr><tr><td><i>
2840 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2841 * </i></td><td>
2842 * 17
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>.
2849 */
2850 public long recip128(long extra) {
2851 if ((this.exponent < 0 && this.mantissa != 0))
2852 return 0;
2853 if ((this.exponent < 0 && this.mantissa == 0)) {
2854 makeZero(sign);
2855 return 0;
2857 if ((this.exponent == 0 && this.mantissa == 0)) {
2858 makeInfinity(sign);
2859 return 0;
2861 byte s = sign;
2862 sign = 0;
2863 // Special case, simple power of 2
2864 if (mantissa == 0x4000000000000000L && extra == 0) {
2865 exponent = 0x80000000-exponent;
2866 if (exponent<0) // Overflow
2867 makeInfinity(s);
2868 return 0;
2870 // Normalize exponent
2871 int exp = 0x40000000-exponent;
2872 exponent = 0x40000000;
2873 // Save -A
2874 { recipTmp.mantissa = this.mantissa; recipTmp.exponent = this.exponent; recipTmp.sign = this.sign; };
2875 long recipTmpExtra = extra;
2876 recipTmp.neg();
2877 // First establish approximate result (actually 63 bit accurate)
2878 recip();
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);
2886 // Fix exponent
2887 scalbn(exp);
2888 // Fix sign
2889 if (!isNan())
2890 sign = s;
2891 return extra;
2893 /**
2894 * Calculates the mathematical integer that is less than or equal to
2895 * this <code>Real</code> divided by <code>a</code>.
2896 * Replaces the contents of this <code>Real</code> with the result.
2898 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2899 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2900 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
2901 * <code>this = Math.{@link Math#floor(double) floor}(this/a);</code>
2902 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
2903 * 0 ULPs
2904 * </td></tr><tr><td><i>
2905 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2906 * </i></td><td>
2907 * 22
2908 * </td></tr></table>
2910 * @param a the <code>Real</code> argument.
2911 */
2912 public void divf(Real a) {
2913 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
2914 makeNan();
2915 return;
2917 if ((this.exponent < 0 && this.mantissa == 0)) {
2918 if ((a.exponent < 0 && a.mantissa == 0))
2919 makeNan();
2920 return;
2922 if ((a.exponent < 0 && a.mantissa == 0)) {
2923 makeZero(sign);
2924 return;
2926 if ((this.exponent == 0 && this.mantissa == 0)) {
2927 if ((a.exponent == 0 && a.mantissa == 0))
2928 makeNan();
2929 return;
2931 if ((a.exponent == 0 && a.mantissa == 0)) {
2932 makeInfinity(sign);
2933 return;
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()
2943 mantissa++;
2944 normalize();
2946 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?
2955 return;
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"
2961 tmp0.mantissa++;
2962 tmp0.normalize();
2964 tmp0.floor();
2965 tmp0.neg(); // tmp0 == -floor(this/a)
2966 extra = tmp0.mul128(0,a,aExtra);
2967 extra = add128(0/*thisExtra*/,tmp0,extra);
2968 roundFrom128(extra);
2970 /**
2971 * Calculates the value of this <code>Real</code> modulo <code>a</code>.
2972 * Replaces the contents of this <code>Real</code> with the result.
2973 * The modulo in this case is defined as the remainder after subtracting
2974 * <code>a</code> multiplied by the mathematical integer that is less than
2975 * or equal to this <code>Real</code> divided by <code>a</code>.
2977 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
2978 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
2979 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;bound:</i></td><td>
2983 * 0 ULPs
2984 * </td></tr><tr><td><i>
2985 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
2986 * </i></td><td>
2987 * 27
2988 * </td></tr></table>
2990 * @param a the <code>Real</code> argument.
2991 */
2992 public void mod(Real a) {
2993 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
2994 makeNan();
2995 return;
2997 if ((this.exponent < 0 && this.mantissa == 0)) {
2998 makeNan();
2999 return;
3001 if ((this.exponent == 0 && this.mantissa == 0)) {
3002 if ((a.exponent == 0 && a.mantissa == 0))
3003 makeNan();
3004 else
3005 sign = a.sign;
3006 return;
3008 if ((a.exponent < 0 && a.mantissa == 0)) {
3009 if (sign != a.sign)
3010 makeInfinity(a.sign);
3011 return;
3013 if ((a.exponent == 0 && a.mantissa == 0)) {
3014 makeZero(a.sign);
3015 return;
3017 modInternal(a,0);
3019 /**
3020 * Calculates the logical <i>AND</i> of this <code>Real</code> and
3021 * <code>a</code>.
3022 * Replaces the contents of this <code>Real</code> with the result.
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&nbsp;</i><code>int</code><i>&nbsp;code:</i></td><td>
3050 * <code>this &= a;</code>
3051 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
3052 * 0 ULPs
3053 * </td></tr><tr><td><i>
3054 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3055 * </i></td><td>
3056 * 1.5
3057 * </td></tr></table>
3059 * @param a the <code>Real</code> argument
3060 */
3061 public void and(Real a) {
3062 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
3063 makeNan();
3064 return;
3066 if ((this.exponent == 0 && this.mantissa == 0) || (a.exponent == 0 && a.mantissa == 0)) {
3067 makeZero();
3068 return;
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)
3078 else
3079 makeZero();
3080 return;
3082 byte s;
3083 int e;
3084 long m;
3085 if (exponent >= a.exponent) {
3086 s = a.sign;
3087 e = a.exponent;
3088 m = a.mantissa;
3089 } else {
3090 s = sign;
3091 e = exponent;
3092 m = mantissa;
3093 sign = a.sign;
3094 exponent = a.exponent;
3095 mantissa = a.mantissa;
3097 int shift = exponent-e;
3098 if (shift>=64) {
3099 if (s == 0)
3100 makeZero(sign);
3101 return;
3103 if (s != 0)
3104 m = -m;
3105 if ((this.sign!=0))
3106 mantissa = -mantissa;
3107 mantissa &= m>>shift;
3108 sign = 0;
3109 if (mantissa < 0) {
3110 mantissa = -mantissa;
3111 sign = 1;
3113 normalize();
3115 /**
3116 * Calculates the logical <i>OR</i> of this <code>Real</code> and
3117 * <code>a</code>.
3118 * Replaces the contents of this <code>Real</code> with the result.
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&nbsp;</i><code>int</code><i>&nbsp;code:</i></td><td>
3128 * <code>this |= a;</code>
3129 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
3130 * 0 ULPs
3131 * </td></tr><tr><td><i>
3132 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3133 * </i></td><td>
3134 * 1.6
3135 * </td></tr></table>
3137 * @param a the <code>Real</code> argument
3138 */
3139 public void or(Real a) {
3140 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
3141 makeNan();
3142 return;
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; };
3147 return;
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; };
3154 } else
3155 makeInfinity(sign | a.sign);
3156 return;
3158 byte s;
3159 int e;
3160 long m;
3161 if (((this.sign!=0) && exponent <= a.exponent) ||
3162 ((a.sign==0) && exponent >= a.exponent))
3164 s = a.sign;
3165 e = a.exponent;
3166 m = a.mantissa;
3167 } else {
3168 s = sign;
3169 e = exponent;
3170 m = mantissa;
3171 sign = a.sign;
3172 exponent = a.exponent;
3173 mantissa = a.mantissa;
3175 int shift = exponent-e;
3176 if (shift>=64 || shift<=-64)
3177 return;
3178 if (s != 0)
3179 m = -m;
3180 if ((this.sign!=0))
3181 mantissa = -mantissa;
3182 if (shift>=0)
3183 mantissa |= m>>shift;
3184 else
3185 mantissa |= m<<(-shift);
3186 sign = 0;
3187 if (mantissa < 0) {
3188 mantissa = -mantissa;
3189 sign = 1;
3191 normalize();
3193 /**
3194 * Calculates the logical <i>XOR</i> of this <code>Real</code> and
3195 * <code>a</code>.
3196 * Replaces the contents of this <code>Real</code> with the result.
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&nbsp;</i><code>int</code><i>&nbsp;code:</i></td><td>
3213 * <code>this ^= a;</code>
3214 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
3215 * 0 ULPs
3216 * </td></tr><tr><td><i>
3217 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3218 * </i></td><td>
3219 * 1.5
3220 * </td></tr></table>
3222 * @param a the <code>Real</code> argument
3223 */
3224 public void xor(Real a) {
3225 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
3226 makeNan();
3227 return;
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; };
3232 return;
3234 if ((this.exponent < 0 && this.mantissa == 0) || (a.exponent < 0 && a.mantissa == 0)) {
3235 makeInfinity(sign ^ a.sign);
3236 return;
3238 byte s;
3239 int e;
3240 long m;
3241 if (exponent >= a.exponent) {
3242 s = a.sign;
3243 e = a.exponent;
3244 m = a.mantissa;
3245 } else {
3246 s = sign;
3247 e = exponent;
3248 m = mantissa;
3249 sign = a.sign;
3250 exponent = a.exponent;
3251 mantissa = a.mantissa;
3253 int shift = exponent-e;
3254 if (shift>=64)
3255 return;
3256 if (s != 0)
3257 m = -m;
3258 if ((this.sign!=0))
3259 mantissa = -mantissa;
3260 mantissa ^= m>>shift;
3261 sign = 0;
3262 if (mantissa < 0) {
3263 mantissa = -mantissa;
3264 sign = 1;
3266 normalize();
3268 /**
3269 * Calculates the value of this <code>Real</code> <i>AND NOT</i>
3270 * <code>a</code>. The opeation is read as "bit clear".
3271 * Replaces the contents of this <code>Real</code> with the result.
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&nbsp;</i><code>int</code><i>&nbsp;code:</i></td><td>
3281 * <code>this &= ~a;</code>
3282 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
3283 * 0 ULPs
3284 * </td></tr><tr><td><i>
3285 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3286 * </i></td><td>
3287 * 1.5
3288 * </td></tr></table>
3290 * @param a the <code>Real</code> argument
3291 */
3292 public void bic(Real a) {
3293 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
3294 makeNan();
3295 return;
3297 if ((this.exponent == 0 && this.mantissa == 0) || (a.exponent == 0 && a.mantissa == 0))
3298 return;
3299 if ((this.exponent < 0 && this.mantissa == 0) || (a.exponent < 0 && a.mantissa == 0)) {
3300 if (!(this.exponent < 0 && this.mantissa == 0)) {
3301 if ((this.sign!=0))
3302 if ((a.sign!=0))
3303 makeInfinity(0);
3304 else
3305 makeInfinity(1);
3306 } else if ((a.sign!=0)) {
3307 if ((a.exponent < 0 && a.mantissa == 0))
3308 makeInfinity(0);
3309 else
3310 makeZero();
3312 return;
3314 int shift = exponent-a.exponent;
3315 if (shift>=64 || (shift<=-64 && (this.sign==0)))
3316 return;
3317 long m = a.mantissa;
3318 if ((a.sign!=0))
3319 m = -m;
3320 if ((this.sign!=0))
3321 mantissa = -mantissa;
3322 if (shift<0) {
3323 if ((this.sign!=0)) {
3324 if (shift<=-64)
3325 mantissa = ~m;
3326 else
3327 mantissa = (mantissa>>(-shift)) & ~m;
3328 exponent = a.exponent;
3329 } else
3330 mantissa &= ~(m<<(-shift));
3331 } else
3332 mantissa &= ~(m>>shift);
3333 sign = 0;
3334 if (mantissa < 0) {
3335 mantissa = -mantissa;
3336 sign = 1;
3338 normalize();
3340 private int compare(int a) {
3341 tmp0.assign(a);
3342 return compare(tmp0);
3344 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
3351 * <code>this = Math.{@link Math#sqrt(double) sqrt}(this);</code>
3352 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
3353 * 1 ULPs
3354 * </td></tr><tr><td><i>
3355 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3356 * </i></td><td>
3357 * 19
3358 * </td></tr></table>
3359 */
3360 public void sqrt() {
3361 /*
3362 * Adapted from:
3363 * Cephes Math Library Release 2.2: December, 1990
3364 * Copyright 1984, 1990 by Stephen L. Moshier
3366 * sqrtl.c
3368 * long double sqrtl(long double x);
3369 */
3370 if ((this.exponent < 0 && this.mantissa != 0))
3371 return;
3372 if ((this.exponent == 0 && this.mantissa == 0)) {
3373 sign=0;
3374 return;
3376 if ((this.sign!=0)) {
3377 makeNan();
3378 return;
3380 if ((this.exponent < 0 && this.mantissa == 0))
3381 return;
3382 // Save X
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
3390 mul(sqrtTmp);
3391 { sqrtTmp.sign=(byte)0; sqrtTmp.exponent=0x3fffffff; sqrtTmp.mantissa=0x71f1e120690deae8L; };//0.89019407351052789754347
3392 add(sqrtTmp);
3393 mul(recipTmp2);
3394 { sqrtTmp.sign=(byte)0; sqrtTmp.exponent=0x3ffffffe; sqrtTmp.mantissa=0x5045ee6baf28677aL; };//0.31356706742295303132394
3395 add(sqrtTmp);
3396 // adjust for odd powers of 2
3397 if ((e&1) != 0)
3398 mul(SQRT2);
3399 // calculate exponent
3400 exponent += e>>1;
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);
3406 add(recipTmp2);
3407 scalbn(-1);
3410 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
3417 * <code>this = 1/Math.{@link Math#sqrt(double) sqrt}(this);</code>
3418 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
3419 * 1 ULPs
3420 * </td></tr><tr><td><i>
3421 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3422 * </i></td><td>
3423 * 21
3424 * </td></tr></table>
3425 */
3426 public void rsqrt() {
3427 sqrt();
3428 recip();
3430 /**
3431 * Calculates the cube root of this <code>Real</code>.
3432 * Replaces the contents of this <code>Real</code> with the result.
3433 * The cube root of a negative value is the negative of the cube
3434 * root of that value's magnitude.
3436 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3437 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3438 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
3439 * <code>this = Math.{@link Math#cbrt(double) cbrt}(this);</code>
3440 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
3441 * 2 ULPs
3442 * </td></tr><tr><td><i>
3443 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3444 * </i></td><td>
3445 * 32
3446 * </td></tr></table>
3447 */
3448 public void cbrt() {
3449 if (!(this.exponent >= 0 && this.mantissa != 0))
3450 return;
3451 byte s = sign;
3452 sign = 0;
3453 // Calculates recipocal cube root of normalized Real,
3454 // not zero, nan or infinity
3455 final long start = 0x5120000000000000L;
3456 // Save -A
3457 { recipTmp.mantissa = this.mantissa; recipTmp.exponent = this.exponent; recipTmp.sign = this.sign; };
3458 recipTmp.neg();
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;
3463 normalize();
3464 if (expRmd>0) {
3465 { recipTmp2.sign=(byte)0; recipTmp2.exponent=0x3fffffff; recipTmp2.mantissa=0x6597fa94f5b8f20bL; }; // cbrt(1/2)
3466 mul(recipTmp2);
3467 if (expRmd>1)
3468 mul(recipTmp2);
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; };
3474 sqr();
3475 sqr();
3476 mul(recipTmp);
3477 recipTmp2.scalbn(2);
3478 add(recipTmp2);
3479 mul(THIRD);
3481 recip();
3482 if (!(this.exponent < 0 && this.mantissa != 0))
3483 sign = s;
3485 /**
3486 * Calculates the n'th root of this <code>Real</code>.
3487 * Replaces the contents of this <code>Real</code> with the result.
3488 * For odd integer n, the n'th root of a negative value is the
3489 * negative of the n'th root of that value's magnitude.
3491 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3492 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3493 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;error&nbsp;bound:</i></td><td>
3497 * 2 ULPs
3498 * </td></tr><tr><td><i>
3499 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3500 * </i></td><td>
3501 * 110
3502 * </td></tr></table>
3504 * @param n the <code>Real</code> argument.
3505 */
3506 public void nroot(Real n) {
3507 if ((n.exponent < 0 && n.mantissa != 0)) {
3508 makeNan();
3509 return;
3511 if (n.compare(THREE)==0) {
3512 cbrt(); // Most probable application of nroot...
3513 return;
3514 } else if (n.compare(TWO)==0) {
3515 sqrt(); // Also possible, should be optimized like this
3516 return;
3518 boolean negative = false;
3519 if ((this.sign!=0) && n.isIntegral() && n.isOdd()) {
3520 negative = true;
3521 abs();
3523 { tmp2.mantissa = n.mantissa; tmp2.exponent = n.exponent; tmp2.sign = n.sign; }; // Copy to temporary location in case of x.nroot(x)
3524 tmp2.recip();
3525 pow(tmp2);
3526 if (negative)
3527 neg();
3529 /**
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&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;error&nbsp;bound:</i></td><td>
3539 * 1 ULPs
3540 * </td></tr><tr><td><i>
3541 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3542 * </i></td><td>
3543 * 24
3544 * </td></tr></table>
3546 * @param a the <code>Real</code> argument.
3547 */
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)
3550 tmp1.sqr();
3551 sqr();
3552 add(tmp1);
3553 sqrt();
3555 private void exp2Internal(long extra) {
3556 if ((this.exponent < 0 && this.mantissa != 0))
3557 return;
3558 if ((this.exponent < 0 && this.mantissa == 0)) {
3559 if ((this.sign!=0))
3560 makeZero(0);
3561 return;
3563 if ((this.exponent == 0 && this.mantissa == 0)) {
3564 { this.mantissa = ONE.mantissa; this.exponent = ONE.exponent; this.sign = ONE.sign; };
3565 return;
3567 // Extract integer part
3568 { expTmp.mantissa = this.mantissa; expTmp.exponent = this.exponent; expTmp.sign = this.sign; };
3569 expTmp.add(HALF);
3570 expTmp.floor();
3571 int exp = expTmp.toInteger();
3572 if (exp > 0x40000000) {
3573 makeInfinity(sign);
3574 return;
3576 if (exp < -0x40000000) {
3577 makeZero(sign);
3578 return;
3580 // Subtract integer part (this is where we need the extra accuracy)
3581 expTmp.neg();
3582 add128(extra,expTmp,0);
3583 /*
3584 * Adapted from:
3585 * Cephes Math Library Release 2.7: May, 1998
3586 * Copyright 1984, 1991, 1998 by Stephen L. Moshier
3588 * exp2l.c
3590 * long double exp2l(long double x);
3591 */
3592 // Now -0.5<X<0.5
3593 // rational approximation
3594 // exp2(x) = 1 + 2x P(x²)/(Q(x²) - x P(x²))
3595 { expTmp2.mantissa = this.mantissa; expTmp2.exponent = this.exponent; expTmp2.sign = this.sign; };
3596 expTmp2.sqr();
3597 // P(x²)
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);
3605 mul(expTmp);
3606 // Q(x²)
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);
3616 expTmp.sub(this);
3617 div(expTmp);
3618 scalbn(1);
3619 add(ONE);
3620 // Scale by power of 2
3621 scalbn(exp);
3623 /**
3624 * Calculates <i>e</i> raised to the power of this <code>Real</code>.
3625 * Replaces the contents of this <code>Real</code> with the result.
3627 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3628 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3629 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
3630 * <code>this = Math.{@link Math#exp(double) exp}(this);</code>
3631 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
3632 * 1 ULPs
3633 * </td></tr><tr><td><i>
3634 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3635 * </i></td><td>
3636 * 31
3637 * </td></tr></table>
3638 */
3639 public void exp() {
3640 { expTmp.sign=(byte)0; expTmp.exponent=0x40000000; expTmp.mantissa=0x5c551d94ae0bf85dL; }; // log2(e)
3641 long extra = mul128(0,expTmp,0xdf43ff68348e9f44L);
3642 exp2Internal(extra);
3644 /**
3645 * Calculates 2 raised to the power of this <code>Real</code>.
3646 * Replaces the contents of this <code>Real</code> with the result.
3648 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3649 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3650 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;error&nbsp;bound:</i></td><td>
3654 * 1 ULPs
3655 * </td></tr><tr><td><i>
3656 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3657 * </i></td><td>
3658 * 27
3659 * </td></tr></table>
3660 */
3661 public void exp2() {
3662 exp2Internal(0);
3664 /**
3665 * Calculates 10 raised to the power of this <code>Real</code>.
3666 * Replaces the contents of this <code>Real</code> with the result.
3668 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3669 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3670 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;error&nbsp;bound:</i></td><td>
3674 * 1 ULPs
3675 * </td></tr><tr><td><i>
3676 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3677 * </i></td><td>
3678 * 31
3679 * </td></tr></table>
3680 */
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))
3689 return 0;
3690 if ((this.sign!=0)) {
3691 makeNan();
3692 return 0;
3694 if ((this.exponent == 0 && this.mantissa == 0)) {
3695 makeInfinity(1);
3696 return 0;
3698 if ((this.exponent < 0 && this.mantissa == 0))
3699 return 0;
3700 /*
3701 * Adapted from:
3702 * Cephes Math Library Release 2.7: May, 1998
3703 * Copyright 1984, 1990, 1998 by Stephen L. Moshier
3705 * logl.c
3707 * long double logl(long double x);
3708 */
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) {
3715 e--;
3716 exponent++;
3718 sub(ONE);
3719 { expTmp2.mantissa = this.mantissa; expTmp2.exponent = this.exponent; expTmp2.sign = this.sign; };
3720 // P(x)
3721 { this.sign=(byte)0; this.exponent=0x3ffffff1; this.mantissa=0x5ef0258ace5728ddL; };//4.5270000862445199635215E-5
3722 mul(expTmp2);
3723 { expTmp3.sign=(byte)0; expTmp3.exponent=0x3ffffffe; expTmp3.mantissa=0x7fa06283f86a0ce8L; };//0.4985410282319337597221
3724 add(expTmp3);
3725 mul(expTmp2);
3726 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000002; expTmp3.mantissa=0x69427d1bd3e94ca1L; };//6.5787325942061044846969
3727 add(expTmp3);
3728 mul(expTmp2);
3729 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000004; expTmp3.mantissa=0x77a5ce2e32e7256eL; };//29.911919328553073277375
3730 add(expTmp3);
3731 mul(expTmp2);
3732 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000005; expTmp3.mantissa=0x79e63ae1b0cd4222L; };//60.949667980987787057556
3733 add(expTmp3);
3734 mul(expTmp2);
3735 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000005; expTmp3.mantissa=0x7239d65d1e6840d6L; };//57.112963590585538103336
3736 add(expTmp3);
3737 mul(expTmp2);
3738 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000004; expTmp3.mantissa=0x502880b6660c265fL; };//20.039553499201281259648
3739 add(expTmp3);
3740 // Q(x)
3741 { expTmp.mantissa = expTmp2.mantissa; expTmp.exponent = expTmp2.exponent; expTmp.sign = expTmp2.sign; };
3742 { expTmp3.sign=(byte)0; expTmp3.exponent=0x40000003; expTmp3.mantissa=0x7880d67a40f8dc5cL; };//15.062909083469192043167
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);
3759 div(expTmp);
3760 { expTmp3.mantissa = expTmp2.mantissa; expTmp3.exponent = expTmp2.exponent; expTmp3.sign = expTmp2.sign; };
3761 expTmp3.sqr();
3762 mul(expTmp3);
3763 mul(expTmp2);
3764 expTmp3.scalbn(-1);
3765 sub(expTmp3);
3766 add(expTmp2);
3767 return e;
3769 /**
3770 * Calculates the natural logarithm (base-<i>e</i>) of this
3771 * <code>Real</code>.
3772 * Replaces the contents of this <code>Real</code> with the result.
3774 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3775 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3776 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
3777 * <code>this = Math.{@link Math#log(double) log}(this);</code>
3778 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
3779 * 2 ULPs
3780 * </td></tr><tr><td><i>
3781 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3782 * </i></td><td>
3783 * 51
3784 * </td></tr></table>
3785 */
3786 public void ln() {
3787 int exp = lnInternal();
3788 expTmp.assign(exp);
3789 expTmp.mul(LN2);
3790 add(expTmp);
3792 /**
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&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;error&nbsp;bound:</i></td><td>
3802 * 1 ULPs
3803 * </td></tr><tr><td><i>
3804 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3805 * </i></td><td>
3806 * 51
3807 * </td></tr></table>
3808 */
3809 public void log2() {
3810 int exp = lnInternal();
3811 mul(LOG2E);
3812 add(exp);
3814 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
3821 * <code>this = Math.{@link Math#log10(double) log10}(this);</code>
3822 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
3823 * 2 ULPs
3824 * </td></tr><tr><td><i>
3825 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3826 * </i></td><td>
3827 * 53
3828 * </td></tr></table>
3829 */
3830 public void log10() {
3831 int exp = lnInternal();
3832 expTmp.assign(exp);
3833 expTmp.mul(LN2);
3834 add(expTmp);
3835 mul(LOG10E);
3837 /**
3838 * Calculates the closest power of 10 that is less than or equal to this
3839 * <code>Real</code>.
3840 * Replaces the contents of this <code>Real</code> with the result.
3841 * The base-10 exponent of the result is returned.
3843 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3844 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3845 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;bound:</i></td><td>
3851 * ½ ULPs
3852 * </td></tr><tr><td><i>
3853 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3854 * </i></td><td>
3855 * 3.6
3856 * </td></tr></table>
3858 * @return the base-10 exponent
3859 */
3860 public int lowPow10() {
3861 if (!(this.exponent >= 0 && this.mantissa != 0))
3862 return 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);
3868 else
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; };
3872 pow(e);
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; };
3875 tmp3.pow(e+1);
3876 } else {
3877 { tmp3.mantissa = this.mantissa; tmp3.exponent = this.exponent; tmp3.sign = this.sign; };
3878 tmp3.mul10();
3880 if (tmp3.compare(tmp2) <= 0) {
3881 // First estimate of log10 was too low
3882 e++;
3883 { this.mantissa = tmp3.mantissa; this.exponent = tmp3.exponent; this.sign = tmp3.sign; };
3885 return e;
3887 /**
3888 * Calculates the value of this <code>Real</code> raised to the power of
3889 * <code>a</code>.
3890 * Replaces the contents of this <code>Real</code> with the result.
3892 * <p> Special cases:
3893 * <ul>
3894 * <li> if a is 0.0 or -0.0 then result is 1.0
3895 * <li> if a is NaN then result is NaN
3896 * <li> if this is NaN and a is not zero then result is NaN
3897 * <li> if a is 1.0 then result is this
3898 * <li> if |this| > 1.0 and a is +Infinity then result is +Infinity
3899 * <li> if |this| < 1.0 and a is -Infinity then result is +Infinity
3900 * <li> if |this| > 1.0 and a is -Infinity then result is +0
3901 * <li> if |this| < 1.0 and a is +Infinity then result is +0
3902 * <li> if |this| = 1.0 and a is ±Infinity then result is NaN
3903 * <li> if this = +0 and a > 0 then result is +0
3904 * <li> if this = +0 and a < 0 then result is +Inf
3905 * <li> if this = -0 and a > 0, and odd integer then result is -0
3906 * <li> if this = -0 and a < 0, and odd integer then result is -Inf
3907 * <li> if this = -0 and a > 0, not odd integer then result is +0
3908 * <li> if this = -0 and a < 0, not odd integer then result is +Inf
3909 * <li> if this = +Inf and a > 0 then result is +Inf
3910 * <li> if this = +Inf and a < 0 then result is +0
3911 * <li> if this = -Inf and a not integer then result is NaN
3912 * <li> if this = -Inf and a > 0, and odd integer then result is -Inf
3913 * <li> if this = -Inf and a > 0, not odd integer then result is +Inf
3914 * <li> if this = -Inf and a < 0, and odd integer then result is -0
3915 * <li> if this = -Inf and a < 0, not odd integer then result is +0
3916 * <li> if this < 0 and a not integer then result is NaN
3917 * <li> if this < 0 and a odd integer then result is -(|this|<sup>a</sup>)
3918 * <li> if this < 0 and a not odd integer then result is |this|<sup>a</sup>
3919 * <li> else result is exp(ln(this)*a)
3920 * </ul>
3922 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
3923 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
3924 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
3925 * <code>this = Math.{@link Math#pow(double,double) pow}(this, a);</code>
3926 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
3927 * 2 ULPs
3928 * </td></tr><tr><td><i>
3929 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
3930 * </i></td><td>
3931 * 110
3932 * </td></tr></table>
3934 * @param a the <code>Real</code> argument.
3935 */
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; };
3939 return;
3941 if ((this.exponent < 0 && this.mantissa != 0) || (a.exponent < 0 && a.mantissa != 0)) {
3942 makeNan();
3943 return;
3945 if (a.compare(ONE)==0)
3946 return;
3947 if ((a.exponent < 0 && a.mantissa == 0)) {
3948 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
3949 tmp1.abs();
3950 int test = tmp1.compare(ONE);
3951 if (test>0) {
3952 if ((a.sign==0))
3953 makeInfinity(0);
3954 else
3955 makeZero();
3956 } else if (test<0) {
3957 if ((a.sign!=0))
3958 makeInfinity(0);
3959 else
3960 makeZero();
3961 } else {
3962 makeNan();
3964 return;
3966 if ((this.exponent == 0 && this.mantissa == 0)) {
3967 if ((this.sign==0)) {
3968 if ((a.sign==0))
3969 makeZero();
3970 else
3971 makeInfinity(0);
3972 } else {
3973 if (a.isIntegral() && a.isOdd()) {
3974 if ((a.sign==0))
3975 makeZero(1);
3976 else
3977 makeInfinity(1);
3978 } else {
3979 if ((a.sign==0))
3980 makeZero();
3981 else
3982 makeInfinity(0);
3985 return;
3987 if ((this.exponent < 0 && this.mantissa == 0)) {
3988 if ((this.sign==0)) {
3989 if ((a.sign==0))
3990 makeInfinity(0);
3991 else
3992 makeZero();
3993 } else {
3994 if (a.isIntegral()) {
3995 if (a.isOdd()) {
3996 if ((a.sign==0))
3997 makeInfinity(1);
3998 else
3999 makeZero(1);
4000 } else {
4001 if ((a.sign==0))
4002 makeInfinity(0);
4003 else
4004 makeZero();
4006 } else {
4007 makeNan();
4010 return;
4012 if (a.isIntegral() && a.exponent <= 0x4000001e) {
4013 pow(a.toInteger());
4014 return;
4016 byte s=0;
4017 if ((this.sign!=0)) {
4018 if (a.isIntegral()) {
4019 if (a.isOdd())
4020 s = 1;
4021 } else {
4022 makeNan();
4023 return;
4025 sign = 0;
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; };
4033 tmp2.floor();
4034 { tmp3.mantissa = this.mantissa; tmp3.exponent = this.exponent; tmp3.sign = this.sign; };
4035 tmp3.pow(tmp2.toInteger());
4036 tmp1.sub(tmp2);
4037 } else {
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);
4044 tmp2.assign(e);
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; };
4050 mul(tmp3);
4051 if (!(this.exponent < 0 && this.mantissa != 0))
4052 sign = s;
4054 /**
4055 * Calculates the value of this <code>Real</code> raised to the power of
4056 * the integer <code>a</code>.
4057 * Replaces the contents of this <code>Real</code> with the result.
4059 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4060 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4061 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4062 * <code>this = Math.{@link Math#pow(double,double) pow}(this, a);</code>
4063 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
4064 * ½ ULPs
4065 * </td></tr><tr><td><i>
4066 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4067 * </i></td><td>
4068 * 84
4069 * </td></tr></table>
4071 * @param a the integer argument.
4072 */
4073 public void pow(int a) {
4074 // Calculate power of integer by successive squaring
4075 boolean recp=false;
4076 if (a < 0) {
4077 a = -a; // Also works for 0x80000000
4078 recp = true;
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) {
4084 if ((a & 1) != 0)
4085 extra = mul128(extra,expTmp,expTmpExtra);
4086 expTmpExtra = expTmp.mul128(expTmpExtra,expTmp,expTmpExtra);
4088 if (recp)
4089 extra = recip128(extra);
4090 roundFrom128(extra);
4092 private void sinInternal() {
4093 /*
4094 * Adapted from:
4095 * Cephes Math Library Release 2.7: May, 1998
4096 * Copyright 1985, 1990, 1998 by Stephen L. Moshier
4098 * sinl.c
4100 * long double sinl(long double x);
4101 */
4102 // X<PI/4
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; };
4107 tmp2.sqr();
4108 { this.sign=(byte)1; this.exponent=0x3fffffd7; this.mantissa=0x6aa891c4f0eb2713L; };//-7.578540409484280575629E-13
4109 mul(tmp2);
4110 { tmp3.sign=(byte)0; tmp3.exponent=0x3fffffdf; tmp3.mantissa=0x58482311f383326cL; };//1.6058363167320443249231E-10
4111 add(tmp3);
4112 mul(tmp2);
4113 { tmp3.sign=(byte)1; tmp3.exponent=0x3fffffe6; tmp3.mantissa=0x6b9914a35f9a00d8L; };//-2.5052104881870868784055E-8
4114 add(tmp3);
4115 mul(tmp2);
4116 { tmp3.sign=(byte)0; tmp3.exponent=0x3fffffed; tmp3.mantissa=0x5c778e94cc22e47bL; };//2.7557319214064922217861E-6
4117 add(tmp3);
4118 mul(tmp2);
4119 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffff3; tmp3.mantissa=0x680680680629b28aL; };//-1.9841269841254799668344E-4
4120 add(tmp3);
4121 mul(tmp2);
4122 { tmp3.sign=(byte)0; tmp3.exponent=0x3ffffff9; tmp3.mantissa=0x4444444444442b4dL; };//8.3333333333333225058715E-3
4123 add(tmp3);
4124 mul(tmp2);
4125 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffffd; tmp3.mantissa=0x555555555555554cL; };//-1.6666666666666666640255E-1
4126 add(tmp3);
4127 mul(tmp2);
4128 mul(tmp1);
4129 add(tmp1);
4131 private void cosInternal() {
4132 /*
4133 * Adapted from:
4134 * Cephes Math Library Release 2.7: May, 1998
4135 * Copyright 1985, 1990, 1998 by Stephen L. Moshier
4137 * sinl.c
4139 * long double cosl(long double x);
4140 */
4141 // X<PI/4
4142 // polynomial approximation
4143 // cos(x) = 1 - x²/2 + x**4 Q(x²)
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; };
4146 tmp2.sqr();
4147 { this.sign=(byte)0; this.exponent=0x3fffffd3; this.mantissa=0x6aaf461d37ccba1bL; };//4.7377507964246204691685E-14
4148 mul(tmp2);
4149 { tmp3.sign=(byte)1; tmp3.exponent=0x3fffffdb; tmp3.mantissa=0x64e4c907ac7a179bL; };//-1.147028484342535976567E-11
4150 add(tmp3);
4151 mul(tmp2);
4152 { tmp3.sign=(byte)0; tmp3.exponent=0x3fffffe3; tmp3.mantissa=0x47bb632432cf29a8L; };//2.0876754287081521758361E-9
4153 add(tmp3);
4154 mul(tmp2);
4155 { tmp3.sign=(byte)1; tmp3.exponent=0x3fffffea; tmp3.mantissa=0x49f93edd7ae32696L; };//-2.7557319214999787979814E-7
4156 add(tmp3);
4157 mul(tmp2);
4158 { tmp3.sign=(byte)0; tmp3.exponent=0x3ffffff0; tmp3.mantissa=0x68068068063329f7L; };//2.4801587301570552304991E-5L
4159 add(tmp3);
4160 mul(tmp2);
4161 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffff6; tmp3.mantissa=0x5b05b05b05b03db3L; };//-1.3888888888888872993737E-3
4162 add(tmp3);
4163 mul(tmp2);
4164 { tmp3.sign=(byte)0; tmp3.exponent=0x3ffffffb; tmp3.mantissa=0x555555555555554dL; };//4.1666666666666666609054E-2
4165 add(tmp3);
4166 mul(tmp2);
4167 sub(HALF);
4168 mul(tmp2);
4169 add(ONE);
4171 /**
4172 * Calculates the trigonometric sine of this <code>Real</code>.
4173 * Replaces the contents of this <code>Real</code> with the result.
4174 * The input value is treated as an angle measured in radians.
4176 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4177 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4178 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4179 * <code>this = Math.{@link Math#sin(double) sin}(this);</code>
4180 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4181 * 1 ULPs
4182 * </td></tr><tr><td><i>
4183 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4184 * </i></td><td>
4185 * 28
4186 * </td></tr></table>
4187 */
4188 public void sin() {
4189 if (!(this.exponent >= 0 && this.mantissa != 0)) {
4190 if (!(this.exponent == 0 && this.mantissa == 0))
4191 makeNan();
4192 return;
4194 // Since sin(-x) = -sin(x) we can make sure that x > 0
4195 boolean negative = false;
4196 if ((this.sign!=0)) {
4197 abs();
4198 negative = true;
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) {
4205 sub(PI2);
4206 neg();
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) {
4211 sub(PI);
4212 neg();
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) {
4216 sub(PI_2);
4217 neg();
4218 cosInternal();
4219 } else {
4220 sinInternal();
4222 if (negative)
4223 neg();
4224 if ((this.exponent == 0 && this.mantissa == 0))
4225 abs(); // Remove confusing "-"
4227 /**
4228 * Calculates the trigonometric cosine of this <code>Real</code>.
4229 * Replaces the contents of this <code>Real</code> with the result.
4230 * The input value is treated as an angle measured in radians.
4232 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4233 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4234 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4235 * <code>this = Math.{@link Math#cos(double) cos}(this);</code>
4236 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4237 * 1 ULPs
4238 * </td></tr><tr><td><i>
4239 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4240 * </i></td><td>
4241 * 37
4242 * </td></tr></table>
4243 */
4244 public void cos() {
4245 if ((this.exponent == 0 && this.mantissa == 0)) {
4246 { this.mantissa = ONE.mantissa; this.exponent = ONE.exponent; this.sign = ONE.sign; };
4247 return;
4249 if ((this.sign!=0))
4250 abs();
4251 if (this.compare(PI_4) < 0) {
4252 cosInternal();
4253 } else {
4254 add(PI_2);
4255 sin();
4258 /**
4259 * Calculates the trigonometric tangent of this <code>Real</code>.
4260 * Replaces the contents of this <code>Real</code> with the result.
4261 * The input value is treated as an angle measured in radians.
4263 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4264 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4265 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4266 * <code>this = Math.{@link Math#tan(double) tan}(this);</code>
4267 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4268 * 2 ULPs
4269 * </td></tr><tr><td><i>
4270 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4271 * </i></td><td>
4272 * 70
4273 * </td></tr></table>
4274 */
4275 public void tan() {
4276 { tmp4.mantissa = this.mantissa; tmp4.exponent = this.exponent; tmp4.sign = this.sign; };
4277 tmp4.cos();
4278 sin();
4279 div(tmp4);
4281 /**
4282 * Calculates the trigonometric arc sine of this <code>Real</code>,
4283 * in the range -&pi;/2 to &pi;/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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4289 * <code>this = Math.{@link Math#asin(double) asin}(this);</code>
4290 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4291 * 3 ULPs
4292 * </td></tr><tr><td><i>
4293 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4294 * </i></td><td>
4295 * 68
4296 * </td></tr></table>
4297 */
4298 public void asin() {
4299 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4300 sqr();
4301 neg();
4302 add(ONE);
4303 rsqrt();
4304 mul(tmp1);
4305 atan();
4307 /**
4308 * Calculates the trigonometric arc cosine of this <code>Real</code>,
4309 * in the range 0.0 to &pi;.
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4315 * <code>this = Math.{@link Math#acos(double) acos}(this);</code>
4316 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4317 * 2 ULPs
4318 * </td></tr><tr><td><i>
4319 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4320 * </i></td><td>
4321 * 67
4322 * </td></tr></table>
4323 */
4324 public void acos() {
4325 boolean negative = (this.sign!=0);
4326 abs();
4327 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4328 sqr();
4329 neg();
4330 add(ONE);
4331 sqrt();
4332 div(tmp1);
4333 atan();
4334 if (negative) {
4335 neg();
4336 add(PI);
4339 /**
4340 * Calculates the trigonometric arc tangent of this <code>Real</code>,
4341 * in the range -&pi;/2 to &pi;/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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4347 * <code>this = Math.{@link Math#atan(double) atan}(this);</code>
4348 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4349 * 2 ULPs
4350 * </td></tr><tr><td><i>
4351 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4352 * </i></td><td>
4353 * 37
4354 * </td></tr></table>
4355 */
4356 public void atan() {
4357 /*
4358 * Adapted from:
4359 * Cephes Math Library Release 2.7: May, 1998
4360 * Copyright 1984, 1990, 1998 by Stephen L. Moshier
4362 * atanl.c
4364 * long double atanl(long double x);
4365 */
4366 if ((this.exponent == 0 && this.mantissa == 0) || (this.exponent < 0 && this.mantissa != 0))
4367 return;
4368 if ((this.exponent < 0 && this.mantissa == 0)) {
4369 byte s = sign;
4370 { this.mantissa = PI_2.mantissa; this.exponent = PI_2.exponent; this.sign = PI_2.sign; };
4371 sign = s;
4372 return;
4374 byte s = sign;
4375 sign = 0;
4376 // range reduction
4377 boolean addPI_2 = false;
4378 boolean addPI_4 = false;
4379 { tmp1.mantissa = SQRT2.mantissa; tmp1.exponent = SQRT2.exponent; tmp1.sign = SQRT2.sign; };
4380 tmp1.add(ONE);
4381 if (this.compare(tmp1) > 0) {
4382 addPI_2 = true;
4383 recip();
4384 neg();
4385 } else {
4386 tmp1.sub(TWO);
4387 if (this.compare(tmp1) > 0) {
4388 addPI_4 = true;
4389 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4390 tmp1.add(ONE);
4391 sub(ONE);
4392 div(tmp1);
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; };
4400 tmp2.sqr();
4401 mul(tmp2);
4402 { tmp3.sign=(byte)1; tmp3.exponent=0x3fffffff; tmp3.mantissa=0x6f2f89336729c767L; };//-0.8686381817809218753544
4403 tmp3.mul(tmp2);
4404 { tmp4.sign=(byte)1; tmp4.exponent=0x40000003; tmp4.mantissa=0x7577d35fd03083f3L; };//-14.683508633175792446076
4405 tmp3.add(tmp4);
4406 tmp3.mul(tmp2);
4407 { tmp4.sign=(byte)1; tmp4.exponent=0x40000005; tmp4.mantissa=0x7ff42abff948a9f7L; };//-63.976888655834347413154
4408 tmp3.add(tmp4);
4409 tmp3.mul(tmp2);
4410 { tmp4.sign=(byte)1; tmp4.exponent=0x40000006; tmp4.mantissa=0x63fd1f9f76d37cebL; };//-99.988763777265819915721
4411 tmp3.add(tmp4);
4412 tmp3.mul(tmp2);
4413 { tmp4.sign=(byte)1; tmp4.exponent=0x40000005; tmp4.mantissa=0x65c9c9b0b55e5b62L; };//-50.894116899623603312185
4414 tmp3.add(tmp4);
4415 mul(tmp3);
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
4418 tmp3.add(tmp4);
4419 tmp3.mul(tmp2);
4420 { tmp4.sign=(byte)0; tmp4.exponent=0x40000007; tmp4.mantissa=0x47fed7d13d233b5cL; };//143.99096122250781605352
4421 tmp3.add(tmp4);
4422 tmp3.mul(tmp2);
4423 { tmp4.sign=(byte)0; tmp4.exponent=0x40000008; tmp4.mantissa=0x5a5c35f774e071d5L; };//361.44079386152023162701
4424 tmp3.add(tmp4);
4425 tmp3.mul(tmp2);
4426 { tmp4.sign=(byte)0; tmp4.exponent=0x40000008; tmp4.mantissa=0x61e4d84c2853d5e0L; };//391.57570175111990631099
4427 tmp3.add(tmp4);
4428 tmp3.mul(tmp2);
4429 { tmp4.sign=(byte)0; tmp4.exponent=0x40000007; tmp4.mantissa=0x4c5757448806c48eL; };//152.68235069887081006606
4430 tmp3.add(tmp4);
4431 div(tmp3);
4432 add(tmp1);
4433 if (addPI_2)
4434 add(PI_2);
4435 if (addPI_4)
4436 add(PI_4);
4437 if (s != 0)
4438 neg();
4440 /**
4441 * Calculates the trigonometric arc tangent of this
4442 * <code>Real</code> divided by <code>x</code>, in the range -&pi;
4443 * to &pi;. 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&nbsp;</i><code>double</code><i>&nbsp;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&nbsp;error&nbsp;bound:</i></td><td>
4453 * 2 ULPs
4454 * </td></tr><tr><td><i>
4455 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4456 * </i></td><td>
4457 * 48
4458 * </td></tr></table>
4460 * @param x the <code>Real</code> argument.
4461 */
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))) {
4464 makeNan();
4465 return;
4467 if ((this.exponent == 0 && this.mantissa == 0) && (x.exponent == 0 && x.mantissa == 0))
4468 return;
4469 byte s = sign;
4470 byte s2 = x.sign;
4471 sign = 0;
4472 x.sign = 0;
4473 div(x);
4474 atan();
4475 if (s2 != 0) {
4476 neg();
4477 add(PI);
4479 sign = s;
4481 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4488 * <code>this = Math.{@link Math#sinh(double) sinh}(this);</code>
4489 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4490 * 2 ULPs
4491 * </td></tr><tr><td><i>
4492 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4493 * </i></td><td>
4494 * 67
4495 * </td></tr></table>
4496 */
4497 public void sinh() {
4498 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4499 tmp1.neg();
4500 tmp1.exp();
4501 exp();
4502 sub(tmp1);
4503 scalbn(-1);
4505 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4512 * <code>this = Math.{@link Math#cosh(double) cosh}(this);</code>
4513 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4514 * 2 ULPs
4515 * </td></tr><tr><td><i>
4516 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4517 * </i></td><td>
4518 * 66
4519 * </td></tr></table>
4520 */
4521 public void cosh() {
4522 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4523 tmp1.neg();
4524 tmp1.exp();
4525 exp();
4526 add(tmp1);
4527 scalbn(-1);
4529 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4536 * <code>this = Math.{@link Math#tanh(double) tanh}(this);</code>
4537 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4538 * 2 ULPs
4539 * </td></tr><tr><td><i>
4540 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4541 * </i></td><td>
4542 * 70
4543 * </td></tr></table>
4544 */
4545 public void tanh() {
4546 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4547 tmp1.neg();
4548 tmp1.exp();
4549 exp();
4550 { tmp2.mantissa = this.mantissa; tmp2.exponent = this.exponent; tmp2.sign = this.sign; };
4551 tmp2.add(tmp1);
4552 sub(tmp1);
4553 div(tmp2);
4555 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4562 * <i>none</i>
4563 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4564 * 2 ULPs
4565 * </td></tr><tr><td><i>
4566 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4567 * </i></td><td>
4568 * 77
4569 * </td></tr></table>
4570 */
4571 public void asinh() {
4572 if (!(this.exponent >= 0 && this.mantissa != 0))
4573 return;
4574 // Use symmetry to prevent underflow error for very large negative
4575 // values
4576 byte s = sign;
4577 sign = 0;
4578 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4579 tmp1.sqr();
4580 tmp1.add(ONE);
4581 tmp1.sqrt();
4582 add(tmp1);
4583 ln();
4584 if (!(this.exponent < 0 && this.mantissa != 0))
4585 sign = s;
4587 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4594 * <i>none</i>
4595 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4596 * 2 ULPs
4597 * </td></tr><tr><td><i>
4598 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4599 * </i></td><td>
4600 * 75
4601 * </td></tr></table>
4602 */
4603 public void acosh() {
4604 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4605 tmp1.sqr();
4606 tmp1.sub(ONE);
4607 tmp1.sqrt();
4608 add(tmp1);
4609 ln();
4611 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4618 * <i>none</i>
4619 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4620 * 2 ULPs
4621 * </td></tr><tr><td><i>
4622 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4623 * </i></td><td>
4624 * 57
4625 * </td></tr></table>
4626 */
4627 public void atanh() {
4628 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4629 tmp1.neg();
4630 tmp1.add(ONE);
4631 add(ONE);
4632 div(tmp1);
4633 ln();
4634 scalbn(-1);
4636 /**
4637 * Calculates the factorial of this <code>Real</code>.
4638 * Replaces the contents of this <code>Real</code> with the result.
4639 * The definition is generalized to all real numbers (not only integers),
4640 * by using the fact that <code>(n!)={@link #gamma() gamma}(n+1)</code>.
4642 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4643 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4644 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4645 * <i>none</i>
4646 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4647 * 15 ULPs
4648 * </td></tr><tr><td><i>
4649 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4650 * </i></td><td>
4651 * 8-190
4652 * </td></tr></table>
4653 */
4654 public void fact() {
4655 if (!(this.exponent >= 0))
4656 return;
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)
4660 add(ONE);
4661 gamma();
4662 return;
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) {
4667 mul(tmp1);
4668 tmp1.sub(ONE);
4671 /**
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4678 * <i>none</i>
4679 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4680 * 100+ ULPs
4681 * </td></tr><tr><td><i>
4682 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4683 * </i></td><td>
4684 * 190
4685 * </td></tr></table>
4686 */
4687 public void gamma() {
4688 if (!(this.exponent >= 0))
4689 return;
4690 // x<0: gamma(-x) = -pi/(x*gamma(x)*sin(pi*x))
4691 boolean negative = (this.sign!=0);
4692 abs();
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)
4695 // n=20
4696 { tmp2.mantissa = ONE.mantissa; tmp2.exponent = ONE.exponent; tmp2.sign = ONE.sign; };
4697 boolean divide = false;
4698 while (this.compare(20) < 0) {
4699 divide = true;
4700 tmp2.mul(this);
4701 add(ONE);
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; };
4707 tmp4.sqr(); // x²
4708 // (x-1/2)*ln(x)-x
4709 ln(); { tmp5.mantissa = tmp3.mantissa; tmp5.exponent = tmp3.exponent; tmp5.sign = tmp3.sign; }; tmp5.sub(HALF); mul(tmp5); sub(tmp3);
4710 // + ln(2*pi)/2
4711 { tmp5.sign=(byte)0; tmp5.exponent=0x3fffffff; tmp5.mantissa=0x759fc72192fad29aL; }; add(tmp5);
4712 // + 1/12x
4713 tmp5.assign( 12); tmp5.mul(tmp3); tmp5.recip(); add(tmp5); tmp3.mul(tmp4);
4714 // - 1/360x³
4715 tmp5.assign( 360); tmp5.mul(tmp3); tmp5.recip(); sub(tmp5); tmp3.mul(tmp4);
4716 // + 1/1260x**5
4717 tmp5.assign(1260); tmp5.mul(tmp3); tmp5.recip(); add(tmp5); tmp3.mul(tmp4);
4718 // - 1/1680x**7
4719 tmp5.assign(1680); tmp5.mul(tmp3); tmp5.recip(); sub(tmp5); tmp3.mul(tmp4);
4720 // + 1/1188x**9
4721 tmp5.assign(1188); tmp5.mul(tmp3); tmp5.recip(); add(tmp5);
4722 exp();
4723 if (divide)
4724 div(tmp2);
4725 if (negative) {
4726 { tmp5.mantissa = tmp1.mantissa; tmp5.exponent = tmp1.exponent; tmp5.sign = tmp1.sign; }; // sin() uses tmp1
4727 // -pi/(x*gamma(x)*sin(pi*x))
4728 mul(tmp5);
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() {
4734 // 3 5 7 9
4735 // 2 / x x x x // erfc(x) = 1 - ------ | x - --- + ---- - ---- + ---- - ... |
4736 // sqrt(pi)\ 3 2!*5 3!*7 4!*9 /
4737 //
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);
4742 tmp2.neg();
4743 { tmp3.mantissa = ONE.mantissa; tmp3.exponent = ONE.exponent; tmp3.sign = ONE.sign; }; tmp3Extra = 0;
4744 int i=1;
4745 do {
4746 tmp1Extra = tmp1.mul128(tmp1Extra,tmp2,tmp2Extra);
4747 tmp4.assign(i);
4748 tmp3Extra = tmp3.mul128(tmp3Extra,tmp4,0);
4749 tmp4.assign(2*i+1);
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);
4754 i++;
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() {
4762 // -x² -1
4763 // e x / 1 3 3*5 3*5*7 // erfc(x) = -------- | 1 - --- + ------ - ------ + ------ - ... |
4764 // sqrt(pi) \ 2x² 2 3 4 /
4765 // (2x²) (2x²) (2x²)
4766 // Calculate iteration stop criteria
4767 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4768 tmp1.sqr();
4769 { tmp2.sign=(byte)0; tmp2.exponent=0x40000000; tmp2.mantissa=0x5c3811b4bfd0c8abL; }; // 1/0.694
4770 tmp2.mul(tmp1);
4771 tmp2.sub(HALF);
4772 int digits = tmp2.toInteger(); // number of accurate digits = x*x/0.694-0.5
4773 if (digits > 64)
4774 digits = 64;
4775 tmp1.scalbn(1);
4776 int dxq = tmp1.toInteger()+1;
4777 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
4778 recip();
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; };
4781 tmp3.sqr();
4782 tmp3.neg();
4783 tmp3.scalbn(-1);
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; };
4786 int i=1;
4787 do {
4788 tmp4.mul(2*i-1);
4789 tmp4.mul(tmp3);
4790 add(tmp4);
4791 i++;
4792 } while (tmp4.exponent-0x40000000>-(digits+2) && 2*i-1<dxq);
4793 mul(tmp2);
4794 tmp1.sqr();
4795 tmp1.neg();
4796 tmp1.exp();
4797 mul(tmp1);
4798 { tmp1.sign=(byte)0; tmp1.exponent=0x3fffffff; tmp1.mantissa=0x48375d410a6db447L; }; // 1/sqrt(pi)
4799 mul(tmp1);
4801 /**
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/&#8730;<span style="text-decoration:
4807 * overline;">&pi;</span>&nbsp;·<i>e</i><sup>-t²</sup>&nbsp;dt. It is
4808 * related to the error function, <i>erf</i>, by the formula
4809 * erfc(x)=1-erf(x).
4811 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4812 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4813 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4814 * <i>none</i>
4815 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4816 * 2<sup>19</sup> ULPs
4817 * </td></tr><tr><td><i>
4818 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4819 * </i></td><td>
4820 * 80-4900
4821 * </td></tr></table>
4822 */
4823 public void erfc() {
4824 if ((this.exponent < 0 && this.mantissa != 0))
4825 return;
4826 if ((this.exponent == 0 && this.mantissa == 0)) {
4827 { this.mantissa = ONE.mantissa; this.exponent = ONE.exponent; this.sign = ONE.sign; };
4828 return;
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; };
4833 } else
4834 makeZero(0);
4835 return;
4837 byte s = sign;
4838 sign = 0;
4839 { tmp1.sign=(byte)0; tmp1.exponent=0x40000002; tmp1.mantissa=0x570a3d70a3d70a3dL; }; // 5.44
4840 if (this.lessThan(tmp1))
4841 erfc1Internal();
4842 else
4843 erfc2Internal();
4844 if (s != 0) {
4845 neg();
4846 add(TWO);
4849 /**
4850 * Calculates the inverse complementary error function for this
4851 * <code>Real</code>.
4852 * Replaces the contents of this <code>Real</code> with the result.
4854 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
4855 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
4856 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
4857 * <i>none</i>
4858 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
4859 * 2<sup>19</sup> ULPs
4860 * </td></tr><tr><td><i>
4861 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
4862 * </i></td><td>
4863 * 240-5100
4864 * </td></tr></table>
4865 */
4866 public void inverfc() {
4867 if ((this.exponent < 0 && this.mantissa != 0) || (this.sign!=0) || this.greaterThan(TWO)) {
4868 makeNan();
4869 return;
4871 if ((this.exponent == 0 && this.mantissa == 0)) {
4872 makeInfinity(0);
4873 return;
4875 if (this.equalTo(TWO)) {
4876 makeInfinity(1);
4877 return;
4879 int sign = ONE.compare(this);
4880 if (sign==0) {
4881 makeZero();
4882 return;
4884 if (sign<0) {
4885 neg();
4886 add(TWO);
4888 // Using invphi to calculate inverfc, like this
4889 // inverfc(x) = -invphi(x/2)/(sqrt(2))
4890 scalbn(-1);
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; };
4898 tmp1.ln();
4899 tmp1.mul(-2);
4900 tmp1.sqrt();
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
4903 tmp2.mul(tmp1);
4904 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffffa; tmp3.mantissa=0x53a731ce1ea0be15L; }; // P3=-0.0204231210245
4905 tmp2.add(tmp3);
4906 tmp2.mul(tmp1);
4907 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffffe; tmp3.mantissa=0x579d2d719fc517f3L; }; // P2=-0.342242088547
4908 tmp2.add(tmp3);
4909 tmp2.mul(tmp1);
4910 tmp2.add(-1); // P1=-1
4911 tmp2.mul(tmp1);
4912 { tmp3.sign=(byte)1; tmp3.exponent=0x3ffffffe; tmp3.mantissa=0x527dd3193bc8dd4cL; }; // P0=-0.322232431088
4913 tmp2.add(tmp3);
4914 { tmp3.sign=(byte)0; tmp3.exponent=0x3ffffff7; tmp3.mantissa=0x7e5b0f681d161e7dL; }; // Q4=0.0038560700634
4915 tmp3.mul(tmp1);
4916 { tmp4.sign=(byte)0; tmp4.exponent=0x3ffffffc; tmp4.mantissa=0x6a05ccf9917da0a8L; }; // Q3=0.103537752850
4917 tmp3.add(tmp4);
4918 tmp3.mul(tmp1);
4919 { tmp4.sign=(byte)0; tmp4.exponent=0x3fffffff; tmp4.mantissa=0x43fb32c0d3c14ec4L; }; // Q2=0.531103462366
4920 tmp3.add(tmp4);
4921 tmp3.mul(tmp1);
4922 { tmp4.sign=(byte)0; tmp4.exponent=0x3fffffff; tmp4.mantissa=0x4b56a41226f4ba95L; }; // Q1=0.588581570495
4923 tmp3.add(tmp4);
4924 tmp3.mul(tmp1);
4925 { tmp4.sign=(byte)0; tmp4.exponent=0x3ffffffc; tmp4.mantissa=0x65bb9a7733dd5062L; }; // Q0=0.0993484626060
4926 tmp3.add(tmp4);
4927 tmp2.div(tmp3);
4928 tmp1.add(tmp2);
4929 tmp1.neg();
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; };
4936 tmp5.mul(SQRT1_2);
4937 tmp5.neg();
4938 tmp5.erfc();
4939 tmp5.scalbn(-1);
4940 tmp5.sub(this);
4941 // R = Q*sqrt(2*pi)*e^(Y²/2)
4942 { tmp3.mantissa = sqrtTmp.mantissa; tmp3.exponent = sqrtTmp.exponent; tmp3.sign = sqrtTmp.sign; };
4943 tmp3.sqr();
4944 tmp3.scalbn(-1);
4945 tmp3.exp();
4946 tmp5.mul(tmp3);
4947 { tmp3.sign=(byte)0; tmp3.exponent=0x40000001; tmp3.mantissa=0x50364c7fd89c1659L; }; // sqrt(2*pi)
4948 tmp5.mul(tmp3);
4949 // Z = Y-R/(1+R*Y/2)
4950 { this.mantissa = sqrtTmp.mantissa; this.exponent = sqrtTmp.exponent; this.sign = sqrtTmp.sign; };
4951 mul(tmp5);
4952 scalbn(-1);
4953 add(ONE);
4954 rdiv(tmp5);
4955 neg();
4956 add(sqrtTmp);
4957 // calculate inverfc(x) = -invphi(x/2)/(sqrt(2))
4958 mul(SQRT1_2);
4959 if (sign>0)
4960 neg();
4962 //*************************************************************************
4963 // Calendar conversions taken from
4964 // http://www.fourmilab.ch/documents/calendar/
4965 private static int floorDiv(int a, int b) {
4966 if (a>=0)
4967 return a/b;
4968 return -((-a+b-1)/b);
4970 private static int floorMod(int a, int b) {
4971 if (a>=0)
4972 return a%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) {
4982 return ((366 - 1) +
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;
4995 wjd = jd;
4996 depoch = wjd - 366;
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)))
5006 year++;
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;
5014 /**
5015 * Converts this <code>Real</code> from "hours" to "days, hours,
5016 * minutes and seconds".
5017 * Replaces the contents of this <code>Real</code> with the result.
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
5039 * <i>none</i>
5040 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
5041 * ?
5042 * </td></tr><tr><td><i>
5043 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
5044 * </i></td><td>
5045 * 19
5046 * </td></tr></table>
5047 */
5048 public void toDHMS() {
5049 if (!(this.exponent >= 0 && this.mantissa != 0))
5050 return;
5051 boolean negative = (this.sign!=0);
5052 abs();
5053 int D,m;
5054 long h;
5055 h = toLong();
5056 frac();
5057 tmp1.assign(60);
5058 mul(tmp1);
5059 m = toInteger();
5060 frac();
5061 mul(tmp1);
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; };
5065 tmp2.scalbn(-16);
5066 add(tmp2);
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; };
5070 m++;
5071 if (m >= 60) {
5072 m -= 60;
5073 h++;
5075 // Phew! That was close. From now on it is integer arithmetic...
5076 } else {
5077 // Nope. So try to undo the damage...
5078 sub(tmp2);
5080 D = (int)(h/24);
5081 h %= 24;
5082 if (D >= 366)
5083 D = jd_to_gregorian(D);
5084 add(m*100);
5085 div(10000);
5086 tmp1.assign(D*100L+h);
5087 add(tmp1);
5088 if (negative)
5089 neg();
5091 /**
5092 * Converts this <code>Real</code> from "days, hours, minutes and
5093 * seconds" to "hours".
5094 * Replaces the contents of this <code>Real</code> with the result.
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
5111 * calculations <a
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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
5117 * <i>none</i>
5118 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
5119 * ?
5120 * </td></tr><tr><td><i>
5121 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
5122 * </i></td><td>
5123 * 19
5124 * </td></tr></table>
5125 */
5126 public void fromDHMS() {
5127 if (!(this.exponent >= 0 && this.mantissa != 0))
5128 return;
5129 boolean negative = (this.sign!=0);
5130 abs();
5131 int Y,M,D,m;
5132 long h;
5133 h = toLong();
5134 frac();
5135 tmp1.assign(100);
5136 mul(tmp1);
5137 m = toInteger();
5138 frac();
5139 mul(tmp1);
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; };
5143 tmp2.scalbn(-10);
5144 add(tmp2);
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; };
5148 m++;
5149 if (m >= 100) {
5150 m -= 100;
5151 h++;
5153 // Phew! That was close. From now on it is integer arithmetic...
5154 } else {
5155 // Nope. So try to undo the damage...
5156 sub(tmp2);
5158 D = (int)(h/100);
5159 h %= 100;
5160 if (D>=10000) {
5161 M = D/100;
5162 D %= 100;
5163 if (D==0) D=1;
5164 Y = M/100;
5165 M %= 100;
5166 if (M==0) M=1;
5167 D = gregorian_to_jd(Y,M,D);
5169 add(m*60);
5170 div(3600);
5171 tmp1.assign(D*24L+h);
5172 add(tmp1);
5173 if (negative)
5174 neg();
5176 /**
5177 * Assigns this <code>Real</code> the current time. The time is
5178 * encoded into the digits of the number (in decimal form), using the
5179 * format "<code>hh.mmss</code>", where "<code>hh</code>" is hours,
5180 * "<code>mm</code>" is minutes and "code>ss</code>" is seconds.
5182 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5183 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5184 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
5185 * <i>none</i>
5186 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
5187 * ½ ULPs
5188 * </td></tr><tr><td><i>
5189 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
5190 * </i></td><td>
5191 * 8.9
5192 * </td></tr></table>
5193 */
5194 public void time() {
5195 long now = System.currentTimeMillis();
5196 int h,m,s;
5197 now /= 1000;
5198 s = (int)(now % 60);
5199 now /= 60;
5200 m = (int)(now % 60);
5201 now /= 60;
5202 h = (int)(now % 24);
5203 assign((h*100+m)*100+s);
5204 div(10000);
5206 /**
5207 * Assigns this <code>Real</code> the current date. The date is
5208 * encoded into the digits of the number (in decimal form), using
5209 * the format "<code>YYYYMMDD00</code>", where "<code>YYYY</code>"
5210 * is the year, "<code>MM</code>" is the month (1-12) and
5211 * "<code>DD</code>" is the day of the month (1-31). The
5212 * "<code>00</code>" in this format is a sort of padding to make it
5213 * compatible with the format used by {@link #toDHMS()} and {@link
5214 * #fromDHMS()}.
5216 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5217 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5218 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
5219 * <i>none</i>
5220 * </td></tr><tr><td><i>Error&nbsp;bound:</i></td><td>
5221 * 0 ULPs
5222 * </td></tr><tr><td><i>
5223 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
5224 * </i></td><td>
5225 * 30
5226 * </td></tr></table>
5227 */
5228 public void date() {
5229 long now = System.currentTimeMillis();
5230 now /= 86400000; // days
5231 now *= 24; // hours
5232 assign(now);
5233 add(719528*24); // 1970-01-01 era
5234 toDHMS();
5236 //*************************************************************************
5237 /**
5238 * The seed of the first 64-bit CRC generator of the random
5239 * routine. Set this value to control the generated sequence of random
5240 * numbers. Should never be set to 0. See {@link #random()}.
5241 * Initialized to mantissa of pi.
5242 */
5243 public static long randSeedA = 0x6487ed5110b4611aL;
5244 /**
5245 * The seed of the second 64-bit CRC generator of the random
5246 * routine. Set this value to control the generated sequence of random
5247 * numbers. Should never be set to 0. See {@link #random()}.
5248 * Initialized to mantissa of e.
5249 */
5250 public static long randSeedB = 0x56fc2a2c515da54dL;
5251 // 64 Bit CRC Generators
5252 //
5253 // The generators used here are not cryptographically secure, but
5254 // two weak generators are combined into one strong generator by
5255 // skipping bits from one generator whenever the other generator
5256 // produces a 0-bit.
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) {
5263 long answer = 0;
5264 while (bits-- > 0) {
5265 while (randSeedA >= 0)
5266 advanceBit();
5267 answer = (answer<<1) + (randSeedB < 0 ? 1 : 0);
5268 advanceBit();
5270 return answer;
5272 /**
5273 * Accumulate more randomness into the random number generator, to
5274 * decrease the predictability of the output from {@link
5275 * #random()}. The input should contain data with some form of
5276 * inherent randomness e.g. System.currentTimeMillis().
5278 * @param seed some extra randomness for the random number generator.
5279 */
5280 public static void accumulateRandomness(long seed) {
5281 randSeedA ^= seed & 0x5555555555555555L;
5282 randSeedB ^= seed & 0xaaaaaaaaaaaaaaaaL;
5283 nextBits(63);
5285 /**
5286 * Calculates a pseudorandom number in the range [0,&nbsp;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&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
5298 * <code>this = Math.{@link Math#random() random}();</code>
5299 * </td></tr><tr><td><i>Approximate&nbsp;error&nbsp;bound:</i></td><td>
5300 * -
5301 * </td></tr><tr><td><i>
5302 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
5303 * </i></td><td>
5304 * 81
5305 * </td></tr></table>
5306 */
5307 public void random() {
5308 sign = 0;
5309 exponent = 0x3fffffff;
5310 while (nextBits(1) == 0)
5311 exponent--;
5312 mantissa = 0x4000000000000000L+nextBits(62);
5314 //*************************************************************************
5315 private int digit(char a, int base, boolean twosComplement) {
5316 int digit = -1;
5317 if (a>='0' && a<='9')
5318 digit = a-'0';
5319 else if (a>='A' && a<='F')
5320 digit = a-'A'+10;
5321 if (digit >= base)
5322 return -1;
5323 if (twosComplement)
5324 digit ^= base-1;
5325 return digit;
5327 private void shiftUp(int base) {
5328 if (base==2)
5329 scalbn(1);
5330 else if (base==8)
5331 scalbn(3);
5332 else if (base==16)
5333 scalbn(4);
5334 else
5335 mul10();
5337 private void atof(String a, int base) {
5338 makeZero();
5339 int length = a.length();
5340 int index = 0;
5341 byte tmpSign = 0;
5342 boolean compl = false;
5343 while (index<length && a.charAt(index)==' ')
5344 index++;
5345 if (index<length && a.charAt(index)=='-') {
5346 tmpSign=1;
5347 index++;
5348 } else if (index<length && a.charAt(index)=='+') {
5349 index++;
5350 } else if (index<length && a.charAt(index)=='/') {
5351 // Input is twos complemented negative number
5352 compl=true;
5353 tmpSign=1;
5354 index++;
5356 int d;
5357 while (index<length && (d=digit(a.charAt(index),base,compl))>=0) {
5358 shiftUp(base);
5359 add(d);
5360 index++;
5362 int exp=0;
5363 if (index<length && (a.charAt(index)=='.' || a.charAt(index)==',')) {
5364 index++;
5365 while (index<length && (d=digit(a.charAt(index),base,compl))>=0) {
5366 shiftUp(base);
5367 add(d);
5368 exp--;
5369 index++;
5372 if (compl)
5373 add(ONE);
5374 while (index<length && a.charAt(index)==' ')
5375 index++;
5376 if (index<length && (a.charAt(index)=='e' || a.charAt(index)=='E')) {
5377 index++;
5378 int exp2 = 0;
5379 boolean expNeg = false;
5380 if (index<length && a.charAt(index)=='-') {
5381 expNeg = true;
5382 index++;
5383 } else if (index<length && a.charAt(index)=='+') {
5384 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';
5392 index++;
5394 if (expNeg)
5395 exp2 = -exp2;
5396 exp += exp2;
5398 if (base==2)
5399 scalbn(exp);
5400 else if (base==8)
5401 scalbn(exp*3);
5402 else if (base==16)
5403 scalbn(exp*4);
5404 else {
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; };
5409 if (exp<0) {
5410 tmp1.pow(-exp/2);
5411 div(tmp1);
5412 } else {
5413 tmp1.pow(exp/2);
5414 mul(tmp1);
5416 exp -= exp/2;
5418 { tmp1.mantissa = TEN.mantissa; tmp1.exponent = TEN.exponent; tmp1.sign = TEN.sign; };
5419 if (exp<0) {
5420 tmp1.pow(-exp);
5421 div(tmp1);
5422 } else if (exp>0) {
5423 tmp1.pow(exp);
5424 mul(tmp1);
5427 sign = tmpSign;
5429 //*************************************************************************
5430 private void normalizeDigits(byte[] digits, int nDigits, int base) {
5431 byte carry = 0;
5432 boolean isZero = true;
5433 for (int i=nDigits-1; i>=0; i--) {
5434 if (digits[i] != 0)
5435 isZero = false;
5436 digits[i] += carry;
5437 carry = 0;
5438 if (digits[i] >= base) {
5439 digits[i] -= base;
5440 carry = 1;
5443 if (isZero) {
5444 exponent = 0;
5445 return;
5447 if (carry != 0) {
5448 if (digits[nDigits-1] >= base/2)
5449 digits[nDigits-2] ++; // Rounding, may be inaccurate
5450 System.arraycopy(digits, 0, digits, 1, nDigits-1);
5451 digits[0] = carry;
5452 exponent++;
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;
5461 exponent--;
5464 private int getDigits(byte[] digits, int base) {
5465 if (base == 10)
5467 { tmp1.mantissa = this.mantissa; tmp1.exponent = this.exponent; tmp1.sign = this.sign; };
5468 tmp1.abs();
5469 { tmp2.mantissa = tmp1.mantissa; tmp2.exponent = tmp1.exponent; tmp2.sign = tmp1.sign; };
5470 int exp = exponent = tmp1.lowPow10();
5471 exp -= 18;
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
5479 if (exp_neg)
5480 tmp2.mul(tmp1);
5481 else
5482 tmp2.div(tmp1);
5483 { tmp1.mantissa = TEN.mantissa; tmp1.exponent = TEN.exponent; tmp1.sign = TEN.sign; };
5484 tmp1.pow(exp-(exp/2));
5485 } else {
5486 { tmp1.mantissa = TEN.mantissa; tmp1.exponent = TEN.exponent; tmp1.sign = TEN.sign; };
5487 tmp1.pow(exp);
5489 if (exp_neg)
5490 tmp2.mul(tmp1);
5491 else
5492 tmp2.div(tmp1);
5493 long a;
5494 if (tmp2.exponent > 0x4000003e) {
5495 tmp2.exponent--;
5496 tmp2.round();
5497 a = tmp2.toLong();
5498 if (a >= 5000000000000000000L) { // Rounding up gave 20 digits
5499 exponent++;
5500 a /= 5;
5501 digits[18] = (byte)(a%10);
5502 a /= 10;
5503 } else {
5504 digits[18] = (byte)((a%5)*2);
5505 a /= 5;
5507 } else {
5508 tmp2.round();
5509 a = tmp2.toLong();
5510 digits[18] = (byte)(a%10);
5511 a /= 10;
5513 for (int i=17; i>=0; i--) {
5514 digits[i] = (byte)(a%10);
5515 a /= 10;
5517 digits[19] = 0;
5518 return 19;
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
5524 } else {
5525 if ((this.sign!=0)) {
5526 mantissa = -mantissa;
5527 if (((mantissa >> 62)&3) == 3) {
5528 mantissa <<= 1;
5529 exponent--;
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
5539 mantissa <<= 1;
5540 exponent--;
5541 accurateBits--;
5543 else if (shift>0) {
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
5566 exponent++;
5567 digits[0] = 1;
5568 for (int i=1; i<digits.length; i++)
5569 digits[i] = 0;
5570 return true;
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);
5578 /**
5579 * The number format used to convert <code>Real</code> values to
5580 * <code>String</code> using {@link Real#toString(Real.NumberFormat)
5581 * Real.toString()}. The default number format uses base-10, maximum
5582 * precision, removal of trailing zeros and '.' as radix point.
5584 * <p>Note that the fields of <code>NumberFormat</code> are not
5585 * protected in any way, the user is responsible for setting the
5586 * correct values to get a correct result.
5587 */
5588 public static class NumberFormat
5590 /**
5591 * The number base of the conversion. The default value is 10,
5592 * valid options are 2, 8, 10 and 16. See {@link Real#and(Real)
5593 * Real.and()} for an explanation of the interpretation of a
5594 * <code>Real</code> in base 2, 8 and 16.
5596 * <p>Negative numbers output in base-2, base-8 and base-16 are
5597 * shown in two's complement form. This form guarantees that a
5598 * negative number starts with at least one digit that is the
5599 * maximum digit for that base, i.e. '1', '7', and 'F',
5600 * respectively. A positive number is guaranteed to start with at
5601 * least one '0'. Both positive and negative numbers are extended
5602 * to the left using this digit, until {@link #maxwidth} is
5603 * reached.
5604 */
5605 public int base = 10;
5606 /**
5607 * Maximum width of the converted string. The default value is 30.
5608 * If the conversion of a <code>Real</code> with a given {@link
5609 * #precision} would produce a string wider than
5610 * <code>maxwidth</code>, <code>precision</code> is reduced until
5611 * the number fits within the given width. If
5612 * <code>maxwidth</code> is too small to hold the number with its
5613 * sign, exponent and a <code>precision</code> of 1 digit, the
5614 * string may become wider than <code>maxwidth</code>.
5616 * <p>If <code>align</code> is set to anything but
5617 * <code>ALIGN_NONE</code> and the converted string is shorter
5618 * than <code>maxwidth</code>, the resulting string is padded with
5619 * spaces to the specified width according to the alignment.
5620 */
5621 public int maxwidth = 30;
5622 /**
5623 * The precision, or number of digits after the radix point in the
5624 * converted string when using the <i>FIX</i>, <i>SCI</i> or
5625 * <i>ENG</i> format (see {@link #fse}). The default value is 16,
5626 * valid values are 0-16 for base-10 and base-16 conversion, 0-21
5627 * for base-8 conversion, and 0-63 for base-2 conversion.
5629 * <p>The <code>precision</code> may be reduced to make the number
5630 * fit within {@link #maxwidth}. The <code>precision</code> is
5631 * also reduced if it is set higher than the actual numbers of
5632 * significant digits in a <code>Real</code>. When
5633 * <code>fse</code> is set to <code>FSE_NONE</code>, i.e. "normal"
5634 * output, the precision is always at maximum, but trailing zeros
5635 * are removed.
5636 */
5637 public int precision = 16;
5638 /**
5639 * The special output formats <i>FIX</i>, <i>SCI</i> or <i>ENG</i>
5640 * are enabled with this field. The default value is
5641 * <code>FSE_NONE</code>. Valid options are listed below.
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>&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;-3.4753e-13</code>
5653 * <p><dl>
5654 * <dt>{@link #FSE_NONE}
5655 * <dd>Normal output. Numbers are output with maximum precision,
5656 * trailing zeros are removed. The format is changed to
5657 * exponent form if the number is larger than the number of
5658 * significant digits allows, or if the resulting string would
5659 * exceed <code>maxwidth</code> without the exponent form.
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.
5676 * </dl>
5677 */
5678 public int fse = FSE_NONE;
5679 /**
5680 * The character used as the radix point. The default value is
5681 * <code>'.'</code>. Theoretcally any character that does not
5682 * otherwise occur in the output can be used, such as
5683 * <code>','</code>.
5685 * <p>Note that setting this to anything but <code>'.'</code> and
5686 * <code>','</code> is not supported by any conversion method from
5687 * <code>String</code> back to <code>Real</code>.
5688 */
5689 public char point = '.';
5690 /**
5691 * Set to <code>true</code> to remove the radix point if this is
5692 * the last character in the converted string. This is the
5693 * default.
5694 */
5695 public boolean removePoint = true;
5696 /**
5697 * The character used as the thousands separator. The default
5698 * value is the character code <code>0</code>, which disables
5699 * thousands-separation. Theoretcally any character that does not
5700 * otherwise occur in the output can be used, such as
5701 * <code>','</code> or <code>' '</code>.
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.
5714 */
5715 public char thousand = 0;
5716 /**
5717 * The alignment of the output string within a field of {@link
5718 * #maxwidth} characters. The default value is
5719 * <code>ALIGN_NONE</code>. Valid options are defined as follows:
5721 * <p><dl>
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.
5739 * </dl>
5740 */
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)
5762 s.append(' ');
5763 } else if (format.align == NumberFormat.ALIGN_RIGHT) {
5764 while (s.length()<format.maxwidth)
5765 s.insert(0,' ');
5766 } else if (format.align == NumberFormat.ALIGN_CENTER) {
5767 while (s.length()<format.maxwidth) {
5768 s.append(' ');
5769 if (s.length()<format.maxwidth)
5770 s.insert(0,' ');
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);
5778 /**
5779 * This string holds the only valid characters to use in hexadecimal
5780 * numbers. Equals <code>"0123456789ABCDEF"</code>.
5781 * See {@link #assign(String,int)}.
5782 */
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) {
5796 case 2:
5797 digitsPerThousand = 8;
5798 break;
5799 case 8:
5800 digitsPerThousand = 1000; // Disable thousands separator
5801 break;
5802 case 16:
5803 digitsPerThousand = 4;
5804 break;
5805 case 10:
5806 default:
5807 digitsPerThousand = 3;
5808 break;
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
5816 int precision;
5817 int pointPos = 0;
5818 do
5820 int width = format.maxwidth-1; // subtract 1 for decimal point
5821 int prefix = 0;
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;
5830 useExp = true;
5831 break;
5832 case NumberFormat.FSE_ENG:
5833 pointPos = floorMod(tmp4.exponent,3);
5834 precision = format.precision+1+pointPos;
5835 useExp = true;
5836 break;
5837 case NumberFormat.FSE_FIX:
5838 case NumberFormat.FSE_NONE:
5839 default:
5840 precision = 1000;
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)
5850 useExp = true;
5851 } else {
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
5858 width++;
5861 break;
5863 if (prefix!=0 && pointPos>=0)
5864 width -= prefix;
5865 ftoaExp.setLength(0);
5866 if (useExp) {
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)
5874 precision = width;
5875 if (precision > width+pointPos) // In case of negative pointPos
5876 precision = width+pointPos;
5877 if (precision <= 0)
5878 precision = 1;
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);
5890 if (pointPos < 0) {
5891 ftoaBuf.append(prefixChar);
5892 ftoaBuf.append(format.point);
5893 while (pointPos < -1) {
5894 ftoaBuf.append(prefixChar);
5895 pointPos++;
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);
5903 if (pointPos == 0)
5904 ftoaBuf.append(format.point);
5905 pointPos--;
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);
5917 // Add exponent
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) {
5922 pointPos2++;
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();
5934 /**
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.
5940 *
5941 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5942 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5943 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td><td>
5944 * <code>this.toString()
5945 * </td></tr><tr><td><i>
5946 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;
5947 * </i></td><td>
5948 * 130
5949 * </td></tr></table>
5951 * @return a <code>String</code> representation of this <code>Real</code>.
5952 */
5953 public String toString() {
5954 tmpFormat.base = 10;
5955 return ftoa(tmpFormat);
5957 /**
5958 * Converts this <code>Real</code> to a <code>String</code> using
5959 * the default <code>NumberFormat</code> with <code>base</code> set
5960 * according to the argument.
5962 * <p>See {@link Real.NumberFormat NumberFormat} for a description
5963 * of the default way that numbers are formatted.
5964 *
5965 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5966 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5967 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td>
5968 * <td colspan="2">
5969 * <code>this.toString() // Works only for base-10</code>
5970 * </td></tr><tr><td rowspan="4" valign="top"><i>
5971 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;</i>
5972 * </td><td width="1%">base-2</td><td>
5973 * 120
5974 * </td></tr><tr><td>base-8</td><td>
5975 * 110
5976 * </td></tr><tr><td>base-10</td><td>
5977 * 130
5978 * </td></tr><tr><td>base-16&nbsp;&nbsp;</td><td>
5979 * 120
5980 * </td></tr></table>
5982 * @param base the base for the conversion. Valid base values are
5983 * 2, 8, 10 and 16.
5984 * @return a <code>String</code> representation of this <code>Real</code>.
5985 */
5986 public String toString(int base) {
5987 tmpFormat.base = base;
5988 return ftoa(tmpFormat);
5990 /**
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.
5996 *
5997 * <p><table border="1" width="100%" cellpadding="3" cellspacing="0"
5998 * bgcolor="#e8d0ff"><tr><td width="1%"><i>
5999 * Equivalent&nbsp;</i><code>double</code><i>&nbsp;code:</i></td>
6000 * <td colspan="2">
6001 * <code>String.format("%...g",this); // Works only for base-10</code>
6002 * </td></tr><tr><td rowspan="4" valign="top"><i>
6003 * Execution&nbsp;time&nbsp;relative&nbsp;to&nbsp;add:&nbsp;&nbsp;</i>
6004 * </td><td width="1%">base-2</td><td>
6005 * 120
6006 * </td></tr><tr><td>base-8</td><td>
6007 * 110
6008 * </td></tr><tr><td>base-10</td><td>
6009 * 130
6010 * </td></tr><tr><td>base-16&nbsp;&nbsp;</td><td>
6011 * 120
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>.
6016 */
6017 public String toString(NumberFormat format) {
6018 return ftoa(format);