Trigonometric Functions in Standard C Library

«
»
Question: What are Various Trigonometric Functions in Standard C Library?

Answer: Following trigonometric functions are defined in ‘math.h’ header.

    double sin(double angle);
    double cos(double angle);
    double tan(double angle);
    double asin(double angle);
    double acos(double angle);
    double atan(double angle);
    double atan2(double x, double y);

Recall that PI radians equals 180 degrees. Functions ‘sin()’, ‘cos()’ and ‘tan()’ take angles, in radians, as arguments and return their sine, cosine and tangent respectively. Functions ‘asin()’, ‘acos()’ and ‘atan()’ return their arc sine, arc cosine and arc tangent respectively. Remember that domain error occurs when arguments to ‘asin()’ or ‘acos()’ isn’t in the range -1 to 1. ‘asin()’ and ‘atan()’ returns a value in the range -PI/2 to PI/2 radians, and ‘acos()’ returns value in range 0 to PI radians.

‘atan2()’ function returns arc tangent of expression x/y but uses the signs of both arguments to determine which quadrant the result lies within. It returns a result in range -PI to PI radians.

Let’s consider a simple C program to learn using ‘sin()’ function,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
/* fp_sin_func.c -- trigonometric function 'sin()' returns double */
/* value for angle */
#include <stdio.h>
#include <stdlib.h>
#include <math.h> /* trigonometric functions defined */
 
int main(void)
{
    double angle, sine;
 
    printf("User, write in angle, in radians, whose sine to be "
            "computed...\n");
    scanf("%lf", &angle);
 
    /* calling sin() to compute sine of angle argument */
    sine = sin(angle);
 
    printf("angle: %lf ,in radians, converted to: %lf sine\n",
            angle, sine);
 
    return 0;
}

Notice output, for several arbitrary values, below

User, write in angle, in radians, whose sine to be computed...
4.5
angle: 4.500000 ,in radians, converted to: -0.977530 sine
 
User, write in angle, in radians, whose sine to be computed...
5.0
angle: 5.000000 ,in radians, converted to: -0.958924 sine
 
User, write in angle, in radians, whose sine to be computed...
4.0
angle: 4.000000 ,in radians, converted to: -0.756802 sine
 
User, write in angle, in radians, whose sine to be computed...
1000000000000000000000
angle: 1000000000000000000000.000000 ,in radians, converted to:
 -0.667120 sine

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.