Debian Codegraph @

rhino/testsrc/tests/js1_7/extensions/regress-363040-02.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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 var gTestfile = 'regress-363040-02.js';
7 //-----------------------------------------------------------------------------
8 var BUGNUMBER = 363040;
9 var summary = 'Array.prototype.reduce application in continued fraction';
10 var actual = '';
11 var expect = '';
12
13
14 //-----------------------------------------------------------------------------
15 test();
16 //-----------------------------------------------------------------------------
17
18 function test()
19 {
20 enterFunc ('test');
21 printBugNumber(BUGNUMBER);
22 printStatus (summary);
23
24
25 // Print x as a continued fraction in compact abbreviated notation and return
26 // the convergent [n, d] such that x - (n / d) <= epsilon.
27 function contfrac(x, epsilon) {
28 let i = Math.floor(x);
29 let a = [i];
30 x = x - i;
31 let maxerr = x;
32 while (maxerr > epsilon) {
33 x = 1 / x;
34 i = Math.floor(x);
35 a.push(i);
36 x = x - i;
37 maxerr = x * maxerr / i;
38 }
39 print(uneval(a));
40 return a.reduceRight(function (x, y) {return [x[0] * y + x[1], x[0]];}, [1, 0]);
41 }
42
43 if (!Array.prototype.reduceRight)
44 {
45 print('Test skipped. Array.prototype.reduceRight not implemented');
46 }
47 else
48 {
49 // Show contfrac in action on some interesting numbers.
50 for each (num in [Math.PI, Math.sqrt(2), 1 / (Math.sqrt(Math.E) - 1)]) {
51 print('Continued fractions for', num);
52 for each (eps in [1e-2, 1e-3, 1e-5, 1e-7, 1e-10]) {
53 let frac = contfrac(num, eps);
54 let est = frac[0] / frac[1];
55 let err = num - est;
56 print(uneval(frac), est, err);
57 }
58 print();
59 }
60 }
61
62 reportCompare(expect, actual, summary);
63
64 exitFunc ('test');
65 }