C Library math.h functions

On this page, We will list down popular C Library math.h functions that make your programming life easier. Like, ceil(), floor(), pow(), sqrt(),…etc.

In the project, you often need to perform the mathematical operations as per the project’s needs. If you want you can make your own version of the mathematical function. But it is ok only for a few functions and scenarios. So it is better to use library function as much as possible because they’re optimized and handles many negative scenarios.

It would be best if you remembered that you should include the <math.h> header file in your code before using any math library function because they are declared in this header file.

 

 

Function Work of Function
ceil() Compute the smallest integer value.
floor() Compute the largest integer value.
fabs() Compute the absolute value.
log() Compute the base-e (natural) logarithm.
log10() Compute the base-10 (common) logarithm.
sqrt() Compute the nonnegative square root.
pow(x,y) Compute x raised to the power y.
modf()
exp(x) Compute the base-e exponential of x.
fmod(x,y) Compute the floating-point remainder of x/y.
log2() Compute the base-2 logarithm.

 

Let’s see some simple C examples code to understand the use of predefined math.h library functions.

Code example: ceil()

#include <stdio.h>
#include <math.h>

int main ()
{
    double val1, val2, val3, val4;
    val1 = 3.5;
    val2 = -2.3;
    val3 = 3.8;
    val4 = 4.9;

    printf ("value1 = %.1lf\n", ceil(val1));
    printf ("value2 = %.1lf\n", ceil(val2));
    printf ("value3 = %.1lf\n", ceil(val3));
    printf ("value4 = %.1lf\n", ceil(val4));

    return(0);
}

Output:

value1 = 4.0
value2 = -2.0
value3 = 4.0
value4 = 5.0

 

Code example: floor()

#include <stdio.h>
#include <math.h>

int main ()
{
    double val1, val2, val3, val4;
    val1 = 3.5;
    val2 = -2.3;
    val3 = 3.8;
    val4 = 4.9;

    printf ("value1 = %.1lf\n", floor(val1));
    printf ("value2 = %.1lf\n", floor(val2));
    printf ("value3 = %.1lf\n", floor(val3));
    printf ("value4 = %.1lf\n", floor(val4));

    return(0);
}

Output:

value1 = 3.0
value2 = -3.0
value3 = 3.0
value4 = 4.0

 

Code example: fabs()

#include <stdio.h>
#include <math.h>

int main()
{
    double x, result;
    x = 3.5;
    
    result = fabs(x);
    printf("|%.2lf| =  %.2lf\n", x, result);
    
    x = -2.6;
    
    result = fabs(x);
    printf("|%.2lf| =  %.2lf\n", x, result);
    
    x = -5.9;
    
    result = fabs(x);
    printf("|%.2lf| =  %.2lf\n", x, result);
    
    return 0;
}

Output:

|3.50| = 3.50
|-2.60| = 2.60
|-5.90| = 5.90

 

 

Code example: log()

#include <stdio.h>
#include <math.h>

int main()
{
    double x = 4.2, result;

    result = log(x);

    printf("log(%lf) = %lf", x, result);

    return 0;
}

Output:

log(4.200000) = 1.435085

 

Code example: sqrt()

#include <math.h>
#include <stdio.h>

int main()
{
    double data, squareRoot;

    printf("Enter a number: ");
    scanf("%lf", &data);

    // Compute square root of data
    squareRoot = sqrt(data);

    //print square root of data
    printf("Square root of %f =  %f",data, squareRoot);

    return 0;
}

Output:

Enter a number: 16
Square root of 16.000000 = 4.000000

 

Code example: pow()

#include <stdio.h>
#include <math.h>

int main()
{
    double base, exponent, result;
    
    printf("Enter the base number: ");
    scanf("%lf", &base);
    
    printf("Enter the exponent raised: ");
    scanf("%lf",&exponent);
    
    result = pow(base,exponent);
    
    printf("%f^%f = %f", base, exponent, result);
    
    return 0;
}

Output:

Enter the base number: 3
Enter the exponent raised: 4
3.000000^4.000000 = 81.000000

 

Code example: fmod()

/**
 C Program to Calculate Floating-Point
 Remainder using fmod()
*/
#include <math.h>
#include <stdio.h>

int main()
{
    double x, y, z;
    x = 10.0; // Dividend
    y = 3.5;  // Divisor
    z = fmod(x,y);      /* z = 3.0 */

    printf("fmod( %lf, %lf) = %lf\n", x, y, z);
    return 0;
}

Output:

/*

fmod( 10.000000, 3.500000) = 3.000000

*/

 

 

Code example: exp()

#include <math.h>
#include <stdio.h>

int main()
{
    double x = 12.0, result;
    
    result = exp(x);
    
    printf("exp of %.2lf = %.2lf", x, result);
    
    return 0;
}

Output:

exp of 2.00 = 7.39

 

Code example: log2()

#include <stdio.h>
#include <math.h>

int main()
{
    double x = 4.2, result;
    
    result = log2(x);
    
    printf("log2(%lf) = %lf", x, result);
    
    return 0;
}

Output:

log2(4.200000) = 2.070389

 

Recommended Articles for you: