The Wayback Machine - https://web.archive.org/web/20240527224610/https://www.geeksforgeeks.org/c-library-math-h-functions/
Open In App

C Library math.h Functions

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The math.h header defines various C mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result. Let us discuss some important C math functions one by one.

C Math Functions

1. double ceil (double x)

The C library function double ceil (double x) returns the smallest integer value greater than or equal to x.

Syntax

double ceil(double x);

Example

C




// C code to illustrate
// the use of ceil function.
#include <math.h>
#include <stdio.h>
  
int main()
{
    float val1, val2, val3, val4;
  
    val1 = 1.6;
    val2 = 1.2;
    val3 = -2.8;
    val4 = -2.3;
  
    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 = 2.0
value2 = 2.0
value3 = -2.0
value4 = -2.0

2. double floor(double x)

The C library function double floor(double x) returns the largest integer value less than or equal to x.

Syntax

double floor(double x);

Example

C




// C code to illustrate
// the use of floor function
#include <math.h>
#include <stdio.h>
  
int main()
{
    float val1, val2, val3, val4;
  
    val1 = 1.6;
    val2 = 1.2;
    val3 = -2.8;
    val4 = -2.3;
  
    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 = 1.0
Value2 = 1.0
Value3 = -3.0
Value4 = -3.0

3. double fabs(double x)

The C library function double fabs(double x) returns the absolute value of x.

Syntax

syntax : double fabs(double x)

Example

C




// C code to illustrate
// the use of fabs function
#include <math.h>
#include <stdio.h>
  
int main()
{
    int a, b;
    a = 1234;
    b = -344;
  
    printf("The absolute value of %d is %lf\n", a, fabs(a));
    printf("The absolute value of %d is %lf\n", b, fabs(b));
  
    return (0);
}


Output

The absolute value of 1234 is 1234.000000
The absolute value of -344 is 344.000000

4. double log(double x)

The C library function double log(double x) returns the natural logarithm (base-e logarithm) of x. 

Syntax

double log(double x)

Example

C




// C code to illustrate
// the use of log function
  
#include <math.h>
#include <stdio.h>
  
int main()
{
    double x, ret;
    x = 2.7;
  
    /* finding log(2.7) */
    ret = log(x);
    printf("log(%lf) = %lf", x, ret);
  
    return (0);
}


Output

log(2.700000) = 0.993252

5. double log10(double x)

The C library function double log10(double x) returns the common logarithm (base-10 logarithm) of x. 

Syntax

double log10(double x);

Example

C




// C code to illustrate
// the use of log10 function
#include <math.h>
#include <stdio.h>
  
int main()
{
    double x, ret;
    x = 10000;
  
    /* finding value of log1010000 */
    ret = log10(x);
    printf("log10(%lf) = %lf\n", x, ret);
  
    return (0);
}


Output

log10(10000.000000) = 4.000000

6. double fmod(double x, double y)

The C library function double fmod(double x, double y) returns the remainder of x divided by y. 

Syntax

double fmod(double x, double y) 

Example

C




// C code to illustrate
// the use of fmod function
#include <math.h>
#include <stdio.h>
  
int main()
{
    float a, b;
    int c;
    a = 8.2;
    b = 5.7;
    c = 3;
    printf("Remainder of %f / %d is %lf\n", a, c,
           fmod(a, c));
    printf("Remainder of %f / %f is %lf\n", a, b,
           fmod(a, b));
  
    return (0);
}


Output 

Remainder of 8.200000 / 3 is 2.200000
Remainder of 8.200000 / 5.700000 is 2.500000

7. double sqrt(double x)

The C library function double sqrt(double x) returns the square root of x. 

Syntax

double sqrt(double x);

Example

C




// C code to illustrate
// the use of sqrt function
#include <math.h>
#include <stdio.h>
  
int main()
{
  
    printf("Square root of %lf is %lf\n", 225.0,
           sqrt(225.0));
    printf("Square root of %lf is %lf\n", 300.0,
           sqrt(300.0));
  
    return (0);
}


Output

Square root of 225.000000 is 15.000000
Square root of 300.000000 is 17.320508

8. double pow(double x, double y)

The C library function double pow(double x, double y) returns x raised to the power of y i.e. xy.

Syntax

double pow(double x, double y);

Example

C




// C code to illustrate
// the use of pow function
#include <math.h>
#include <stdio.h>
  
int main()
{
    printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));
  
    printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
  
    return (0);
}


Output

Value 8.0 ^ 3 = 512.000000
Value 3.05 ^ 1.98 = 9.097324

9. double modf(double x, double *integer)

The C library function double modf(double x, double *integer) returns the fraction component (part after the decimal), and sets integer to the integer component.

Syntax

double modf(double x, double *integer)

Example

C




// C code to illustrate
// the use of modf function
#include <math.h>
#include <stdio.h>
  
int main()
{
    double x, fractpart, intpart;
  
    x = 8.123456;
    fractpart = modf(x, &intpart;);
  
    printf("Integral part = %lf\n", intpart);
    printf("Fraction Part = %lf \n", fractpart);
  
    return (0);
}


Output

Integral part = 8.000000
Fraction Part = 0.123456 

10. double exp(double x)

The C library function double exp(double x) returns the value of e raised to the xth power.

Syntax

double exp(double x);

Example

C




// C code to illustrate
// the use of exp function
#include <math.h>
#include <stdio.h>
  
int main()
{
    double x = 0;
  
    printf("The exponential value of %lf is %lf\n", x,
           exp(x));
    printf("The exponential value of %lf is %lf\n", x + 1,
           exp(x + 1));
    printf("The exponential value of %lf is %lf\n", x + 2,
           exp(x + 2));
  
    return (0);
}


Output

The exponential value of 0.000000 is 1.000000
The exponential value of 1.000000 is 2.718282
The exponential value of 2.000000 is 7.389056

11. double cos(double x)

The C library function double cos(double x) returns the cosine of a radian angle x. 

Syntax

double cos(double x);

The same syntax can be used for other trigonometric functions like sin, tan, etc. 

Example

C




// C code to illustrate
// the use of cos function
#include <math.h>
#include <stdio.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

The cosine of 60.000000 is 0.500000 degrees
The cosine of 90.000000 is 0.000000 degrees

12. double acos(double x)

The C library function double acos(double x) returns the arc cosine of x in radians.

Syntax

double acos(double x);

The same syntax can be used for other arc trigonometric functions like asin, atan etc.

Example

C




// C code to illustrate
// the use of acos function
#include <math.h>
#include <stdio.h>
  
#define PI 3.14159265
  
int main()
{
    double x, ret, val;
  
    x = 0.9;
    val = 180.0 / PI;
  
    ret = acos(x) * val;
    printf("The arc cosine of %lf is %lf degrees", x, ret);
  
    return (0);
}


Output

The arc cosine of 0.900000 is 25.841933 degrees

13. double tanh(double x)

The C library function double tanh(double x) returns the hyperbolic tangent of x.

Syntax

double tanh(double x);

The same syntax can be used for other hyperbolic trigonometric functions like sinh, cosh etc.

Example

C




// C code to illustrate
// the use of tanh function
#include <math.h>
#include <stdio.h>
  
int main()
{
    double x, ret;
    x = 0.5;
  
    ret = tanh(x);
    printf("The hyperbolic tangent of %lf is %lf degrees",
           x, ret);
  
    return (0);
}


Output

The hyperbolic tangent of 0.500000 is 0.462117 degrees


Previous Article
Next Article

Similar Reads

PHP Math Functions Complete Reference
The predefined math functions in PHP are used to handle the mathematical operations within the integer and float types. These math functions are part of the PHP core. Installation: These functions have not required any installation. The complete list of PHP math functions are given below: Example: Programs illustrate the decbin() function in PHP: C
3 min read
SQL Functions (Aggregate and Scalar Functions)
SQL Functions are built-in programs that are used to perform different operations on the database. There are two types of functions in SQL: Aggregate FunctionsScalar FunctionsSQL Aggregate FunctionsSQL Aggregate Functions operate on a data group and return a singular output. They are mostly used with the GROUP BY clause to summarize data.  Some com
4 min read
&lt;strings&gt; library in C++ STL
Member functions String.constructor : Construct string object (public member function ).String.destructor : String destructor (public member function )String.operator= : String assignment (public member function ) Iterators Begin : Return iterator to beginning (public member function )End : Return iterator to end (public member function )Advance :
3 min read
&lt;numeric&gt; library in C++ STL
Common mathematical functions std::fabs: This function returns the absolute value. std::sqrt: This function returns the square root std::sin: This function returns the sine measured in radians. Special mathematical functions std::beta: This function evaluates the (complete) Beta integral with given parameters std::hermite: This function Returns the
2 min read
&lt;regex&gt; library in C++ STL
Main classes These classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters. basic_regex: Regular expression object (class template) sub_match: Identifies the sequence of characters matched by a sub-expression (class template) match_results: Identifies one regular expression ma
2 min read
RSA Algorithm using Multiple Precision Arithmetic Library
Public Key Cryptography is also known as Asymmetric Cryptography is the type of cryptography which involves the usage of two keys namely Public Key and Private Key. The Public Key of the receiver is used to encrypt the plaintext by the sender while the Private Key of the receiver is used to decrypt the encrypted message and hence can be decrypted o
4 min read
Information Technology Infrastructure Library (ITIL)
Information Technology Infrastructure Library (ITIL) is provided with a framework of best practices for delivering IT services. The ITIL is an appropriate method to management of IT service and also management can help businesses manage risk, strengthen customer relations, establish cost-effective practices, and build a stable IT environment that p
3 min read
Tokenization Using Spacy library
Before moving to the explanation of tokenization, let's first discuss what is Spacy. Spacy is a library that comes under NLP (Natural Language Processing). It is an object-oriented Library that is used to deal with pre-processing of text, and sentences, and to extract information from the text using modules and functions. Tokenization is the proces
3 min read
How to Use Picasso Image Loader Library in Android?
Picasso is open source and one of the widely used image download libraries in Android. It is created and maintained by Square. It is among the powerful image download and caching library for Android. Picasso simplifies the process of loading images from external URLs and displays them on your application. For example, downloading an image from the
6 min read
Containers in C++ STL (Standard Template Library)
A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows great flexibility in the types supported as elements. The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators (reference ob
2 min read
Article Tags :
Practice Tags :