Math Functions in C
×


Math Functions in C

296

 Math functions in C provide a variety of tools for performing mathematical operations in programs.

 These functions are available in the standard C library.

Here's a simplified overview of some common math functions along with examples:

 The library in C provides functions for performing various mathematical operations, including arithmetic, trigonometry, logarithms, exponentiation, and more.

 These functions accept numeric values as input and return computed results.

Common Math Functions in C 

1Arithmetic Functions:

 sqrt(x): Calculates the square root of x.

 pow(x, y) computes the result of raising the base x to the exponent y, effectively calculating x raised to the power of y.


2Trigonometric Functions:

 sin(x), cos(x), tan(x): Calculate sine, cosine, and tangent of x (in radians).

 asin(x), acos(x), atan(x): Calculate inverse sine, cosine, and tangent of x.


3Logarithmic Functions:

 log(x): Natural logarithm of x.

 log10(x): Base-10 logarithm of x.


4Exponential Functions:

 exp(x): Calculates the exponential (e^x) of x.

 exp2(x): Calculates 2 raised to the power of x.


5Rounding Functions:

 ceil(x): Rounds x upwards to the nearest integer.

 floor(x): Rounds x downwards to the nearest integer.

 round(x): Rounds x to the nearest integer.

Example:

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

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

    printf("Square root of %.2f is %.2f\n", x, sqrt(x));
    printf("%.2f raised to power %.2f is %.2f\n", x, y, pow(x, y));
    printf("Sine of %.2f radians is %.2f\n", x, sin(x));
    printf("Natural logarithm of %.2f is %.2f\n", x, log(x));
    printf("Ceil of %.2f is %.2f\n", x, ceil(x));

    return 0;
}
Output:

Square root of 2.50 is 1.58
2.50 raised to the power 1.50 is 3.95
Sine of 2.50 radians is 0.60
Natural logarithm of 2.50 is 0.92
Ceil of 2.50 is 3.00

 In this example, we include to access math functions.

 We then use various math functions to perform calculations on numeric values, printing the results to the console.

 Math functions in C provide powerful capabilities for performing mathematical operations, allowing you to handle complex calculations with ease.

By leveraging these functions, you can enhance the functionality and accuracy of your C programs, particularly in scientific, engineering, and mathematical applications.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments