C math Functions

The math.h header file contains various C math functions that help to perform various mathematical operations on numeric values. In this article, we will explain the available list of math library functions in C programming language with an example of each.

The mathematical library is a part of the C programming language standard library, so we must include <math.h> at the beginning (#include <math.h>). All the C math functions accept double data type as input and return a value of type double.

When performing financial calculations or scientific computing, finding a square, cube root, or trigonometric value is the most common scenario. Generally, we must write a lot of logic to perform a simple calculation. However, the C math functions reduce the code logic and ready made solutions.

List of C math functions

The following list shows the available math library functions in C programming language. Most of the math functions mentioned below accept a single argument. However, a few methods, including pow(), hypot() functions, etc accepts two arguments to produce the result.

C Math FunctionsDescription
absReturns the absolute positive value of an integer.
acosIt returns a trigonometric arc cosine value.
asinIt returns a trigonometric arc sine value.
atanReturns a trigonometric arc tangent value.
atan2It returns a trigonometric arc tangent value in terms of y/x.
cbrtFinds the cube root of a number.
ceilReturns the smallest number that is greater than or equal to x.
cosIt returns a trigonometric cosine value.
coshIt returns a hyperbolic cosine value.
expReturns the exponential value of a given number.
expm1(x)Returns the exponential value of x minus 1.
exp2(x)It returns 2 raised to the power of x (2x).
frexp(x)It breaks a floating-point numeric value into its mantissa and exponent.
fabsReturns the absolute positive value of a floating-point value.
floorIt returns the largest number that is less than or equal to x.
fmod(x, y)Returns the remainder of x divided by y.
hypotFind the trigonometric hypotenuse value of two sides.
ldexp(x)It is the inverse of the frexp() function.
logIt returns the natural logarithmic value of a given number.
log1p(x)Returns the natural logarithmic value of x plus 1.
log2(x)It returns the base 2 logarithmic value of a given number.
log10It returns the base 10 logarithmic value of a given number.
logb(x)Returns the exponential value to get the x value.
modf()Splits the number into an integer and a fraction part.
powReturns x raised to the power of y.
roundRounds the given value to the nearest number based on the decimal values.
sinIt returns a trigonometric sine value.
sinhIt returns a hyperbolic sine value.
sqrtFinds the square root of a number.
tanIt returns a trigonometric Tangent value.
tanhIt returns a hyperbolic tangent value.
truncTruncates the decimal values in a given number.

C math functions to find the absolute value

The math library has fabs() and abs() functions that accept double and integer values and return the absolute positive numeric values.

double fbas(double n)

The fabs() function accepts a numeric value and returns the absolute positive value of the given number.

#include <stdio.h>
#include<math.h>
int main()
{
int n = -520;

printf("%.2f", fabs(n));
return 0;
}
520.00

int abs(int n)

The abs() function accepts an integer value and returns the absolute positive value of it.

#include <stdio.h>
#include <math.h>
int main()
{
int n = -15;
printf("%d", abs(n));
return 0;
}
15

C math functions to perform mathematical calculations

The math library consists of sqrt(), cbrt(), and pow() functions to find the square root, cube root, and power of a given number. The sqrt(), cbrt(), and pow() functions are very helpful for performing mathematical calculations and scientific computing.

double sqrt(double x)

The sqrt() function returns the square root of a given number x. For a negative number, it returns Nan.

double cbrt(double x)

The cbrt() function returns the cube root of a given number x. For a negative number, it returns Nan.

In the following C math functions example, the sqrt() and cbrt() methods calculate the square root and cube root of 64, which are 8 and 4.

#include <stdio.h>
#include<math.h>
int main()
{
double n = 64.0, x = 2.0, y = 4.0;
printf("Square Root = %.2f\n", sqrt(n));
printf("Cube Root = %.2f\n", cbrt(n));
return 0;
}

Result

Square Root = 8.00
Cube Root = 4.00

double pow(double x, double y)

The pow() function accepts two arguments and returns x raised to the power of y. For a negative number, it returns Nan.

In the following example, we used the pow() function to find 2 raised to the power of 4 (24 = 2 * 2 * 2 * 2 = 16).

#include <stdio.h>
#include<math.h>
int main()
{
double x = 2.0, y = 4.0;
printf("x raised to power y = %.2f", pow(x, y));
return 0;
}

Result

x raised to power y = 16.00

double scalbn(double x, int n)

The scalbn() function scales floating-point numbers by a power of 2. The scalbn() function returns x * 2 raised to the power of n. In other words, x * 2n

double scalbln(double x, long int n)

The scalbln() function works the same as the scalbn() method and returns the same result. However, the second argument must be a long integer.

In the following C math functions example, the first printf() statement returns 32 because 24 (n = 4) is 16 and multiplying by 2 (x) is 32. The second statement returns 2048 because 210 is 1024 and multiplying by 2 (x) is 2048.

#include <stdio.h>
#include<math.h>
int main()
{
double x = 2.0, y = 4;
long z = 10L;
printf("x * 2 raised to power Int y = %.2f\n", scalbn(x, y));
printf("x * 2 raised to power Long z = %.2f", scalbln(x, z));
return 0;
}

Result

x * 2 raised to power Int y = 32.00
x * 2 raised to power Long z = 2048.00

C math functions to find the modulus

The <math.h> header file contains the fmod() function to return the remainder value. On the other hand, the modf() function returns the integer and fractional parts of a number.

double fmod(double x, double y)

The fmod() function returns the remainder of x divided by y. It returns the remainder of the division. In the following example, we used the fmod() function on a floating-point value to find the remainder. However, the result will be of a double type.

#include <stdio.h>
#include<math.h>
int main()
{
float x = 100.5, y = 30.5;
printf("fmod = %.2f\n", fmod(x, y));
return 0;
}
fmod  = 9.00

double modf(double x, double *intpart):

The modf() function splits the given number into an integer part and a fraction part. Here, x is the actual double value, and intpart points to the integer part memory location. The return value is the fractional part of the x value.

#include <stdio.h>
#include<math.h>
int main()
{
double x = 10.98756;
double intpart, fracpart;

fracpart = modf(x, &intpart);
printf("Integer Part = %f\n", intpart);
printf("Fractional Part = %f\n", fracpart);
return 0;
}

Result

Integer Part = 10.000000
Fractional Part = 0.987560

C math functions for rounding a value

The C math library contains three functions to round a given value to the nearest integer: ceil(), floor(), and round(). Apart from these three, there is a trunc() function that truncates the number.

  • double ceil(double x): The ceil() function returns the smallest integer value that is greater than or equal to x. Use the ceil() function to round up to the next possible integer value.
  • double floor(double x): The floor() function returns the largest integer value that is less than or equal to x. Use the floor() function to round down to the possible integer value.
  • double round(double x): As the name suggests, the round() function rounds the user-given value to the nearest numeric value. If the decimal value is greater than or equal to 50 (10.5 and above), it returns the next number (11). If the decimal value is less than 50 (10.49 and below), it returns the same numeric value (10).
  • double trunc(double x): The trunc() function strips or removes the decimal values and sets them to 0.

In the following C math functions example, we declared a double data type variable and assigned a value of 10.65. Next, use the ceil(), floor(), round(), and trunc() functions on that value.

#include <stdio.h>
#include<math.h>
int main()
{
double n = 10.65;

printf("Ceil = %.2f\n", ceil(n));
printf("Floor = %.2f\n", floor(n));
printf("Round = %.2f\n", round(n));
printf("Trunc = %.2f", trunc(n));

return 0;
}

Result

Ceil  = 11.00

Floor = 10.00

Round = 11.00

Trunc = 10.00

C math functions to find the log value

In the math library, there are log(), log2(), log10(), and log1p() functions finds the logarithmic values. These functions are helpful in scientific computing.

C math functions – log(), log2(), log10(), and log1p()

  • double log(double x): The log() function returns the natural logarithmic value of a given number. Here, the natural logarithm means the base e value of x. Here, e is equivalent to 2.718.
  • double log10(double x): The log10() function returns the base 10 logarithmic value of a given number.
  • double log2(double x): The log2() function returns the base 2 logarithmic value of a given number.
  • double log1p(double x): The log1p() function returns the natural logarithmic value of x plus 1. It means log1p = log(x + 1)

TIP: The log() function is the inverse of the exp() function. The return value of the log() function is equivalent to the exp() function.

In the following C math functions example, we used the built-in log(), log10(), log2(), and log10() methods on 15.5, 50, 20, and 10.5.

#include <stdio.h>
#include<math.h>
int main()
{
printf("Base e Logarithmic Value = %2f\n", log(15.5));
printf("Base 10 Logarithmic Value = %2f\n", log10(50));
printf("Base 2 Logarithmic Value = %2f\n", log2(20));
printf("log1p Result = %.2f", log1p(10.5));
return 0;
}

Result

Base e Logarithmic Value  = 2.740840
Base 10 Logarithmic Value = 1.698970
Base 2 Logarithmic Value = 4.321928
log1p Result = 2.44

logb(double x)

The logb() function returns the exponential value to get the x value. Here x = 2e and the logb() function returns that e value. In the following example, 32 means 25

ilogb(double x)

The ilogb() function performs the same operation as the logb() method and returns the same result. However, the ilogb() function returns an integer value, whereas the logb() returns a double value and follows the floating point process.

#include <stdio.h>
#include<math.h>
int main()
{
double n = 32.0;
printf("logb = %.2f\n", logb(n));
printf("ilogb = %d", ilogb(n));
return 0;
}

Result

logb = 5.00
ilogb = 5

C math functions for exponential values

The math library contains exp(), frexp(), and ldexp() functions to find the exponential values of the given number.

double exp(double x)

The exp() function returns the exponential value of x. In other words, the exp() function calculates e raised to the power of x (ex), where e is approximately equal to 2.718.

double expm1(double x)

The expm1() function returns the exponential value of x minus 1. In other words, the expm1() returns the same result as the exp() function and subtracts 1 from it.

double exp2(double x)

The exp2() function returns the exponential value by calculating 2 raised to the power of x (2x). The following C math functions example shows a scenario of exp(), exp2(), and expm1() methods.

#include <stdio.h>
#include<math.h>
int main()
{
double n = 5.0;
printf("exp = %.2f\n", exp(n));
printf("expm1 = %.2f\n", expm1(n));
printf("exp2 = %.2f\n", exp2(n));
return 0;
}

Result

exp = 148.41
expm1 = 147.41
exp2 = 32.00

double frexp(double x, int *exponent)

The frexp() function accepts a double and a pointer to break a floating-point numeric value into its fraction (mantissa) and exponent. The math formula is x = fraction * 2exponent.

In the following example, we demonstrated how to use the frexp() to break the given number into a fraction and exponent. Next, we used the math formula to reconstruct the result to obtain the original value.

#include <stdio.h>
#include<math.h>
int main()
{
double x = 100, f;
int e;

f = frexp(x, &e);
printf("Fraction = %.2f\n", f);
printf("Exponent = %d\n", e);

printf("Original: %.2f\n", f * pow(2, e));
return 0;
}

Result

Fraction = 0.78

Exponent = 7

Original: 100.00

double ldexp(double x, int exponent)

The ldexp() function is the inverse of the frexp() function. It accepts a double value and an exponent (int) and multiplies the given floating-point value by 2 raised to the power of e. The math formula is ldexp(fraction, exponent) = fraction * 2exponent.

#include <stdio.h>
#include<math.h>
int main()
{
double x, f = 0.7813;
int e = 7;

x = ldexp(f, e);
printf("Number = %.2f\n", x);
printf("Fraction = %.2f\n", f);
printf("Exponent = %d\n", e);
return 0;
}

Result

Number = 100.01

Fraction = 0.78

Exponent = 7

C math library Trigonometric functions

The math library contains built-in trigonometric functions such as sin(), cos(), and tan() for returning the sine, cosine, and tangent values of a given angle.

sin(), cos(), and tan() functions

  1. sin(): The sin() function returns the trigonometric sine value of a given number.
  2. cos(): The cos() function returns the cosine value of a given angle.
  3. tan(): The tan() function returns the trigonometric tangent value of a given angle.

TIP: All these three functions return a double value as the output.

In the following C math functions example, we have declared a double variable with a value of 45.0. Next, we used the sin(), cos(), and tan() methods to find out the sine, cosine, and tangent values of 45.0.

#include <stdio.h>
#include<math.h>
int main()
{
double n = 45.0;

printf("Sine = %.2f\n", sin(n));
printf("Cosine = %.2f\n", cos(n));
printf("Tangent = %.2f", tan(n));

return 0;
}

Result

Sine    = 0.85

Cosine  = 0.53

Tangent = 1.62

Double hypot(double x, double y)

Apart from the above mentioned three methods, there is a hypot() function that returns the trigonometric hypotenuse value of a given two sides.

#include <stdio.h>
#include<math.h>
int main()
{
double x = 10.0, y = 25.0;
printf("hypot = %.2f", hypot(x, y));
return 0;
}

26.93

C math library hyperbolic functions

In trigonometry, hyperbolic functions look similar to the regular trigonometric functions. However, they are different because instead of using a circle, they are defined using a hyperbola. In c math library functions, there are three types of hyperbolic methods: sinh(), cosh(), and tanh().

  • double sinh(double x): The sinh() function returns the hyperbolic sine value of x.
  • double cosh(double x): The cosh() function returns the hyperbolic cosine value of x.
  • double tanh(double x): The tanh() function returns the hyperbolic tangent value of x.

TIP: sinh(), cosh(), and tanh() functions return a double value as the output.

In the following example, we have declared a double variable and assigned 5 to it. Next, we used the sinh(), cosh(), and tanh() methods from built-in C math functions to find out the hyperbolic sine, cosine, and tangent values of 5.

#include <stdio.h>
#include<math.h>
int main()
{
double n = 5;

printf("Hyperbolic Sine = %.2f\n", sinh(n));
printf("Hyperbolic Cosine = %.2f\n", cosh(n));
printf("Hyperbolic Tangent = %.2f", tanh(n));

return 0;
}

Result

Hyperbolic Sine    = 74.20

Hyperbolic Cosine  = 74.21

Hyperbolic Tangent = 1.00

C math library inverse trigonometric functions

The math.h header file also contains the built-in inverse trigonometric functions, and they are asin(), acos(), atan(), and atan2() functions.

  • double asin(double x): The asin() function returns the arc sine value of x.
  • double acos(double x): The acos() function returns the arc cosine value of x.
  • double atan(double x): The atan() function returns the arc tangent value of x.
  • double atan2(double y, double x): The atan2() function accepts two arguments and returns the arc tangent of “y/x” based on the sign of them.

TIP: asin(), acosh(), atan(), and atan2() functions return a double value as the output.

In the following example, we have declared a double variable and assigned 5 to it. Next, we used the asin(), acos(), atan, and atan2() library methods from built-in C math functions to find out the arc sine, cosine, and tangent values of 5.

#include <stdio.h>
#include<math.h>
int main()
{
double n = 0.90;

printf("Arc Sine = %.2f\n", asin(n));
printf("Arc Cosine = %.2f\n", acos(n));
printf("Arc Tangent = %.2f\n", atan(n));
printf("Arc Tangent = %.2f", atan2(n, 4));
return 0;
}

Result

Arc Sine    = 1.12
Arc Cosine  = 0.45
Arc Tangent = 0.73
Arc Tangent = 0.22

C math library functions to find Gamma values

There are two gamma functions, and they are the tgamma () and lgamma().

  • double tgamma(double x): The tgamma function returns the factorial of x – 1. The following example returns 24 because the factorial of 5 -1 = 4! = 24.
  • double lgamma(double x): The lgamma function returns the natural logarithmic value of the factorial of x – 1. In other words, lgamma(x) = log(tgamma(x)).

The following example returns 3.14, which is the base e logarithmic value of 24.

#include <stdio.h>
#include<math.h>
int main()
{
double n = 5.0;
printf("tgamma = %.2f\n", tgamma(n));
printf("lgamma = %.2f\n", lgamma(n));
return 0;
}

Result

tgamma = 24.00
lgamma = 3.18

Floating-point Utility math functions in C

The fdim(), fmax(), fmin(), and fma() functions are floating-point utility functions available in the math library.

  • double fmin(double x, double y): The fmin() function accepts two arguments, finds and returns the minimum value among them.
  • double fmax(double x, double y): The fmax() function finds and returns the maximum value among the given two values.
  • double fdim(double x, double y): The fdim() function returns the positive difference between the given two numbers. If x is greater than y, the fdin() function returns x – y. Otherwise, it returns 0 (no negative result).
  • double fma(double x, double y, double z): The fma() function means floating-point multiplication and addition in a single operation. It accepts three arguments and multiplies the first two, then adds the third value. fma(x, y, z) = (x * y) + z.

In the following C math functions example, we declared two floating-point numbers and used the fdim(), fmax(), fmin(), and fma() functions on them. For the fma(), it needs a third argument, so we hardcoded 99.

#include <stdio.h>
#include<math.h>
int main()
{
double x = 40.0, y = 30.0;
printf("Minimum = %.2f\n", fmin(x, y));
printf("Maximum = %.2f\n", fmax(x, y));
printf("Difference = %.2f\n", fdim(x, y));
printf("Multiply and Add = %.2f\n", fma(x, y, 99));
return 0;
}

Result

Minimum = 30.00
Maximum = 40.00
Difference = 10.00
Multiply and Add = 1299.00

Comparison Macros in C math library functions

  1. int isgreater(x, y): The isgreater() function checks whether x is greater than y or either of them is Nan. The isgreater() function returns 1 (true) or 0 (false) as the output.
  2. int isgreaterequal(x, y): The isgreaterequal() function checks whether x is greater than or equal to y and returns 1 (true) or 0 (false) as the output.
  3. int isless(x, y): The isless() function checks whether x is less than y or either of them is Nan. The isless() function returns 1 (true) or 0 (false) as the output.
  4. int islessequal(x, y): The islessequal() function checks whether x is less than or equal to y and returns 1 (true) or 0 (false) as the output.
  5. int islessgreater(x, y): The islessgreater() function checks whether x is either less than or greater than y. If x and y are equal, the islessgreater() function returns 0 (false); otherwise, it returns 1 (true) as the output.
  6. int isunordered(x, y): The isunordered() function checks whether either x or y is a NAN (not a number) value. If True, return 1; else 0.

The following C math functions example demonstrates the isgreater(), isgreaterequal(), isless(), islessequal(), and  islessgreater() functions.

#include <stdio.h>
#include<math.h>
int main()
{
printf("isgreater = %d\n", isgreater(10.0, 8.0));
printf("isgreaterequal = %d\n", isgreaterequal(10.0, 18.0));
printf("isless = %d\n", isless(12.0, 11.0));
printf("islessequal = %d\n", islessequal(12.0, 22.0));
printf("islessgreater = %d\n", islessgreater(12.0, 22.0));
printf("islessgreater = %d\n", islessgreater(12.0, 12.0));
printf("isunordered = %d\n", isunordered(NAN, 12.0));
return 0;
}

Result

isgreater = 1
isgreaterequal = 0
isless = 0
islessequal = 1
islessgreater = 1
islessgreater = 0
isunordered = 1