sin taylors series in javascript

index.tsx
// Function to calculate the sine value using Taylor series approximation
function sinTaylor(x, terms) {
    let result = 0;

    for (let i = 0; i < terms; i++) {
        result += Math.pow(-1, i) * Math.pow(x, 2 * i + 1) / factorial(2 * i + 1);
    }

    return result;
}

// Function to calculate the factorial
function factorial(num) {
    if (num === 0 || num === 1){
        return 1;
    } else {
        return num * factorial(num - 1);
    }
}

// Input values
const angleInRadians = Math.PI / 4; // Example angle in radians
const numberOfTerms = 10; // Number of terms to include in the Taylor series approximation

// Calculate sin value using Taylor series
const sinValue = sinTaylor(angleInRadians, numberOfTerms);

console.log(`sin(${angleInRadians}) using Taylor series with ${numberOfTerms} terms: ${sinValue}`);
830 chars
29 lines

related categories

gistlibby LogSnag