Math Function in C

Math Function in C. Math Function in C Tutorial. Learn Math Function in C
Math Function in C.

What is Math Function in C ?

In C, math functions are a set of built-in functions provided by the math library (math.h header file) that allow you to perform various mathematical operations. These functions are used to perform calculations involving numbers, such as trigonometric calculations, logarithmic calculations, exponential calculations, etc.

Math functions are used to simplify complex mathematical calculations and provide a standardized way of performing common operations. They can be helpful in various domains such as scientific computing, engineering, financial analysis, and more.

Here is a table summarizing some commonly used math functions in C:

FunctionDescription
sqrt(x)Returns the square root of x.
pow(x, y)Returns x raised to the power of y.
exp(x)Returns the exponential value of x (e^x).
log(x)Returns the natural logarithm of x (base e).
log10(x)Returns the logarithm of x (base 10).
sin(x)Returns the sine of x (in radians).
cos(x)Returns the cosine of x (in radians).
tan(x)Returns the tangent of x (in radians).
floor(x)Returns the largest integer less than or equal to x.
ceil(x)Returns the smallest integer greater than or equal to x.
fabs(x)Returns the absolute value of x.
fmod(x, y)Returns the remainder of x divided by y.

Example of Math Function in C:

Now, let’s see an example which has all math functions.

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

int main() {
    double x = 1.5;
    double y = 2.0;

    double sqrtResult = sqrt(x);
    double powResult = pow(x, y);
    double expResult = exp(x);
    double logResult = log(x);
    double log10Result = log10(x);
    double sinResult = sin(x);
    double cosResult = cos(x);
    double tanResult = tan(x);
    double floorResult = floor(x);
    double ceilResult = ceil(x);
    double fabsResult = fabs(x);
    double fmodResult = fmod(x, y);

    printf("Square root of %.2f: %.2f\n", x, sqrtResult);
    printf("%.2f raised to the power %.2f: %.2f\n", x, y, powResult);
    printf("Exponential value of %.2f: %.2f\n", x, expResult);
    printf("Natural logarithm of %.2f: %.2f\n", x, logResult);
    printf("Logarithm of %.2f (base 10): %.2f\n", x, log10Result);
    printf("Sine of %.2f: %.2f\n", x, sinResult);
    printf("Cosine of %.2f: %.2f\n", x, cosResult);
    printf("Tangent of %.2f: %.2f\n", x, tanResult);
    printf("Floor value of %.2f: %.2f\n", x, floorResult);
    printf("Ceiling value of %.2f: %.2f\n", x, ceilResult);
    printf("Absolute value of %.2f: %.2f\n", x, fabsResult);
    printf("Remainder of %.2f divided by %.2f: %.2f\n", x, y, fmodResult);

    return 0;
}

Output:

Square root of 1.50: 1.22
1.50 raised to the power 2.00: 2.25
Exponential value of 1.50: 4.48
Natural logarithm of 1.50: 0.41
Logarithm of 1.50 (base 10): 0.18
Sine of 1.50: 0.99
Cosine of 1.50: 0.07
Tangent of 1.50: 14.10
Floor value of 1.50: 1.00
Ceiling value of 1.50: 2.00
Absolute value of 1.50: 1.50
Remainder of 1.50 divided by 2.00: 1.50

Explanation:

In aove example, each math function is applied to the value of x, and the corresponding result is printed out using printf. The example demonstrates the usage and output of all the math functions discussed earlier.

More: