C Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Math functions in C

Math functions in C

shape Description

Math functions in C are performed by math function. To implement the functions in the program, one has to use <math.h> library file. These functions has the arguments in double type.Also, the return statements are also in double type.

shape Functions

Table giving the description of few Math functions in C.
Function Description
double asin(double a) arc sine of a is returned in radians.
double acos(double x) arc cosine of a is returned in radians.
double atan(double a) arc tangent of a is returned in radians.
double atan2(double a, double b) The arc tangent of b/a gives the quadrant depending on signs given is eturned
double sin(double a) Sine of angle a is returned in radians
double sinh(double a) Hyperbolic Sine of angle a is returned in radians
double cos(double a) Cosine of angle a is returned in radians
double cosh(double a) Hyperbolic Sine of angle a is returned in radians
double tanh(double a) Hyperbolic Tangent of angle a is returned in radians
double exp(double a) The value is the result of e lifted to the ath power
double frexp(double a, int *exp) a=mantissa* 2^ exp is returned
double log(double a) Logarithmic value is returned with base e
double log10(double a) Logarithmic value is returned with base 10
double sqrt(double a) Square root of a is returned
double ceil(double a) Smaller value is returned which is greater or equal to a
double fabs(double a) Absolute integer value of a is returned
double floor(double a) Larger value is returned which is lesser equal to a
double fmod(double a, double b) Remainder of a divided by b is returned
double log10(double a) Logarithmic value is returned with base 10
double sqrt(double a) Square root of a is returned
double pow(double a, double b) The value obtained after a raised to power of b is returned

shape More Info

The macro used in time function is HUGE_VAL. When the output is too large in magnitude, HUGE_VAL is used by setting the errno to ERANGE. If the result is small, then there is no need to use this.

shape Examples

[c] #include <stdio.h> #include <math.h> #define PI 3.14159265 int main () { double x,y, i,r ,a,b; x = 45.0; y = 0.8; a = PI / 180; b = 180.0 / PI; i = sin( x*a ); printf("The sine of %lf is %lf degrees\n", x, i); i = cos( x*a ); printf("The cosine of %lf is %lf degrees\n", x, i); r = asin(y) * b; printf("The arc sine of %lf is %lf degrees\n", y, r); r = acos(y) * b; printf("The arc sine of %lf is %lf degrees\n", y, r); return(0); }[/c] Output [c] The sine of 45.000000 is 0.707107 degrees The cosine of 45.000000 is 0.707107 degrees The arc sine of 0.800000 is 53.130102 degrees The arc sine of 0.800000 is 36.869898 degrees[/c] Program to demonstrate math functions [c] #include <stdio.h> #include <math.h> int main () { double i, result; i = 2.7; //find log(2.7) result = log(i); printf("log(%lf) = %lf \n", i, result); //find square root printf("Square root of i = %lf \n",sqrt(i) ); //Rounding to the nearest lower integer printf ("value = %.1lf \n", ceil(i)); //Find absolute value printf("The absolute value of i = %lf\n", i, fabs(i)); //rounding to the nearest greater integer printf("Value = %.1lf \n", floor(i)); return(0); }[/c] Output [c] log(2.700000) = 0.993252 Square root of i = 1.643168 value = 3.0 The absolute value of i = 2.700000 Value = 2.0[/c]

Summary

shape Key Points

  • Math functions in C are implemented by <math.h> and supports all arithmetic functions.
  • Returns the arguments in double type.
  • sin, tan, sqrt, log, floor, pow, fabs, ceil are some of math functions.