Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 17490454x 17490454x 17490454x 17477244x 11954x 17465292x 17465292x 2x 130248x 130248x 127854x 2x 130248x 10288x 10288x 10288x 33876x 33876x 33876x 33876x 33876x 33876x 22182x 22182x 22182x 2436x 2436x 2436x 2436x 2436x 2436x 4870x 4870x 4870x 4870x 4870x 2436x 2436x 2436x 15396x 15396x 15396x 2x 2x 2x 2502x 2502x 2502x 2502x 2502x 2502x 2502x 2502x 2502x 2502x 2502x 2502x 2502x 2x 2x 2502x 2502x 2502x 2502x 2502x 2502x 842x 1662x 2502x 360x 360x 360x 360x 360x 360x 360x 360x 2x 2x 360x 360x 360x 360x 360x 360x 360x 360x 2x 2x 360x 360x 358x 4x 4x 360x 360x 360x 360x 1434x 978x 978x 978x 978x 360x 360x 360x 360x 360x 360x 360x 256x 256x 256x 78x 180x 256x 2x 256x 256x 256x 650x 650x 650x 650x 650x 2594x 1526x 650x 650x 650x 650x 650x 650x 650x 650x 650x 650x 650x 650x 48x 48x 48x 78x 78x 78x 48x 48x 2x 650x 650x 650x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1218x 1218x 1218x 1178x 1178x 1178x 1178x 1178x 1178x 1014x 2x 2x 6x 6x 2x 2x 272x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 76x 76x 76x 76x 76x 58430x 58430x 58430x 58430x 58430x 58430x 24346x 24346x 24346x 24346x 24346x 24346x 24346x 24346x 24346x 24346x 24346x 9742x 9742x 9742x 9742x 9742x 9742x 9742x 9742x 268982x 268982x 268982x 315028x 315028x 315028x 315028x 17044x 17044x 17044x 17044x 17044x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24346x 24346x 24346x 24346x 9740x 9740x 9740x 9740x 7306x 7306x 7306x 7306x 7306x 7306x 7306x 7306x 7306x 7306x 7306x 7306x 7306x 41178x 41178x 41178x 41178x 41178x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 33876x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 2436x 4872x 4872x 4872x 4872x 4872x 4872x 4872x 4872x 4872x 4872x 4872x 4872x 4872x 4872x 4872x 2x 2x 2x 2x 2x 2x 2x 1100748x 1100748x 1100748x 1100748x 1100748x 9254418x 9254418x 9254418x 1100748x 77376x 1854x 1854x 1854x 1854x 1854x 1854x 1854x 1854x 1854x 1854x 928x 928x 2x |
//////////// DD (double-double) precision utilities ///////////
function toDD(x) {
return Array.isArray(x) ? x : [x, 0];
}
// Convert complex [real, imag] to DDc format [r_hi, r_lo, i_hi, i_lo]
function toDDc(c) {
if (c.length == 4) { return c; }
const r = toDD(c[0]);
const j = toDD(c[1]);
return [r[0], r[1], j[0], j[1]];
}
// Add two DD values, returning DD result
function toDDAdd(a, b) {
const aa = toDD(a);
const bb = toDD(b);
const out = [0, 0];
ArddAdd(out, 0, aa[0], aa[1], bb[0], bb[1]);
return out;
}
// Convert various complex formats to QDc format (8 elements)
// Handles: float64 pair [r, i], DDc [r_hi, r_lo, i_hi, i_lo], QDc [r0..r3, i0..i3]
function toQDc(c) {
if (!c) return null;
if (c.length === 8) return c; // Already QDc
if (c.length === 4) {
// DDc format: [r_hi, r_lo, i_hi, i_lo]
return [c[0], c[1], 0, 0, c[2], c[3], 0, 0];
}
if (c.length === 2) {
// Float64 pair: [real, imag]
return [c[0], 0, 0, 0, c[1], 0, 0, 0];
}
return null;
}
// Oct helpers for both main thread and workers (via mathCode)
function toQD(x) {
if (!Array.isArray(x)) return [x, 0, 0, 0];
if (x.length >= 4) return [x[0], x[1], x[2], x[3]];
if (x.length === 3) return [x[0], x[1], x[2], 0];
return [x[0], x[1] || 0, 0, 0];
}
function toQDScale(a, s) {
const o = toQD(a);
return [o[0] * s, o[1] * s, o[2] * s, o[3] * s];
}
function toQDAdd(a, b) {
const aa = toQD(a);
const bb = toQD(b);
const out = new Array(4);
ArqdAdd(out, 0, aa[0], aa[1], aa[2], aa[3], bb[0], bb[1], bb[2], bb[3]);
return out;
}
function toQDSub(a, b) {
const bb = toQD(b);
return toQDAdd(a, [-bb[0], -bb[1], -bb[2], -bb[3]]);
}
function toQDMul(a, b) {
const aa = toQD(a);
const bb = toQD(b);
const out = new Array(4);
ArqdMul(out, 0, aa[0], aa[1], aa[2], aa[3], bb[0], bb[1], bb[2], bb[3]);
return out;
}
function toQDSquare(a) {
const aa = toQD(a);
const out = new Array(4);
ArqdSquare(out, 0, aa[0], aa[1], aa[2], aa[3]);
return out;
}
function toQDDouble(a) {
const aa = toQD(a);
return [aa[0] * 2, aa[1] * 2, aa[2] * 2, aa[3] * 2];
}
function qdToNumber(o) {
const v = toQD(o);
return v[0] + v[1] + v[2] + v[3];
}
function qdToDD(o) {
const v = toQD(o);
return [v[0], v[1] + v[2] + v[3]];
}
// Convert a float64 to its exact BigInt representation, scaled by 10^scale.
function float64ToBigIntScaled(x, scale) {
if (x === 0) return 0n;
if (!Number.isFinite(x)) return 0n;
const sign = x < 0 ? -1n : 1n;
const absX = Math.abs(x);
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
view.setFloat64(0, absX);
const bits = view.getBigUint64(0);
const expBits = Number((bits >> 52n) & 0x7FFn);
const mantissaBits = bits & 0xFFFFFFFFFFFFFn;
let binaryExp, mantissa;
if (expBits === 0) {
mantissa = mantissaBits;
binaryExp = -1022 - 52;
} else {
mantissa = (1n << 52n) | mantissaBits;
binaryExp = expBits - 1023 - 52;
}
const five = 5n, two = 2n;
let result = sign * mantissa * (five ** BigInt(scale));
const netBinaryExp = binaryExp + scale;
if (netBinaryExp >= 0) {
result = result * (two ** BigInt(netBinaryExp));
} else {
result = result / (two ** BigInt(-netBinaryExp));
}
return result;
}
function decimalToQD(decimalString) {
let s = decimalString.trim().toLowerCase();
const neg = s.startsWith('-');
if (neg) s = s.slice(1);
if (s.startsWith('+')) s = s.slice(1);
let exp = 0;
const eIdx = s.indexOf('e');
if (eIdx !== -1) {
exp = parseInt(s.slice(eIdx + 1), 10);
s = s.slice(0, eIdx);
}
const parts = s.split('.');
const intPart = parts[0] || '0';
const fracPart = parts[1] || '';
let scale = fracPart.length - exp;
const bigStr = intPart + fracPart;
let n = BigInt(bigStr.replace(/^0+/, '') || '0');
if (neg) n = -n;
if (scale < 0) {
n = n * (BigInt(10) ** BigInt(-scale));
scale = 0;
}
// Use internal scale of 60 decimal places for exact float64 representation
const internalScale = 60;
if (scale < internalScale) {
n = n * (BigInt(10) ** BigInt(internalScale - scale));
} else if (scale > internalScale) {
n = n / (BigInt(10) ** BigInt(scale - internalScale));
}
const limbs = [];
let residual = n;
const divisor = BigInt(10) ** BigInt(internalScale);
for (let i = 0; i < 4; i++) {
if (residual === 0n) { limbs.push(0); continue; }
const limb = Number(residual) / Number(divisor);
limbs.push(limb);
const limbBigInt = float64ToBigIntScaled(limb, internalScale);
residual = residual - limbBigInt;
}
// Normalize the result to ensure consistent representation across all QD operations.
// Without this, limbs may not satisfy |limb[i]| < ulp(limb[i-1])/2, causing
// ArqdAdd/toQDAdd to produce different representations when adding zero.
// Uses quickTwoSum cascade to renormalize (fully inlined to avoid dependency issues).
let s0 = limbs[0] + limbs[1], e0 = limbs[1] - (s0 - limbs[0]);
let s1 = e0 + limbs[2], e1 = limbs[2] - (s1 - e0);
let s2 = e1 + limbs[3], s3 = limbs[3] - (s2 - e1);
let t0 = s0 + s1; s1 = s1 - (t0 - s0); s0 = t0;
t0 = s1 + s2; s2 = s2 - (t0 - s1); s1 = t0;
t0 = s2 + s3; s3 = s3 - (t0 - s2); s2 = t0;
return [s0, s1, s2, s3];
}
// Parse a complex number string into QD precision real and imaginary parts.
// Accepts formats like: "-0.74543+0.11301i", "0.5i", "-i", "0.25", ""
// Returns { re: QD array, im: QD array } or null if parsing fails
function parseComplexToQD(coordString) {
const trimmed = coordString.trim();
// Empty string returns null to signal default/inherit behavior
if (trimmed === '') {
return null;
}
// Match: optional real part, then optional imaginary part (with 'i' or just 'i')
const match = trimmed.match(
/([-+]?\d*\.?\d+(?:e[-+]?\d+)?\b)?(?:([-+]?\d*\.?\d+(?:e[-+]?\d+)?)i|([-+]?i))?$/
);
if (!match) {
return null;
}
// Extract real part (default to '0' if not present)
const realStr = match[1] || '0';
// Extract imaginary part: either match[2] (number with 'i') or match[3] (just 'i' or '-i')
let imagStr = match[2] || (match[3] ? match[3].replace('i', '1') : '0');
return {
re: decimalToQD(realStr),
im: decimalToQD(imagStr)
};
}
// Convert QD value to decimal string with exact BigInt arithmetic.
function qdToDecimalString(qd, digits) {
const o = Array.isArray(qd) ? qd : [qd, 0, 0, 0];
const internalScale = 60;
let total = 0n;
for (let i = 0; i < 4; i++) {
if (o[i] !== 0) {
total += float64ToBigIntScaled(o[i], internalScale);
}
}
const neg = total < 0n;
if (neg) total = -total;
const divisor = BigInt(10) ** BigInt(internalScale);
const intPart = total / divisor;
let fracPart = total % divisor;
let result = intPart.toString();
if (digits > 0) {
let fracStr = fracPart.toString().padStart(internalScale, '0');
if (digits < fracStr.length) {
const roundDigit = parseInt(fracStr[digits]);
fracStr = fracStr.slice(0, digits);
if (roundDigit >= 5) {
let carry = 1;
let chars = fracStr.split('');
for (let i = chars.length - 1; i >= 0 && carry; i--) {
const d = parseInt(chars[i]) + carry;
chars[i] = (d % 10).toString();
carry = Math.floor(d / 10);
}
fracStr = chars.join('');
if (carry) result = (BigInt(result) + 1n).toString();
}
} else {
fracStr = fracStr.padEnd(digits, '0');
}
fracStr = fracStr.replace(/0+$/, '');
if (fracStr.length > 0) result += '.' + fracStr;
}
return result;
}
function fast2Sum(a, b) {
let s = a + b;
let t = b - (s - a);
return [s, t];
}
function slow2Sum(a, b) {
let s = a + b;
let c = s - a;
return [s, (a - (s - c)) + (b - c)];
}
function ddSplit(a) {
const c = (134217729) * a; // 2^27 + 1, Veltkamp-Dekker constant
const x = c - (c - a);
const y = a - x;
return [x, y];
}
function twoProduct(a, b) {
let p = a * b;
let [ah, al] = ddSplit(a);
let [bh, bl] = ddSplit(b);
let err = ((ah * bh - p) + ah * bl + al * bh) + al * bl;
return [p, err];
}
function twoSquare(a) {
let p = a * a;
let [ah, al] = ddSplit(a);
let err = ((ah * ah - p) + 2 * ah * al) + al * al;
return [p, err];
}
function ddAdd(a, b) {
let [a1, a0] = a;
let [b1, b0] = b;
let [h1, h2] = slow2Sum(a1, b1);
let [l1, l2] = slow2Sum(a0, b0);
let [v1, v2] = fast2Sum(h1, h2 + l1);
return fast2Sum(v1, v2 + l2);
}
function ddMul(a, b) {
let [a1, a0] = a;
let [b1, b0] = b;
let [p1, p2] = twoProduct(a1, b1);
return fast2Sum(p1, p2 + a1 * b0 + b1 * a0);
}
function ddDouble(a) {
return [a[0] * 2, a[1] * 2];
}
function ddScale(q, s) {
let [q1, q0] = q;
let [p1, p2] = twoProduct(q1, s);
return fast2Sum(p1, p2 + s * q0);
}
function ddSquare(a) {
let [a1, a0] = a;
let [p1, p2] = twoSquare(a1);
return fast2Sum(p1, p2 + 2 * a1 * a0);
}
function ddcPow(q, n) {
if (n === 1) return q;
if (n === 2) return ddcSquare(q);
if (n === 3) return ddcMul(ddcSquare(q), q);
let result = [1, 0, 0, 0];
let base = q;
while (n > 0) {
if (n % 2 === 1) { result = ddcMul(result, base); }
base = ddcSquare(base);
n = Math.floor(n / 2);
}
return result;
}
function ddNegate(a) {
let [a1, a0] = a;
return [-a1, -a0];
}
function ddSub(a, b) {
return ddAdd(a, ddNegate(b));
}
function qdDiv(a, b) {
let reciprocal = ddReciprocal(b);
return ddMul(a, reciprocal);
}
function ddReciprocal(b, iters = 2) {
if (b[0] === 0 && b[1] === 0) {
return [NaN, 0]; // Return NaN for division by zero
}
// Refine the approximation using Newton-Raphson iteration
let x = [1 / b[0], 0];
for (let i = 0; i < iters; i++) {
x = ddMul(x, ddSub([2, 0], ddMul(x, b)));
}
return x;
}
// Complex operations in DD precision
function ddcAdd(a, b) {
let realSum = ddAdd([a[0], a[1]], [b[0], b[1]]);
let imagSum = ddAdd([a[2], a[3]], [b[2], b[3]]);
return [realSum[0], realSum[1], imagSum[0], imagSum[1]];
}
// Complex subtraction in DD precision
function ddcSub(a, b) {
let realDiff = ddSub([a[0], a[1]], [b[0], b[1]]);
let imagDiff = ddSub([a[2], a[3]], [b[2], b[3]]);
return [realDiff[0], realDiff[1], imagDiff[0], imagDiff[1]];
}
// Complex multiplication in DD precision
function ddcMul(a, b) {
let ac = ddMul([a[0], a[1]], [b[0], b[1]]);
let bd = ddMul([a[2], a[3]], [b[2], b[3]]);
let adbc = ddMul(ddAdd([a[0], a[1]], [a[2], a[3]]), ddAdd([b[0], b[1]], [b[2], b[3]]));
let real = ddSub(ac, bd);
let imag = ddSub(adbc, ddAdd(ac, bd));
return [real[0], real[1], imag[0], imag[1]];
}
// Complex doubling in DD precision
function ddcDouble(a) {
let realDouble = ddDouble([a[0], a[1]]);
let imagDouble = ddDouble([a[2], a[3]]);
return [realDouble[0], realDouble[1], imagDouble[0], imagDouble[1]];
}
// Complex squaring in DD precision
function ddcSquare(a) {
let a0a0 = ddSquare([a[0], a[1]]);
let a1a1 = ddSquare([a[2], a[3]]);
let a0a1 = ddMul([a[0], a[1]], [a[2], a[3]]);
let real = ddSub(a0a0, a1a1);
let imag = ddDouble(a0a1);
return [real[0], real[1], imag[0], imag[1]];
}
// Complex absolute value in DD precision
function ddcAbs(a) {
let a0a0 = ddSquare([a[0], a[1]]);
let a1a1 = ddSquare([a[2], a[3]]);
return ddAdd(a0a0, a1a1);
}
function qdParse(s) {
s = s.trim().toLowerCase();
if (s === "infinity" || s === "+infinity") return [Infinity, 0];
if (s === "-infinity") return [-Infinity, 0];
if (s === "nan") return [NaN, 0];
let sign = 1;
if (s[0] === '-') {
sign = -1;
s = s.slice(1);
} else if (s[0] === '+') {
s = s.slice(1);
}
let e = 0;
let parts = s.split('e');
if (parts.length > 1) {
s = parts[0];
e = parseInt(parts[1]);
}
let decimalPos = s.indexOf('.');
if (decimalPos !== -1) {
s = s.replace('.', '');
e -= s.length - decimalPos;
}
s = s.replace(/^0+/, '');
if (s === '') return [0, 0];
let result = [0, 0];
for (let digit of s) {
result = ddAdd(ddMul(result, [10, 0]), [parseInt(digit), 0]);
}
if (e !== 0) {
result = ddMul(result, qdPow10(e));
}
if (sign === -1) {
result = ddNegate(result);
}
return result;
}
function qdPow10(e) {
if (e < 0) return ddReciprocal(qdPow10(-e));
// Up to 1e16, the second component is zero.
if (e <= 16) { return [10 ** e, 0]; }
if (e % 2) { return ddMul([1e15, 0], qdPow10(e - 15)) };
return ddSquare(qdPow10(e / 2));
}
function qdFloor(q) {
let [a, b] = q;
let fl = Math.floor(a);
let [r0, r1] = ddAdd([a, b], [-fl, 0]);
if (r0 < 0 || (r0 === 0 && r1 < 0)) {
fl -= 1;
}
return [fl, 0];
}
function ddCompare(a, b) {
if (a[0] < b[0]) return -1;
if (a[0] > b[0]) return 1;
if (a[1] < b[1]) return -1;
if (a[1] > b[1]) return 1;
return 0;
}
function qdCompare(a, b) {
if (a[0] < b[0]) return -1;
if (a[0] > b[0]) return 1;
if (a[1] < b[1]) return -1;
if (a[1] > b[1]) return 1;
if (a[2] < b[2]) return -1;
if (a[2] > b[2]) return 1;
if (a[3] < b[3]) return -1;
if (a[3] > b[3]) return 1;
return 0;
}
function qdLt(a, s) {
return a[0] < s || (a[0] == s && a[1] < 0);
}
function ddEq(a, s) {
return a[0] == s && a[1] == 0;
}
function qdEq(a, s) {
return a[0] === s && a[1] === 0 && a[2] === 0 && a[3] === 0;
}
function qdAbs(q) {
return (q[0] + q[1] < 0) ? ddNegate(q) : q;
}
function qdFixed(q, digits = 0) {
// With fixed-point notation, digits specifies the number of digits after the decimal point.
return qdFormat(q, digits, true);
}
const ddTen = [10, 0];
function qdFormat(q, digits = 'auto', useFixedPoint = false) {
// With scientific notation, digits specifies the number of significant digits.
let [a, b] = q;
let s = a < 0 || (a === 0 && b < 0) ? '-' : '';
let autoFormat = (digits == 'auto');
if (autoFormat) {
digits = 32;
}
q = qdAbs(q);
if (!isFinite(a) || !isFinite(b)) return a.toString();
if (a === 0 && b === 0) {
if (autoFormat) return '0';
if (useFixedPoint) digits += 1;
return '0' + (digits > 1 ? '.' + '0'.repeat(digits - 1) : '');
}
// Scale q to be between 1 and 10
let e = parseInt(q[0].toExponential().match(/e([+-]\d+)/)[1]);
if (ddCompare(q, qdPow10(e)) < 0) { e -= 1; }
if (ddCompare(q, qdPow10(1+e)) >= 0) { e += 1; }
if (e) { q = ddMul(q, qdPow10(-e)); }
if (!qdLt(q, 10)) { // Hit boundary condition
q = qdDiv(q, ddTen);
e += 1;
}
let result = '';
let nonzeroDigits = digits;
if (useFixedPoint && !autoFormat) {
nonzeroDigits = nonzeroDigits + e + 1;
}
for (let i = 0; i < nonzeroDigits + 1; i++) {
let floorQ = qdFloor(q);
let digit = floorQ[0];
result += digit;
q = ddSub(q, floorQ);
q = ddMul(q, ddTen);
}
// Rounding
if (nonzeroDigits < 0) {
// No nonzero signficant digits? Treat as zero.
result = '?';
nonzeroDigits = 0;
e = 0;
} else if (parseInt(result[nonzeroDigits]) >= 5) {
result = result.slice(0, nonzeroDigits) + '?';
let carry = 1;
for (let i = nonzeroDigits - 1; i >= 0; i--) {
let digit = parseInt(result[i]) + carry;
if (digit < 10) {
result = result.slice(0, i) + digit + result.slice(i + 1);
carry = 0;
break;
} else {
result = result.slice(0, i) + '0' + result.slice(i + 1);
carry = 1;
}
}
if (carry) {
result = '1' + result;
nonzeroDigits += 1;
e++;
}
}
result = result.slice(0, nonzeroDigits);
epart = '';
if (!useFixedPoint && (!autoFormat || e < -6 || e >= 32)) {
// Scientific notation
if (result.length > 1) {
result = result[0] + '.' + result.slice(1);
}
if (e !== 0) {
epart = 'e' + e;
}
} else {
// Fixed-point notation
if (e >= 0) {
result = result.padEnd(e + 1, '0');
result = result.slice(0, e + 1) + '.' + result.slice(e + 1);
} else {
result = '0.' + '0'.repeat(-e - 1) + result;
}
// Ensure the correct number of digits after the decimal point
let [intPart, fracPart] = result.split('.');
if (digits <= 0) {
result = intPart;
} else {
fracPart = (fracPart || '').padEnd(digits, '0');
result = intPart + '.' + fracPart;
}
}
if (autoFormat) {
// Auto digit selection: trim trailing zeros
result = result.replace(/\.0+$|(\.\d*[1-9])0+$/, "$1");
}
return s + result + epart;
}
// Array in-place DD precision, allows fast computation
// by avoiding array constructors
function Afast2Sum(r, i, a, b) {
let s = a + b;
r[i] = s;
r[i+1] = b - (s - a);
}
function Aslow2Sum(r, i, a, b) {
let s = a + b;
let c = s - a;
r[i] = s;
r[i+1] = (a - (s - c)) + (b - c);
}
function ArddSplit(r, i, a) {
const c = (134217729) * a; // 2^27 + 1, Veltkamp-Dekker constant
const x = c - (c - a);
const y = a - x;
r[i] = x;
r[i+1] = y;
}
function AtwoProduct(r, i, a, b) {
const p = a * b;
ArddSplit(r, i, a);
const ah = r[i];
const al = r[i+1];
ArddSplit(r, i, b);
const bh = r[i];
const bl = r[i+1];
const err = ((ah * bh - p) + ah * bl + al * bh) + al * bl;
r[i] = p;
r[i+1] = err;
}
function AtwoSquare(r, i, a) {
const p = a * a;
ArddSplit(r, i, a);
const ah = r[i];
const al = r[i+1];
const err = ((ah * ah - p) + 2 * ah * al) + al * al;
r[i] = p;
r[i+1] = err;
}
function AquickTwoSum(a, b) {
const s = a + b;
return [s, b - (s - a)];
}
function AsymmetricTwoSum(a, b) {
const s = a + b;
const bb = s - a;
return [s, (a - (s - bb)) + (b - bb)];
}
// Three-sum from QD library: combines three values with proper error propagation
// Returns [sum, err1, err2] where sum + err1 + err2 = a + b + c (exactly)
function ArqdThreeSum(a, b, c) {
let [t1, t2] = AsymmetricTwoSum(a, b);
let [newA, t3] = AsymmetricTwoSum(c, t1);
let [newB, newC] = AsymmetricTwoSum(t2, t3);
return [newA, newB, newC];
}
function ArddAdd(r, i, a1, a2, b1, b2) {
Aslow2Sum(r, i, a1, b1);
const h1 = r[i];
const h2 = r[i+1];
Aslow2Sum(r, i, a2, b2);
const l1 = r[i];
const l2 = r[i+1];
Afast2Sum(r, i, h1, h2 + l1);
const v1 = r[i];
const v2 = r[i+1];
Afast2Sum(r, i, v1, v2 + l2);
}
function ArddMul(r, i, a1, a2, b1, b2) {
AtwoProduct(r, i, a1, b1);
const p1 = r[i];
const p2 = r[i+1];
Afast2Sum(r, i, p1, p2 + a1 * b2 + b1 * a2);
}
function ArddSet(r, i, a1, a2) {
r[i] = a1;
r[i+1] = a2;
}
function ArddcCopy(r, i, s, si) {
r[i] = s[si];
r[i+1] = s[si+1];
r[i+2] = s[si+2];
r[i+3] = s[si+3];
}
function ArddcGet(s, si) {
return s.slice(si, si+4);
}
function ArddSquare(r, i, a1, a2) {
AtwoSquare(r, i, a1);
const p1 = r[i];
const p2 = r[i+1];
Afast2Sum(r, i, p1, p2 + 2 * a1 * a2);
}
function ArddAbsSub(r, i, a1, a2, b1, b2) {
ArddAdd(r, i, a1, a2, -b1, -b2);
if (r[i] + r[i+1] < 0) {
r[i] = -r[i];
r[i+1] = -r[i+1];
}
}
// Oct-double (four-limb) helpers for deeper precision
function ArqdTwoProduct(a, b) {
const tmp = [];
AtwoProduct(tmp, 0, a, b);
return [tmp[0], tmp[1]];
}
function ArqdTwoSquare(a) {
const tmp = [];
AtwoSquare(tmp, 0, a);
return [tmp[0], tmp[1]];
}
// Renormalization algorithm from QD library (Hida, Li, Bailey)
// Takes 5 inputs and produces 4 normalized outputs
function ArqdRenorm(r, i, c0, c1, c2, c3, c4) {
let t0, t1, t2, t3, s;
// First pass: propagate from bottom to top, collecting errors
[s, t3] = AquickTwoSum(c3, c4);
[s, t2] = AquickTwoSum(c2, s);
[s, t1] = AquickTwoSum(c1, s);
[c0, t0] = AquickTwoSum(c0, s);
// Second pass: combine error terms
[s, t2] = AquickTwoSum(t2, t3);
[s, t1] = AquickTwoSum(t1, s);
[c1, t0] = AquickTwoSum(t0, s);
// Third pass: final cleanup
[s, t1] = AquickTwoSum(t1, t2);
[c2, t0] = AquickTwoSum(t0, s);
c3 = t0 + t1;
ArqdSet(r, i, c0, c1, c2, c3);
}
function ArqdSet(r, i, a1, a2, a3, a4) {
r[i] = a1;
r[i+1] = a2;
r[i+2] = a3;
r[i+3] = a4;
}
function ArqdcCopy(r, i, s, si) {
r[i] = s[si];
r[i+1] = s[si+1];
r[i+2] = s[si+2];
r[i+3] = s[si+3];
r[i+4] = s[si+4];
r[i+5] = s[si+5];
r[i+6] = s[si+6];
r[i+7] = s[si+7];
}
function ArqdcGet(s, si) {
return s.slice(si, si+8);
}
function ArqdAdd(r, i, a1, a2, a3, a4, b1, b2, b3, b4) {
let s0, s1, s2, s3, s4, t0, t1, t2;
[s0, t0] = AsymmetricTwoSum(a1, b1);
[s1, t1] = AsymmetricTwoSum(a2, b2);
[s2, t2] = AsymmetricTwoSum(a3, b3);
s3 = a4 + b4;
[s1, t0] = AsymmetricTwoSum(s1, t0);
[s2, t1] = AsymmetricTwoSum(s2, t1);
s3 += t2;
[s2, t0] = AsymmetricTwoSum(s2, t0);
s3 += t1;
[s3, s4] = AsymmetricTwoSum(s3, t0);
[s0, s1] = AquickTwoSum(s0, s1);
[s1, s2] = AquickTwoSum(s1, s2);
[s2, s3] = AquickTwoSum(s2, s3);
[s3, s4] = AquickTwoSum(s3, s4);
s2 += s4;
[s2, s3] = AquickTwoSum(s2, s3);
[s1, s2] = AquickTwoSum(s1, s2);
ArqdSet(r, i, s0, s1, s2, s3);
}
// QD library sloppy_mul algorithm (Hida, Li, Bailey)
// Uses TwoProduct for 6 products (levels 0-2) for nearly full 212-bit precision
function ArqdMul(r, i, a1, a2, a3, a4, b1, b2, b3, b4) {
// Level 0: a[0]*b[0]
let [p0, q0] = ArqdTwoProduct(a1, b1);
// Level 1: a[0]*b[1], a[1]*b[0]
let [p1, q1] = ArqdTwoProduct(a1, b2);
let [p2, q2] = ArqdTwoProduct(a2, b1);
// Level 2: a[0]*b[2], a[1]*b[1], a[2]*b[0]
let [p3, q3] = ArqdTwoProduct(a1, b3);
let [p4, q4] = ArqdTwoProduct(a2, b2);
let [p5, q5] = ArqdTwoProduct(a3, b1);
// Accumulate terms using three_sum
[p1, p2, q0] = ArqdThreeSum(p1, p2, q0);
[p2, q1, q2] = ArqdThreeSum(p2, q1, q2);
[p3, p4, p5] = ArqdThreeSum(p3, p4, p5);
// Further combining
let [s0, t0] = AsymmetricTwoSum(p2, p3);
let [s1, t1] = AsymmetricTwoSum(q1, p4);
let s2 = q2 + p5;
[s1, t0] = AsymmetricTwoSum(s1, t0);
s2 += t0 + t1;
// Level 3 cross-products and remaining error terms
s1 += a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + q0 + q3 + q4 + q5;
// Renormalize
ArqdRenorm(r, i, p0, p1, s0, s1, s2);
}
// QD library optimized sqr algorithm (Hida, Li, Bailey)
// Exploits symmetry: a[i]*a[j] = a[j]*a[i], so compute once and double
// Uses TwoSquare (cheaper than TwoProduct) where possible
function ArqdSquare(r, i, a1, a2, a3, a4) {
// Level 0: a[0]² - use TwoSquare
let [p0, q0] = ArqdTwoSquare(a1);
// Level 1: 2*a[0]*a[1] - single TwoProduct with doubled arg
let [p1, q1] = ArqdTwoProduct(2 * a1, a2);
// Level 2: 2*a[0]*a[2] + a[1]²
let [p2, q2] = ArqdTwoProduct(2 * a1, a3);
let [p3, q3] = ArqdTwoSquare(a2);
// Accumulate level 1
[p1, p2, q0] = ArqdThreeSum(p1, q0, p2);
// Accumulate level 2
[p2, q1, q2] = ArqdThreeSum(p2, q1, q2);
[p3, q3] = AsymmetricTwoSum(p3, q3);
// Combine accumulated values
let [s0, t0] = AsymmetricTwoSum(p2, p3);
let [s1, t1] = AsymmetricTwoSum(q1, q3);
let s2 = q2;
[s1, t0] = AsymmetricTwoSum(s1, t0);
s2 += t0 + t1;
// Level 3: 2*a[0]*a[3] + 2*a[1]*a[2] (plain multiply)
s1 += 2 * a1 * a4 + 2 * a2 * a3 + q0;
// Renormalize
ArqdRenorm(r, i, p0, p1, s0, s1, s2);
}
// Oct-precision absolute difference: r = |a - b|
// Used for cycle detection in QDPerturbationBoard
function ArqdAbsSub(r, i, a1, a2, a3, a4, b1, b2, b3, b4) {
ArqdAdd(r, i, a1, a2, a3, a4, -b1, -b2, -b3, -b4);
if (r[i] + r[i+1] + r[i+2] + r[i+3] < 0) {
r[i] = -r[i];
r[i+1] = -r[i+1];
r[i+2] = -r[i+2];
r[i+3] = -r[i+3];
}
}
// Additional shared utility function for tracking cycles.
function fibonacciPeriod(iteration) {
// Returns 1 at Fibonacci checkpoints (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...)
// Otherwise returns distance from most recent Fibonacci checkpoint plus 1.
// Fibonacci growth (φ ≈ 1.618) reduces aliasing with periods compared to power-of-2.
if (iteration === 0) return 1;
if (iteration === 1) return 1;
// Find largest Fibonacci number <= iteration
let a = 1, b = 1;
while (b < iteration) {
const next = a + b;
a = b;
b = next;
}
// Return 1 if iteration is exact Fibonacci, else distance from previous
if (b === iteration) return 1;
return iteration - a + 1;
}
// Catmull-Rom spline interpolation for smooth movie camera paths.
// Uses quad-double precision for numerical stability at deep zoom.
function catmullRom1DQD(p0, p1, p2, p3, t) {
const t2 = t * t;
const t3 = t2 * t;
const c0 = (-t3 + 2*t2 - t) / 2;
const c2 = (-3*t3 + 4*t2 + t) / 2;
const c3 = (t3 - t2) / 2;
// Compute offsets from p1 for numerical stability
const s0 = toQDSub(p0, p1);
const s2 = toQDSub(p2, p1);
const s3 = toQDSub(p3, p1);
return toQDAdd(toQDAdd(toQDAdd(toQDScale(s0, c0),
toQDScale(s3, c3)), toQDScale(s2, c2)), p1);
}
function catmullRomSplineQD(p0, p1, p2, p3, t) {
return [
catmullRom1DQD(p0[0], p1[0], p2[0], p3[0], t),
catmullRom1DQD(p0[1], p1[1], p2[1], p3[1], t)
];
}
|