C Math Functions
Hello Everyone, In this article, we will explore C Math Functions, which are built-in functions in the C programming language that help perform various mathematical operations. From basic calculations like addition and subtraction to more complex functions like trigonometry and logarithms, C Math Functions make it easier to work with numbers and solve math problems in your programs. Whether you are new to C or have some experience, this guide will help you to understand basics of Math Function in C effectively with examples. Let’s get started.
C Math Functions are a part of the math library in C programming. They provide a variety of mathematical operations and functions, such as trigonometric, logarithmic, exponential, and rounding functions. To use these functions, you need to include the math.h
header file in your program.
Here’s an example of some commonly used C Math Functions:
#include <stdio.h>
#include <math.h>
int main() {
double num1 = 9.0;
double num2 = 2.0;
// sqrt() function
double square_root = sqrt(num1);
printf("Square root of %.1f: %.1f\\n", num1, square_root);
// pow() function
double power = pow(num1, num2);
printf("%.1f raised to the power of %.1f: %.1f\\n", num1, num2, power);
// sin() function
double sine = sin(num1);
printf("Sine of %.1f: %.1f\\n", num1, sine);
// log() function
double natural_log = log(num1);
printf("Natural logarithm of %.1f: %.1f\\n", num1, natural_log);
// ceil() function
double number = 4.8;
double ceiling = ceil(number);
printf("Ceiling of %.1f: %.1f\\n", number, ceiling);
// floor() function
double floor_val = floor(number);
printf("Floor of %.1f: %.1f\\n", number, floor_val);
return 0;
}
Output:
Square root of 9.0: 3.0
9.0 raised to the power of 2.0: 81.0
Sine of 9.0: 0.4
Natural logarithm of 9.0: 2.2
Ceiling of 4.8: 5.0
Floor of 4.8: 4.0
Here’s a table of some common C Math Functions:
Function | Description |
---|---|
sqrt(x) | Calculates the square root of x |
pow(x, y) | Calculates x raised to the power of y |
sin(x) | Calculates the sine of x (x in radians) |
cos(x) | Calculates the cosine of x (x in radians) |
tan(x) | Calculates the tangent of x (x in radians) |
log(x) | Calculates the natural logarithm of x |
log10(x) | Calculates the base-10 logarithm of x |
ceil(x) | Returns the smallest integer value greater than or equal to x |
floor(x) | Returns the largest integer value less than or equal to x |
fabs(x) | Returns the absolute value of x (for floating-point numbers) |
round(x) | Rounds x to the nearest integer value |
Function | Description |
---|---|
exp(x) | Calculates the exponential (e^x) of x |
cosh(x) | Calculates the hyperbolic cosine of x (x in radians) |
sinh(x) | Calculates the hyperbolic sine of x (x in radians) |
tanh(x) | Calculates the hyperbolic tangent of x (x in radians) |
acos(x) | Calculates the arc cosine of x (result in radians) |
asin(x) | Calculates the arc sine of x (result in radians) |
atan(x) | Calculates the arc tangent of x (result in radians) |
atan2(y, x) | Calculates the arc tangent of y/x (result in radians) |
fmod(x, y) | Calculates the floating-point remainder of x/y |
modf(x, &intpart) | Splits x into integer and fractional parts (intpart is the integer part) |
hypot(x, y) | Calculates the hypotenuse of a right-angled triangle given x and y as the lengths of the other two sides |
cbrt(x) | Calculates the cube root of x |
frexp(x, &exp) | Splits x into a normalized fraction and an integral power of 2 |
ldexp(x, exp) | Calculates x * 2^exp |
scalbn(x, n) | Calculates x * FLT_RADIX^n (FLT_RADIX is the base of the floating-point arithmetic) |
erf(x) | Calculates the error function of x |
erfc(x) | Calculates the complementary error function of x |
tgamma(x) | Calculates the gamma function of x |
lgamma(x) | Calculates the natural logarithm of the absolute value of the gamma function of x |
copysign(x, y) | Returns the value of x with the same sign as y |
fdim(x, y) | Calculates the positive difference between x and y |