Debian Codegraph @

js/src/tests/non262/extensions/regress-363040-02.js

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