Trigonometric, Hyperbolic, Exponential and Logarithmic Functions in C
By: Henry Emailed: 1608 times Printed: 2075 times
Latest comments

The following listing contains a single program that demonstrates several of the Math functions. However if you need to refer to other functions then you may look at the tables below.
Using the C library math functions.
1: /* Demonstrates some of C's math functions */ 2: 3: #include <stdio.h> 4: #include <math.h> 5: 6: main() 7: { 8: 9: double x; 10: 11: printf("Enter a number: "); 12: scanf( "%lf", &x); 13: 14: printf("\n\nOriginal value: %lf", x); 15: 16: printf("\nCeil: %lf", ceil(x)); 17: printf("\nFloor: %lf", floor(x)); 18: if( x >= 0 ) 19: printf("\nSquare root: %lf", sqrt(x) ); 20: else 21: printf("\nNegative number" ); 22: 23: printf("\nCosine: %lf\n", cos(x)); 24: return(0); 25: } Enter a number: 100.95 Original value: 100.950000 Ceil: 101.000000 Floor: 100.000000 Square root: 10.047388 Cosine: 0.913482
ANALYSIS: This listing uses just a few of the math functions. A value accepted on line 12 is printed. Then it's passed to four of the C library math functionsceil(), floor(), sqrt(), and cos(). Notice that sqrt() is called only if the number isn't negative. By definition, negative numbers don't have square roots. You can add any of the other math functions to a program such as this to test their functionality.
Trigonometric Functions
The trigonometric functions perform calculations that are used in some graphical and engineering applications.
Function  Prototype  Description 
acos()  double acos(double x)  Returns the arccosine of its argument. The argument must be in the range 1 <= x <= 1, and the return value is in the range 0 <= acos <= p. 
asin()  double asin(double x)  Returns the arcsine of its argument. The argument must be in the range 1 <= x <= 1, and the return value is in the range p/2 <= asin <= p/2. 
atan()  double atan(double x)  Returns the arctangent of its argument. The return value is in the range p/2 <= atan <= p/2. 
atan2()  double atan2(double x, double y)  Returns the arctangent of x/y. The value returned is in the range p <= atan2 <= p. 
cos()  double cos(double x)  Returns the cosine of its argument. 
sin()  double sin(double x)  Returns the sine of its argument. 
tan()  double tan(double x)  Returns the tangent of its argument. 
Exponential and Logarithmic Functions
The exponential and logarithmic functions are needed for certain types of mathematical calculations.
Function  Prototype  Description 
exp()  double exp(double x)  Returns the natural exponent of its argument, that is, e^{x} where e equals 2.7182818284590452354. 
log()  double log(double x)  Returns the natural logarithm of its argument. The argument must be greater than 0. 
log10()  double log10(double x)  Returns the base10 logarithm of its argument. The argument must be greater than 0. 
frexp()  double frexp(double x, int *y)  The function calculates the normalized fraction representing the value x. The function's return value r is a fraction in the range 0.5 <= r <= 1.0. The function assigns to y an integer exponent such that x = r * 2y. If the value passed to the function is 0, both r and y are 0. 
ldexp()  double ldexp(double x, int y)  Returns x * 2y. 
Hyperbolic Functions
The hyperbolic functions perform hyperbolic trigonometric calculations.
Function  Prototype  Description 
cosh()  double cosh(double x)  Returns the hyperbolic cosine of its argument. 
sinh()  double sinh(double x)  Returns the hyperbolic sine of its argument. 
tanh()  double tanh(double x)  Returns the hyperbolic tangent of its argument. 
Other Mathematical Functions
The standard C library contains the following miscellaneous mathematical functions:
Function  Prototype  Description 
sqrt()  double sqrt(double x)  Returns the square root of its argument. The argument must be zero or greater. 
ceil()  double ceil(double x)  Returns the smallest integer not less than its argument. For example, ceil(4.5) returns 5.0, and ceil(4.5) returns 4.0. Although ceil() returns an integer value, it is returned as a type double. 
abs()  int abs(int x)  Returns the absolute 
labs()  long labs(long x)  value of their arguments. 
floor()  double floor(double x)  Returns the largest integer not greater than its argument. For example, floor(4.5) returns 4.0, and floor(4.5) returns 5.0. 
modf()  double modf(double x, double *y)  Splits x into integral and fractional parts, each with the same sign as x. The fractional part is returned by the function, and the integral part is assigned to *y. 
pow()  double pow(double x, double y)  Returns xy. An error occurs if x == 0 and y <= 0, or if x < 0 and y is not an integer. 
fmod()  double fmod(double x, double y)  Returns the floatingpoint remainder of x/y, with the same sign as x. The function returns 0 if x == 0. 
C Home  All C Tutorials  Latest C Tutorials
Sponsored Links 
If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Comments(3)
1.  View CommentSo inf0rmative! View Tutorial By: Pamela at 20081108 07:29:24 
2.  View CommentYour site is a really big help. Straight to point yet easy to understand! Good Job! Thanks! View Tutorial By: Mark Anthony Santos at 20081216 22:03:57 
3.  View CommentMay I have some examples of function exp and pow, please?! View Tutorial By: janee at 20090806 07:57:07 
Most Viewed Articles (in C )

Most Emailed Articles (in C)
