C Maths:
In C language, we can use math library and perform various operations. This library provides us a wide range of mathematical functions for tasks such as trigonometry, logarithms, exponentiation, and many more. This library provides a set of mathematical functions that can be used for various mathematical operations.Usage:
To use functions from math.h library, we need to include the header file at the top of the program using #include directive.#include <math.h>
maths.c
#include <stdio.h>
#include <math.h>
int main() {
int num = 16;
int result = sqrt(num);
printf("Square root is %d", result);
return 0;
}
Functions in Math Library:
There are different functions which are already defined in math.h Library. We just have to call them in our code. Performing these functions by yourself can be challenging for us and will be time consuming. Therefore, we use these libraries to save our time and to reduce the chance of errors.Function | Description | Code |
---|---|---|
pow(x, y) | It takes the power of x raised to y. |
|
sin(x) | It calculates sin of x. |
|
cos(x) | It calculates cos of x. |
|
tan(x) | It calculates tan of x. |
|
log(x) | It calculates natural log of x. |
|
log10(x) | It calculates log of x with base 10. |
|
fabs(x) | It calculates absolute value of floating point numbers. |
|
ceil(x) | It rounds a number up to a nearest integer. |
|
floor(x) | It rounds a number down to a nearest integer. |
|
round(x) | It rounds a floating point number to a nearest integer. |
|
fmin(x, y) | It returns the minimum value among two floating point numbers. |
|
fmax(x, y) | It returns the maximum value among two floating point numbers. |
|
Note: Before using any function of math.h Library, do not forget to include math.h.