Mathematical Functions Introduction - C Function

C examples for Function:Math Function

Introduction

The math.h header file includes declarations for a range of mathematical functions.

The following table lists numerical calculations of various kinds.

These all require arguments to be of type double.

Function Operation
floor(x) Returns the largest integer that isn't greater than x as type double
ceil(x) Returns the smallest integer that isn't less than x as type double
fabs(x) Returns the absolute value of x
log(x) Returns the natural logarithm (base e) of x
log10(x) Returns the logarithm to base 10 of x
exp(x) Returns the value of ex
sqrt(x) Returns the square root of x
pow(x, y)Returns the value xy

There are also versions of these for types float and long double that have f or l, respectively.

For example, ceilf() applies to float values and sqrtl() applies to long double values, for example.

Demo Code

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

int main(void) { 
    double x = 2.25;
    double less = 0.0;
    double more = 0.0;
    double root = 0.0;
    /*w ww.j  ava2s .  c  o m*/
    less = floor(x);                            // Result is 2.0
    printf("%f",less);    
    
    more = ceil(x);                             // Result is 3.0
    
    printf("%f",more);    
    root = sqrt(x);                             // Result is 1.5  

    printf("%f",root);    
    return 0; 
}

Related Tutorials