Guest User

Untitled

a guest
Dec 27th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. const splitter = 134217729;
  2.  
  3. @inline
  4. function twoSum(a: number, b: number): Double {
  5. let s = a + b;
  6. let a1 = s - b;
  7. return new Double(s, (a - a1) + (b - (s - a1)));
  8. }
  9. @inline
  10. function twoProd(a: number, b: number): Double {
  11. let t = splitter * a;
  12. let ah = t + (a - t), al = a - ah;
  13. t = splitter * b;
  14. let bh = t + (b - t), bl = b - bh;
  15. t = a * b;
  16. return new Double(t, ((ah * bh - t) + ah * bl + al * bh) + al * bl);
  17. }
  18. @inline
  19. function oneSqr(a: number): Double {
  20. let t = splitter * a;
  21. let ah = t + (a - t), al = a - ah;
  22. t = a * a;
  23. let hl = al * ah;
  24. return new Double(t, ((ah * ah - t) + hl + hl) + al * al);
  25. }
  26.  
  27. class Double {
  28. constructor(
  29. public hi: number,
  30. public lo: number
  31. ) { }
  32.  
  33. static clone(X: Double): Double { return new Double(X.hi, X.lo); }
  34.  
  35. toNumber(): number {
  36. return this.hi + this.lo;
  37. }
  38.  
  39. abs(): number {
  40. return Math.abs(this.hi + this.lo);
  41. }
  42.  
  43. lt(f: number): boolean {
  44. return this.hi + this.lo < f;
  45. }
  46.  
  47. add(other: Double): Double {
  48. return Double.add22(Double.clone(this), other);
  49. }
  50. sub(other: Double): Double {
  51. return Double.sub22(Double.clone(this), other);
  52. }
  53. mul(other: Double): Double {
  54. return Double.mul22(Double.clone(this), other);
  55. }
  56. div(other: Double): Double {
  57. return Double.div22(Double.clone(this), other);
  58. }
  59. sqr(): Double {
  60. return Double.sqr2(Double.clone(this));
  61. }
  62.  
  63. static add22(X: Double, Y: Double): Double {
  64. let S = twoSum(X.hi, Y.hi);
  65. let E = twoSum(X.lo, Y.lo);
  66. let c = S.lo + E.hi;
  67. let vh = S.hi + c, vl = c - (vh - S.hi);
  68. c = vl + E.lo;
  69. X.hi = vh + c;
  70. X.lo = c - (X.hi - vh);
  71. return X;
  72. }
  73.  
  74. static sub22(X: Double, Y: Double): Double {
  75. let S = twoSum(X.hi, -Y.hi);
  76. let E = twoSum(X.lo, -Y.lo);
  77. let c = S.lo + E.hi;
  78. let vh = S.hi + c, vl = S.hi - vh + c;
  79. let w = vl + E.lo;
  80. X.hi = vh + w;
  81. X.lo = w - (X.hi - vh);
  82. return X;
  83. }
  84.  
  85. static mul22(X: Double, Y: Double): Double {
  86. let S = twoProd(X.hi, Y.hi);
  87. S.lo += X.hi * Y.lo + X.lo * Y.hi;
  88. X.hi = S.hi + S.lo;
  89. X.lo = S.lo - (X.hi - S.hi);
  90. return X;
  91. }
  92.  
  93. static div22(X: Double, Y: Double): Double {
  94. let s = X.hi / Y.hi;
  95. let T = twoProd(s, Y.hi);
  96. let e = (X.hi - T.hi - T.lo + X.lo - s * Y.lo) / Y.hi;
  97. X.hi = s + e;
  98. X.lo = e - (X.hi - s);
  99. return X;
  100. }
  101.  
  102. static sqr2(X: Double): Double {
  103. let S = oneSqr(X.hi);
  104. let c = X.hi * X.lo;
  105. S.lo += c + c;
  106. X.hi = S.hi + S.lo;
  107. X.lo = S.lo - (X.hi - S.hi);
  108. return X;
  109. }
  110. }
  111.  
  112. export function test(Eh: number, El: number, Lh: number, Ll: number): number {
  113. let result: number = 0;
  114. const e = new Double(Eh, El), log2 = new Double(Lh, Ll);
  115. let diff = e.add(log2).sub(log2).sub(e).abs();
  116. if (diff > 1e-30) result += 1;
  117. diff = e.mul(log2).div(log2).sub(e).abs();
  118. if (diff > 1e-30) result += 2;
  119. return result;
  120. }
  121.  
  122. export function mandelbrot(maxIteration: number, width: number, height: number,
  123. tx0: number, ty0: number, tdx0: number, tdy0: number, i: number, j: number): number {
  124. let x = new Double(0, 0), y = new Double(0, 0);
  125. let xx = new Double(0, 0), xy = new Double(0, 0), yy = new Double(0, 0);
  126. let tx = new Double(tx0, 0), ty = new Double(ty0, 0);
  127. let tdx = new Double(tdx0, 0), tdy = new Double(tdy0, 0);
  128. let cx = tx.sub(tdx).add(tdx.mul(new Double(2 * i, 0)).div(new Double(width, 0)));
  129. let cy = ty.add(tdy).sub(tdy.mul(new Double(2 * j, 0)).div(new Double(height, 0)));
  130. let iteration: number = 0;
  131. while (iteration++ < maxIteration && xx.add(yy).lt(4)) {
  132. x = xx.sub(yy).add(cx);
  133. y = xy.add(xy).add(cy);
  134. xx = x.sqr();
  135. yy = y.sqr();
  136. xy = x.mul(y);
  137. }
  138. return iteration;
  139. }
  140. © 2019 GitHub, Inc.
Advertisement
Add Comment
Please, Sign In to add comment