C Library Math.h Functions https://www.skillvertex.com/blog Fri, 10 May 2024 07:30:06 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png C Library Math.h Functions https://www.skillvertex.com/blog 32 32 C Library Math.h Functions https://www.skillvertex.com/blog/c-library-math-h-functions/ https://www.skillvertex.com/blog/c-library-math-h-functions/#respond Fri, 10 May 2024 07:30:06 +0000 https://www.skillvertex.com/blog/?p=2596 Read more]]>

Table of Contents

C Library Math.h Functions

The math.h library in C is like a toolbox full of tools for doing math in computer programs. It helps C programmers perform various calculations, like adding, subtracting, finding square roots, and more. These tools work with numbers that can have decimal points. Whether you’re building a game, a financial application, or anything that involves math, math.h has the functions you need. So, it’s like having a math helper in your C program to make complex math tasks easier. In this introduction, we’ll look at some of the essential math.h functions and how they can be used in C programming.

C Math Functions

1. double ceil (double x)

The C library function double ceil(double x) is used to round a given floating-point number x up to the smallest integer value that is greater than or equal to x. In other words, it takes a decimal number and “rounds up” to the nearest whole number that is greater than or equal to the original value.

For example, if you call ceil(4.3), it will return 5.0 because 4.3 rounded up is 5. Similarly, if you call ceil(4.0), it will also return 4.0 because 4.0 is already a whole number and doesn’t need to be rounded up.

This function is useful when you need to ensure that a value is rounded up to the next integer, which can be important in various mathematical and programming scenarios.

Syntax

double ceil(double x);

Example

// 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) is used to round a given floating-point number x down to the largest integer value that is less than or equal to x. In other words, it takes a decimal number and “rounds down” to the nearest whole number that is less than or equal to the original value.

For example, if you call floor(4.7), it will return 4.0 because 4.7 rounded down is 4. Similarly, if you call floor(4.0), it will return 4.0 because 4.0 is already a whole number and doesn’t need to be rounded down.

This function is useful when you need to ensure that a value is rounded down to the nearest integer, which can be important in various mathematical and programming scenarios.

Syntax

double floor(double x);

Example

// 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) is used to calculate and return the absolute value of a given floating-point number x. The absolute value of a number is its distance from zero on the number line, and it is always positive or zero.

For example:

  • If you call fabs(5.5), it will return 5.5 because the absolute value of 5.5 is 5.5.
  • If you call fabs(-3.8), it will return 3.8 because the absolute value of -3.8 is 3.8.
  • If you call fabs(0.0), it will return 0.0 because the absolute value of 0.0 is 0.0.

This function is useful when you want to ignore the sign of a number and work with its magnitude. It’s commonly used in various mathematical and programming applications.

Syntax

syntax : double fabs(double x)

Example


// 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) is used to calculate and return the natural logarithm (base-e logarithm) of a given positive floating-point number x. The natural logarithm is the logarithm to the base ‘e,’ where ‘e’ is approximately equal to 2.71828.

In mathematical notation, the natural logarithm of x is represented as “ln(x).” It is the inverse of the exponential function, and it helps solve exponential growth and decay problems.

For example:

  • If you call log(2.0), it will return approximately 0.693147 because ln(2) is approximately 0.693147.
  • If you call log(10.0), it will return approximately 2.302585 because ln(10) is approximately 2.302585.

This function is useful in various scientific, engineering, and mathematical computations where the natural logarithm is required to model or analyze data and functions.

Syntax

double log(double x)

Example



// 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) is used to calculate and return the common logarithm (base-10 logarithm) of a given positive floating-point number x. The common logarithm is the logarithm to the base 10.

In mathematical notation, the common logarithm of x is typically written as “log(x)” without a base, which implies the base 10 logarithm.

For example:

  • If you call log10(100.0), it will return 2.0 because log(100) with base 10 is 2.
  • If you call log10(1000.0), it will return 3.0 because log(1000) with base 10 is 3.

This function is useful in various scientific, engineering, and mathematical computations, especially when working with quantities that are naturally expressed in base-10 logarithmic scales, such as orders of magnitude, decibels, or other logarithmic measurements.

Syntax

double log10(double x);

Example


// 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) is used to calculate and return the remainder when a floating-point number x is divided by another floating-point number y. It returns the fractional part of the quotient.

For example:

  • If you call fmod(10.5, 3.0), it will return 1.5 because the remainder when 10.5 is divided by 3.0 is 1.5.
  • If you call fmod(8.0, 2.5), it will return 0.5 because the remainder when 8.0 is divided by 2.5 is 0.5.
  • If you call fmod(7.0, 2.0), it will return 1.0 because the remainder when 7.0 is divided by 2.0 is 1.0.

This function is particularly useful when you need to perform modulo or remainder operations on floating-point numbers, which can occur in various mathematical and programming scenarios, such as signal processing, physics simulations, and financial calculations.

Syntax

double fmod(double x, double y) 

Example

// 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)



// 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.32050

8. double pow(double x, double y)

The C library function double pow(double x, double y) is used to calculate and return the result of raising a given floating-point number x to the power of another floating-point number y. In other words, it computes the value of x raised to the y power, which is denoted as x^y.

For example:

  • If you call pow(2.0, 3.0), it will return 8.0 because 2^3 equals 8.
  • If you call pow(5.0, 0.5), it will return 2.236068 because 5^0.5 is approximately equal to 2.236068.

This function is commonly used for various mathematical calculations, such as exponentiation, exponential growth, and complex mathematical modeling. It allows you to calculate the result of raising a number to a specific power, even when that power is not an integer.

Syntax

double pow(double x, double y);

Example


// 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) is used to separate a given floating-point number x into its integer and fractional components. It returns the fractional part of x as a double value and sets the integer part of x to the address pointed to by the integer pointer.

Here’s how it works:

  • The function takes two arguments: x, the input floating-point number, and integer, a pointer to a double where the integer part of x will be stored.
  • It returns the fractional part of x as a double value.

Syntax

double modf(double x, double *integer)

Example

// 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 the mathematical constant ‘e’ (approximately equal to 2.71828) raised to the power of a given floating-point number x. In mathematical notation, this is represented as “e^x,” and it calculates the exponential function.

For example:

  • If you call exp(1.0), it will return approximately 2.71828 because e^1 is approximately equal to 2.71828.
  • If you call exp(2.0), it will return approximately 7.38906 because e^2 is approximately equal to 7.38906.

This function is commonly used in various scientific, engineering, and mathematical calculations where exponential growth or decay is involved. It’s a fundamental function for working with exponential functions in C programs.

Syntax

double exp(double x);

Example

// 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) is used to calculate and return the cosine of a radian angle x. It takes an angle in radians as input and computes the cosine of that angle.

In mathematical notation, the cosine function is denoted as cos(x), where x represents the angle in radians.

For example:

  • If you call cos(0.0), it will return 1.0 because the cosine of 0 radians is 1.
  • If you call cos(π) (pi radians), it will return -1.0 because the cosine of pi radians is -1.
  • If you call cos(π/2) (pi/2 radians), it will return 0.0 because the cosine of pi/2 radians is 0.

This function is essential in trigonometry and is commonly used in mathematical and scientific computations, especially when dealing with angles and waveforms.

Syntax

double cos(double x);

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

Example


// 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) is used to calculate and return the arc cosine (inverse cosine) of a given value x. It takes a value between -1 and 1 (inclusive) as input and returns the corresponding angle in radians.

In mathematical notation, the arc cosine function is denoted as acos(x), and it represents the angle whose cosine is x.

For example:

  • If you call acos(1.0), it will return 0.0 radians because the cosine of 0 radians is 1.
  • If you call acos(0.0), it will return 1.570796 radians (approximately π/2) because the cosine of π/2 radians is 0.

This function is often used when you need to find an angle given its cosine value or when dealing with trigonometric calculations involving angles and side lengths of triangles.

Syntax

double acos(double x);

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

Example

// 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) is used to calculate and return the hyperbolic tangent (tanh) of a given value x. The hyperbolic tangent is a trigonometric function, similar to the regular tangent function but with some differences in behavior.

In mathematical notation, the hyperbolic tangent function is denoted as tanh(x) and is defined as (e^x - e^(-x)) / (e^x + e^(-x)), where e is Euler’s number, approximately equal to 2.71828.

For example:

  • If you call tanh(0.0), it will return 0.0 because the hyperbolic tangent of 0 is 0.
  • If you call tanh(1.0), it will return approximately 0.761594 because that’s the hyperbolic tangent of 1.

The hyperbolic tangent is used in various mathematical and scientific applications, especially in areas like statistics, signal processing, and neural networks. It describes the relationship between exponential growth and exponential decay in a way similar to how the regular tangent function describes oscillations in trigonometry.

Syntax

double tanh(double x);

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

Example

// 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

FAQ- C Library Math.h Functions

Q1.Is there a math library for C?

Ans. The C standard library provides a set of functions and tools that expand the capabilities of the C programming language. The math.h header file, in particular, offers a collection of mathematical functions, making it easier to perform complex math calculations in C programs. These functions cover arithmetic, exponentiation, logarithms, trigonometry, and more, allowing programmers to work with numbers and mathematical concepts efficiently. It’s a valuable resource for solving mathematical and scientific problems in C.

Q2.Why do we use #include math h in C?

Ans. The #include <math.h> directive is used to include the math.h header file in a C program. This header file provides access to a variety of mathematical functions, including sine, cosine, square root, and exponentiation (pow), among others. By including math.h, you can use these mathematical functions in your C program to perform a wide range of mathematical calculations and operations.

Q3. How to install a math library in C?

Ans. In some situations, especially when using certain math functions, you may need to link the math library explicitly by adding the -lm flag to the gcc compiler command when compiling your C code. However, it’s not always necessary for every math function.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/c-library-math-h-functions/feed/ 0