C - Mathematical Library Functions
The mathematical library functions in C are part of the standard C library and are declared in the <math.h>
header file. These functions provide a wide range of mathematical operations and functions for performing calculations involving numbers. Here are some commonly used mathematical library functions in C:
1. Basic Mathematical Functions:
-
'sqrt': Calculates the square root of a number.
#include <stdio.h>
#include <math.h>
int main() {
double number = 25.0;
double result = sqrt(number);
printf("The square root of %lf is %lf.\n", number, result);
return 0;
}
Output:
The square root of 25.000000 is 5.000000.
-
'pow': Calculates the power of a number (exponentiation).
#include <stdio.h>
#include <math.h>
int main() {
double base = 2.0;
double exponent = 3.0;
double result = pow(base, exponent);
printf("%lf raised to the power of %lf is %lf.\n", base, exponent, result);
return 0;
}
Output:
2.000000 raised to the power of 3.000000 is 8.000000.
2. Trigonometric Functions:
-
'sin': Calculates the sine of an angle (in radians).
-
'cos': Calculates the cosine of an angle (in radians).
-
'tan': Calculates the tangent of an angle (in radians).
#include <stdio.h>
#include <math.h>
int main() {
double angle = 1.047; // 60 degrees in radians
double sine = sin(angle);
double cosine = cos(angle);
double tangent = tan(angle);
printf("sin(%lf) = %lf\n", angle, sine);
printf("cos(%lf) = %lf\n", angle, cosine);
printf("tan(%lf) = %lf\n", angle, tangent);
return 0;
}
Output:
sin(1.047000) = 0.866025
cos(1.047000) = 0.500000
tan(1.047000) = 1.732051
3. Logarithmic and Exponential Functions:
-
'log': Calculates the natural logarithm (base e) of a number.
-
'exp': Calculates the exponential function e raised to a power.
#include <stdio.h>
#include <math.h>
int main() {
double x = 2.0;
double log_result = log(x);
double exp_result = exp(x);
printf("log(%lf) = %lf\n", x, log_result);
printf("exp(%lf) = %lf\n", x, exp_result);
return 0;
}
Output:
log(2.000000) = 0.693147
exp(2.000000) = 7.389056
These are just a few examples of the many mathematical library functions available in C. They provide a wide range of mathematical capabilities, making it easier to perform various mathematical calculations in C programs.