How to compute sin and cos together in C

1 Answer

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

/* 
   This function receives an angle in radians and computes both sine and cosine.
   Returning the results through pointer parameters keeps the interface simple
   and avoids unnecessary structures. This is a common and efficient pattern in C.
*/
void compute_sin_cos(double radians, double *out_sin, double *out_cos) {
    /* Use the standard math library's trigonometric functions.
       These are optimized and provide correct results across platforms. */
    *out_sin = sin(radians);
    *out_cos = cos(radians);
}

int main(void) {
    double degrees = 60.0;

    /* Convert degrees to radians using the standard formula.
       M_PI is provided by <math.h> on many systems; if not, define your own. */
    double radians = degrees * (M_PI / 180.0);

    double s, c;

    /* Compute both sine and cosine using our helper function. */
    compute_sin_cos(radians, &s, &c);

    /* Display the results. */
    printf("Angle: %.1f degrees\n", degrees);
    printf("Radians: %.6f\n", radians);
    printf("sin: %.6f\n", s);
    printf("cos: %.6f\n", c);

    return 0;
}



/*
run:

Angle: 60.0 degrees
Radians: 1.047198
sin: 0.866025
cos: 0.500000

*/

 



answered 2 days ago by avibootz
...