Math functions in C
Math functions in C:
In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical functions and one macro.
Many functions are available in this library to take double as an argument and then return double as a result. The <math.h> header file consist of various methods and types for performing many mathematical operations in the program, such as sqrt(), floor(), ceil(), pow(), and so on.
The C mathematical operations are a group of functions present in the standard library of the C standard language that implements essential functions of mathematics. In present, all these functions use floating point numbers in one manner or the other.
Different C standards provide a different albeit backward compatible set of functions. Most of the functions are available in the C standard library but in different header files.
Functions included in <math.h> header are as discussed below:
- double ceil (double number)
The ceil() rounds up the given number, and it always returns the integer value, which will be greater than or equal to the given number.
E.g.:
#include <stdio.h> #include <conio.h> #include <math.h> int main() { float n1, n2, n3, n4; n1 = 1.6; n2 = 1.2; n3 = -1.6; n4 = -1.2; printf (“The ceil of n1 = %f \n”, ceil(n1)); printf (“The ceil of n2 = %f \n”, ceil(n2)); printf (“The ceil of n2 = %f \n”, ceil(n2)); printf (“The ceil of n2 = %f \n”, ceil(n2)); return 0; }
Output
- double floor (double number):
The floor() rounds up the given number, and it always returns the integer value, sqrt() which will be greater than or equal to the given number.
E.g.:
#include <stdio.h> #include <conio.h> #include <math.h> int main() { float n1, n2, n3, n4; n1 = 1.6; n2 = 1.2; n3 = -1.6; n4 = -1.2; printf (“The floor of n1 = %f \n”, floor(n1)); printf (“The floor of n2 = %f \n”, floor(n2)); printf (“The floor of n3 = %f \n”, floor(n3)); printf (“The floor of n4 = %f \n”, floor(n4)); return 0; }
Output
- double abs (double number)
The abs() returns the absolute value of the given number.
E.g.:
#include <stdio.h> #include <stdlib.h> int main() { int m = abs(200); // m is assigned to 200 int n = abs(-400); // n is assigned to -400 printf("Absolute value of m = %d\n", m); printf("Absolute value of n = %d \n",n); return 0; }
Output
- double fabs (double number)
The fabs() and abs() function work in a similar way as both returns the absolute value of the number given.
- double pow (base, exponent)
The pow() returns the power of the given number and its exponent.
E.g.:
#include <stdio.h> #include <conio.h> #include <math.h> int main() { int base, power; double float; printf (“Enter the base number:”); scanf (“%d”, &base); printf (“Enter the power to be raised of the number:”); scanf (“%d”, &base); result = pow(base, power); printf (“%d ^ %d = %.2 lf”, base, power, result); return 0; }
Output
- sqrt (double number)
The sqrt() returns the square root of the given number.
E.g.:
#include <stdio.h> #include <conio.h> #include <math.h> int main() { printf (“The square root of the number 5 is: %d”, sqrt(5)); printf (“The square root of the number 10 is: %d”, sqrt(10)); return 0; }
Output
- double log (double number)
The log() returns the natural logarithm of the given number, that is, the base e logarithm of the number.
E.g.:
#include <stdio.h> #include <conio.h> #include <math.h> int main() { double n = 5.6, result; result = log(num); printf (“log(%.lf) = %.2 f”, num, result); return 0; }
Output
- double log10 (double number)
The log10() returns the common logarithm of the given number, that is, the base 10 logarithm of the number.
E.g.:
#include <stdio.h> #include <conio.h> #include <math.h> int main() { double n = 4, result; result = log10(num); printf (“log10(%lf) = %.2 f”, num, result); return 0; }
Output