tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

pi-generator.js (1014B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 //-----------------------------------------------------------------------------
      7 var summary = "A (slow) generator of pi";
      8 var actual, expect;
      9 
     10 printStatus(summary);
     11 
     12 /**************
     13 * BEGIN TEST *
     14 **************/
     15 
     16 function* pi()
     17 {
     18  var val = 0;
     19  var curr = 1;
     20  var isNeg = false;
     21  while (true)
     22  {
     23    if (isNeg)
     24      yield val -= 4/curr;
     25    else
     26      yield val += 4/curr;
     27    curr += 2;
     28    isNeg = !isNeg;
     29  }
     30 }
     31 
     32 var failed = false;
     33 var it = pi();
     34 
     35 var vals =
     36  [4,
     37   4 - 4/3,
     38   4 - 4/3 + 4/5,
     39   4 - 4/3 + 4/5 - 4/7];
     40 
     41 try
     42 {
     43  for (var i = 0, sz = vals.length; i < sz; i++)
     44    if (it.next().value != vals[i])
     45      throw vals[i];
     46 }
     47 catch (e)
     48 {
     49  failed = e;
     50 }
     51 
     52 
     53 
     54 expect = false;
     55 actual = failed;
     56 
     57 reportCompare(expect, actual, summary);