Trigonometric, Hyperbolic, Exponential and Logarithmic Functions in C
By: Henry in C Tutorials on 2007-10-03
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, 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 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. |
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in C ) |
Latest Articles (in C) |
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations 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
Comments