Skip to content
TheChatArchiveThe Chat Archive
Amazon Novaamazon.nova-pro-v1:0· AmazonHugging Face LMArena Human Preference 140K

Write a function to approximate sin function in C - do not use any runtime library!

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219-thinking-32k’s answer.
  1. User

    Write a function to approximate sin function in C - do not use any runtime library!

  2. Amazon Nova Assistant

    Certainly! To approximate the sine function without using any runtime library, we can use a Taylor series expansion. The Taylor series for sine is given by:

    [ \sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \frac{x^9}{9!} - \cdots ]

    We can truncate this series after a certain number of terms to get an approximation. The more terms we include, the more accurate the approximation will be. Here's a simple implementation in C that approximates the sine function using the first few terms of the Taylor series:

    #include <stdio.h>
    
    // Function to calculate factorial
    unsigned long long factorial(int n) {
        unsigned long long result = 1;
        for (int i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }
    
    // Function to approximate sin(x) using Taylor series
    double sin_approx(double x, int terms) {
        double result = 0.0;
        for (int i = 0; i < terms; i++) {
            int power = 2 * i + 1;
            int sign = (i % 2 == 0) ? 1 : -1;
            result += sign * (pow(x, power) / factorial(power));
        }
        return result;
    }
    
    // Simple power function (since we can't use pow from math.h)
    double pow(double base, int exponent) {
        double result = 1.0;
        for (int i = 0; i < exponent; i++) {
            result *= base;
        }
        return result;
    }
    
    int main() {
        double x = 0.5; // Example value for x (in radians)
        int terms = 5;  // Number of terms to use in the Taylor series
    
        double result = sin_approx(x, terms);
        printf("sin(%f) ≈ %f\n", x, result);
    
        return 0;
    }
    

    Explanation:

    1. Factorial Function:
      • factorial(int n) computes the factorial of n.
    2. Power Function:
      • pow(double base, int exponent) computes base raised to the power of exponent. This is a simple implementation since we can't use the pow function from the math library.
    3. Sine Approximation Function:
      • sin_approx(double x, int terms) approximates the sine of x using the first terms of the Taylor series.
    4. Main Function:
      • In main, we set an example value for x and the number of terms to use in the approximation. We then print the approximated value of sin(x).

    Notes:

    • The accuracy of the approximation depends on the number of terms used in the Taylor series. More terms will give a more accurate result but will also require more computation.
    • This implementation assumes that x is in radians.
    • The pow function is a simple implementation and may not handle very large exponents efficiently.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.