Write a function to approximate sin function in C - do not use any runtime library!
- User
Write a function to approximate sin function in C - do not use any runtime library!
Assistant
c// Function to approximate sine using Taylor series double my_sin(double x) { // Constants const double PI = 3.14159265358979323846; const double TWO_PI = 6.28318530717958647692; // Reduce x to the range [-π, π] while (x > PI) { x -= TWO_PI; } while (x < -PI) { x += TWO_PI; } // Calculate sine using Taylor series double x2 = x * x; double term = x; double sum = x; // Add terms from the Taylor series // sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ... term = term * x2 / (2 * 3); sum -= term; term = term * x2 / (4 * 5); sum += term; term = term * x2 / (6 * 7); sum -= term; term = term * x2 / (8 * 9); sum += term; term = term * x2 / (10 * 11); sum -= term; return sum; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.