Trigonometric, Hyperbolic, Exponential and Logarithmic Functions in C

By: Henry Emailed: 1704 times Printed: 2206 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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 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.

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.



Bookmark and Share

Comments(3)


1. View Comment

So inf0rmative!

View Tutorial          By: Pamela at 2008-11-08 07:29:24
2. View Comment

Your site is a really big help. Straight to point yet easy to understand! Good Job! Thanks!

View Tutorial          By: Mark Anthony Santos at 2008-12-16 22:03:57
3. View Comment

May I have some examples of function exp and pow, please?!
It will be a great help...
Thanks...


View Tutorial          By: janee at 2009-08-06 07:57:07

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Henry
Generating Your Key Pair example using keytool in Java
Summary of Operators in Java
JDBC Basics and JDBC Components
The IterationTag Interface in JSP
Trigonometric, Hyperbolic, Exponential and Logarithmic Functions in C
Using Bit-field in C
Java Beans and the Expression Language
Initialization of Variables in C
Creating Struts Modules
else if statement in C
Line Counting sample program in C
Accessing Context Data in Beans using JSF
Using peek() and putback() with cin in C++
Internationalization Support in J2ME MIDP
java.lang.SecurityException: MIDP lifecycle does not support system exit.

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
goto and labels in C
Listing Files and Directories sample program in C
Using memset(), memcpy(), and memmove() in C
Find square and square root for a given number in C
Functions returning non-integer values in C
lseek() sample program in C
Using free() Function in C
Formatting with printf in C
Character Arrays in C
Type Conversions in C (String to Integer, isdigit() etc)
Precedence and Order of Evaluation in C
Using printf function in C
assert() Function Example program in C
perror() Function - example program in C
Printing a simple histogram in C
Most Emailed Articles (in C)
Word Counting sample program in C
Do while Loops in C
Bitwise Operators in C
Functions returning non-integer values in C
getch and ungetch in C
File Inclusion in C
Pointer Arrays and Pointers to Pointers in C
Multi-dimensional Arrays in C (Explained using date conversion program)
Using memset(), memcpy(), and memmove() in C
Using qsort() and bsearch() with strings - example program in C
Statements in C
Formatting with printf in C
while, do while and for loops in C
Infix to Prefix And Postfix in C
Printing a simple histogram in C