Trigonometric, Hyperbolic, Exponential and Logarithmic Functions in C

By: Henry Viewed: 153267 times  Printer Friendly Format    

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 */
3: #include <stdio.h>
4: #include <math.h>
6: main()
7: {
9:     double x;
11:     printf("Enter a number: ");
12:     scanf( "%lf", &x);
14:     printf("\n\nOriginal value: %lf", x);
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" );
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 functions--ceil(), 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, ex 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 base-10 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 floating-point remainder of x/y, with the same sign as x. The function returns 0 if x == 0.

Most Viewed Articles (in C )

Latest Articles (in C)

Comment on this tutorial