C Language sin() Function Guide: Basics, Applications, and Custom Implementation

1. Basics of the sin Function in C Language

In C language, you can perform trigonometric calculations by using the standard library math.h. Among them, the sin function is used to calculate the sine of an angle. In this article, we will explain in detail how to use the sin function in C, its applications, and how to implement your own version of the sin function.

1.1 What is the sin Function?

The sin function takes an angle in radians as input and returns the sine value of that angle. A radian is a unit of measurement for angles based on the mathematical constant π (pi). To convert radians to degrees, or vice versa, you need to apply a conversion formula.

2. Basics of the math.h Library in C

To use trigonometric functions, include math.h. This allows you to use the sin function and other mathematical functions.

#include <stdio.h>
#include <math.h>

int main() {
    double angle = 1.57; // Radians equivalent to 90 degrees
    double result = sin(angle);
    printf("sin(1.57) = %f\n", result);
    return 0;
}

In this code, the sine of 90 degrees (1.57 radians) is output as 1.000000.

2.1 Converting Between Degrees and Radians

To convert degrees to radians, use the following formula:

#define DEG_TO_RAD(deg)  ((deg) / 180.0 * 3.141592653589793)

int main(void) {
    double deg = 90.0;
    double rad = DEG_TO_RAD(deg);
    printf("sin(%f degrees) = %f\n", deg, sin(rad));
    return 0;
}

This program converts 90 degrees to radians and then uses that value in the sin function.


3. Application Example: Generating a Sine Wave

A sine wave is a common waveform used in audio synthesis and signal processing. The following code generates a sine wave and outputs the value of each sample:

#include <stdio.h>
#include <math.h>

int main() {
    int samples = 100;
    double frequency = 1.0;
    double amplitude = 1.0;
    double phase = 0.0;
    double sampleRate = 100.0;

    for (int i = 0; i < samples; i++) {
        double t = i / sampleRate;
        double value = amplitude * sin(2 * M_PI * frequency * t + phase);
        printf("Sample %d: %f\n", i, value);
    }
    return 0;
}

This program generates a sine wave using the specified frequency and sample rate. The generated data can be used in audio or other signal processing tasks.

4. Implementing Your Own sin Function: Maclaurin Series

It is possible to implement your own sin function in C. This can be useful when the standard library is unavailable or when custom behavior is required. The Maclaurin series approximation expresses the sine function as a polynomial.

4.1 sin Function Using Maclaurin Series

The Maclaurin series approximation for the sine function is as follows:

 \sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \dots

Here’s a C implementation based on this formula:

#include <stdio.h>
#include <math.h>

double factorial(int n) {
    double result = 1.0;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

double my_sin(double x) {
    double result = 0.0;
    for (int i = 0; i < 10; i++) {  // Calculate up to 10 terms
        int power = 2 * i + 1;
        double term = pow(x, power) / factorial(power);
        if (i % 2 == 0) {
            result += term;
        } else {
            result -= term;
        }
    }
    return result;
}

int main() {
    double angle = 1.57;
    printf("sin(1.57) = %f\n", my_sin(angle));
    return 0;
}

This program calculates the sine function using the Maclaurin series. The accuracy depends on the number of terms calculated, and around 10 terms are generally sufficient for good precision.

5. Errors and Considerations

When working with floating-point numbers, precision issues can arise when dealing with very small or very large values. In particular, for custom sin function implementations, increasing the number of terms also increases computation cost. Additionally, with the Maclaurin series, calculation accuracy decreases for large angles (e.g., greater than ±π), so it is recommended to normalize angles to an appropriate range when necessary.

6. Summary

In this article, we covered the basics of using the sin function in C, practical applications, and how to implement your own version of sin. Utilizing trigonometric functions in C allows for applications in fields such as physics simulations and audio processing. Implementing your own function with the Maclaurin series also helps you gain a deeper understanding of how the function works. We hope this will be useful in your projects.

侍エンジニア塾