SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { isArray, isString, isFunction } from "lodash";
  2.  
  3. const { performance } = require("perf_hooks");
  4.  
  5. type Parameters<Fn> = Fn extends (...args: infer T) => any ? T : never;
  6.  
  7. const trampoline = <Fn extends (...args: any[]) => any>(
  8.   fn: Fn,
  9.   ...args: Parameters<Fn>
  10. ) => {
  11.   let result = fn(...args);
  12.   let i: number = 0;
  13.   while (result instanceof Function) {
  14.     result = result();
  15.   }
  16.   return result as ReturnType<Fn>;
  17. };
  18.  
  19. const piOver4 = (precision: number) => {
  20.   const _piOver4 = (precision: number, current = 0, iteration = 0): number => {
  21.     const sign = iteration % 2 ? -1 : 1;
  22.     const nextApproximation = current + sign / (1 + 2 * iteration);
  23.     return Math.abs(current - nextApproximation) <= precision / 4
  24.       ? (nextApproximation as any)
  25.       : ((() => _piOver4(precision, nextApproximation, iteration + 1)) as any);
  26.   };
  27.   return trampoline(_piOver4, precision);
  28. };
  29.  
  30. const pi = (precision: number) => 4 * piOver4(precision);
  31.  
  32. const funcTimer = <Fn extends (...args: any[]) => any>(
  33.   fn: Fn,
  34.   ...args: Parameters<Fn>
  35. ) => {
  36.   let result: ReturnType<Fn>;
  37.   const start = performance.now();
  38.   result = fn(...args);
  39.   const end = performance.now();
  40.   console.log({
  41.     time: (end - start).toFixed(3) + "ms",
  42.     result
  43.   });
  44.   return result;
  45. };
  46.  
  47. const pi_while = (precision: number) => {
  48.   let approximation;
  49.   let nextApproximation = 0;
  50.   let iteration: number = 0;
  51.   do {
  52.     approximation = nextApproximation;
  53.     const sign = iteration % 2 ? -1 : 1;
  54.     const summand = sign / (1 + 2 * iteration);
  55.     nextApproximation = nextApproximation + summand;
  56.     iteration++;
  57.   } while (Math.abs(nextApproximation - approximation) > precision / 4);
  58.   return 4 * nextApproximation;
  59. };
  60.  
  61. const factorial = (n: number): number => (n === 0 ? 1 : n * factorial(n - 1));
  62.  
  63. const e_while = (precision: number) => {
  64.   let approximation: number;
  65.   let nextApproximation: number = 0;
  66.   let iteration = 0;
  67.   do {
  68.     approximation = nextApproximation;
  69.     const summand = 1 / factorial(iteration);
  70.     nextApproximation += summand;
  71.     iteration++;
  72.   } while (Math.abs(nextApproximation - approximation) > precision);
  73.   return nextApproximation;
  74. };
  75.  
  76. const timedPi = (precision: number) => funcTimer(pi, precision);
  77. const timedPi_while = (precision: number) => funcTimer(pi_while, precision);
  78. const timedE_while = (precision: number) => funcTimer(e_while, precision);
  79.  
  80. [
  81.   0.1,
  82.   0.01,
  83.   0.001,
  84.   0.0001,
  85.   0.00001,
  86.   0.000001,
  87.   0.0000001,
  88.   0.00000001,
  89.   0.000000001,
  90.   0.0000000001,
  91.   0.00000000001,
  92.   0.000000000001,
  93.   0.0000000000001,
  94.   0.00000000000001,
  95.   0.000000000000001,
  96.   0.0000000000000001,
  97.   0.00000000000000001
  98. ].forEach(timedE_while);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top