| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
var gTestfile = 'regress-363040-01.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 |
|
| 26 |
function contfrac(x, epsilon) { |
| 27 |
let i = Math.floor(x); |
| 28 |
let a = [i]; |
| 29 |
x = x - i; |
| 30 |
let maxerr = x; |
| 31 |
while (maxerr > epsilon) { |
| 32 |
x = 1 / x; |
| 33 |
i = Math.floor(x); |
| 34 |
a.push(i); |
| 35 |
x = x - i; |
| 36 |
maxerr = x * maxerr / i; |
| 37 |
} |
| 38 |
print(uneval(a)); |
| 39 |
a.push([1, 0]); |
| 40 |
a.reverse(); |
| 41 |
return a.reduce(function (x, y) {return [x[0] * y + x[1], x[0]];}); |
| 42 |
} |
| 43 |
|
| 44 |
if (!Array.prototype.reduce) |
| 45 |
{ |
| 46 |
print('Test skipped. Array.prototype.reduce not implemented'); |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
|
| 51 |
for each (num in [Math.PI, Math.sqrt(2), 1 / (Math.sqrt(Math.E) - 1)]) { |
| 52 |
print('Continued fractions for', num); |
| 53 |
for each (eps in [1e-2, 1e-3, 1e-5, 1e-7, 1e-10]) { |
| 54 |
let frac = contfrac(num, eps); |
| 55 |
let est = frac[0] / frac[1]; |
| 56 |
let err = num - est; |
| 57 |
print(uneval(frac), est, err); |
| 58 |
} |
| 59 |
print(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
reportCompare(expect, actual, summary); |
| 64 |
|
| 65 |
exitFunc ('test'); |
| 66 |
} |