Hong-Phuc Bui
2025-01-16 2889de7f0c2d587a17fbd322af57c29e84238620
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * taylor-serie-exp.js
 *
 */
 
import {terminal} from "./dfhi.js";
 
window.main = function(...argv) {
    //const x = Number.parseFloat(argv[0]);
    //const x_rad = x * Math.PI / 180;
    const alpha = [0, 30, 45, 60, 90];
    alpha.map(x => x*Math.PI/180).forEach( a => {
        makeTest("Sinus", a, sin, Math.sin);
        makeTest("Cosinus", a, cos, Math.cos);
    });
 
};
 
 
function makeTest(testName, value, impl, ref) {
    const result = impl(value);
    const expected = ref(value);
    terminal.print(`${testName} result: ${result} expected : ${expected}\n`);
}
 
function sin(x) {
    let sum = x;
    let term = -(x**3 / 6);
    const xsqr = x*x;
    let g = 1;
    for(let i = 4; sum + term !== sum; i+=2) {
        sum = sum + term;
        g = i*i + i;
        term = - term * (xsqr/g);
    }
    return sum;
}
 
function cos(x) {
    let sum = 1;
    const xsqr = x*x;
    let g = 2;
    let term = -1 * (xsqr / g);
    for(let i = 3; sum + term !== sum; i+=2) {
        sum = sum + term;
        g = i*i + i;
        term = -1 * term * (xsqr/g);
    }
    return sum;
}