C Math Library
C Math Library
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. As explained in the previous tutorial, here are more functions that can be used in <math.h> library
- double exp (double number)
The exp() returns the value of e raised to the power of the given number.
E.g.:
#include <stdio.h> #include <conio.h> #include <math.h> int main() { double x = 12.0, result; result = exp(x); printf (“The exponential value of 12 is = %.2lf”, x, result); return 0; }
Output
- double fmod (double number 1, double number 2)
It returns the remainder of the number 1 divided by the number 2. It works as a modulus function.
E.g.:
#include <stdio.h> #include <conio.h> #include <math.h> int main () { int a, b, c; a = 8; b = 6; c = 4; printf("The remainder of 8 / 4 is %lf \n", fmod(a, c)); printf("The remainder of 8 / 6 is %lf \n", fmod(a, b)); printf("The remainder of 6 / 4 is %lf \n", fmod(b, c)); return(0); }
Output
- double acos (double number)
The acos() returns the arc cosine of the number given in radians.
E.g.:
#include <stdio.h> #include <math.h> int main() { const double PI = 3.1415926; double x, result; x = -0.5; result = acos(x); printf("the inverse of cos(-0.5) = %.2lf in radians\n", result); /* converting radians to degree */ result = acos(x)*180/PI; printf("The inverse of cos(-0.5) = %.2lf in degrees\n", result); /* paramter not in range*/ x = 1.2; result = acos(x); printf("Inverse of cos(1.2) = %.2lf", result); return 0; }
Output
- double asin (double number)
The asin() returns the arc sine of the number given in radians.
E.g.:
#include <stdio.h> #include <math.h> int main() { const double PI = 3.1415926; double x, result; x = -0.5; result = asin(x); printf("the inverse of sine(-0.5) = %.2lf in radians\n", result); /* converting radians to degree */ result = asin(x)*180/PI; printf("The inverse of sine(-0.5) = %.2lf in degrees\n", result); /* paramter not in range*/ x = 1.2; result = asin(x); printf("The inverse of sine(1.2) = %.2lf", result); return 0; }
Output
- double atan (double number)
The atan() returns the arc tangent of the number given in radians.
E.g.:
#include <stdio.h> #include <math.h> #define PI 3.141592654 int main() { double num = 1.0, result; result = atan(num); printf("The inverse of tan(1.0) = %.2f in radians \n", result); // Converting radians to degrees result = (result * 180) / PI; printf("the Inverse of tan(1.0) = %.2f in degrees \n", result); return 0; }
Output
- double cos (double number)
The cos() returns the cosine of the number in radian.
E.g.:
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main () { double x, ret, val; x = 60.0; val = PI / 180.0; ret = cos( x*val ); printf("The cosine of %lf is %lf degrees\n", x, ret); x = 90.0; val = PI / 180.0; ret = cos( x*val ); printf("The cosine of %lf is %lf degrees\n", x, ret); return 0; }
Output
- double cosh (double number)
It returns the hyperbolic cosine of the value of the number.
E.g.:
#include <stdio.h> #include <math.h> int main () { double x, result; x = 0.5; result = cosh(x); printf("Hyperbolic cosine of 0.5 (in radians) = %lf\n", x, result); x = -0.5; result = cosh(x); printf("Hyperbolic cosine of -0.5 (in radians) = %lf\n", x, result); x = 0; result = cosh(x); printf("Hyperbolic cosine of 0 (in radians) = %lf\n", x, result); return 0; }
Output
- double sin (double number)
It returns the sine of the number in radian.
E.g.:
#include <stdio.h> #include <math.h> int main() { double x; double result; x = 2.3; result = sin(x); printf("The sin(2.3) = %.2lf \n", result); x = -2.3; result = sin(x); printf("The sin(-2.3) = %.2lf \n", result); x = 0; result = sin(x); printf("The sin(0) = %.2lf \n", result); return 0; }
Output
- double sinh (double number)
It returns the hyperbolic sine of the value of the number.
E.g.:
#include <stdio.h> #include <math.h> #define PI 3.141592654 int main() { double angle = 2.50, result; result = sinh(angle); printf("Sine hyperbolic of 2.50 (in radians) = %.2lf", angle, result); return 0; }
Output
- double tanh (double number)
It returns the hyperbolic tangent of the value of the number.
E.g.:
#include <stdio.h> #include <math.h> int main () { double x, result; x = 0.5; result = tanh(x); printf("The hyperbolic tangent of 0.5 is %lf degrees", result); return(0); }
Output
- double modf (double number, double *integer)
It returned those value which will be the fraction component and sets the integer to the integer component.
E.g.:
#include<stdio.h> #include<math.h> int main () { double x, fractpart, intpart; x = 3.345; fractpart = modf(x, &intpart); printf("The integral part = %lf\n", intpart); printf("The fraction Part = %lf \n", fractpart); return(0); }
Output