MATH FUNCTIONS IN C

Dev Frank
7 min readApr 21, 2024

--

Mathematical operations are an essential part of programming, and C, being a general-purpose programming language, provides a robust set of math functions to perform various calculations.

In fact, did you know that the C standard library provides over 30 math functions to perform tasks such as trigonometry, exponentiation, logarithms, and more?

When working with numbers in C, have you ever wondered how to perform complex calculations like trigonometric functions, squares and so on.

In this topic, we’ll explore the MATH functions in C, how to use them, and some practical examples to get you started with calculations in C programming.

Math functions in C are pre-built functions that perform mathematical operations, making it easy to write programs that calculate various things.

To use these functions in C, you will have to include the math.h header file, which provides the math functions.

Think of them as tools (Math Functions) in your toolbox (math

Here are some basic math function in c

  1. abs(x): Gives the absolute value of x (makes negative numbers positive).
#include <math.h>
#include <stdio.h>

int main() {
double x = -5;

double result = abs(x); // Finds the absolute of -5

printf("The absolute value of -5 is: %f\n", result);

return 0;
}

2. pow(x, y): Raises x to the power of y (like x^y)

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

int main() {
double x = 2.0, y = 3.0;

double result = pow(x, y); // Finds 2.0 raised to the power 3.0

printf("%.2f raised to the power of %.2f is %.2f\n", x, y, result);

return 0;
}

3. sqrt(x): Finds the square root of x

#include <math.h>

int main() {
double x = 4.0;

double result = sqrt(x); // find the square root of 4

printf("The square root of 4 is: %f\n", result);


return 0;
}

4. sin(x), cos(x), tan(x): Trigonometric functions (useful for calculations involving triangles).

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

int main() {
double angle_degrees = 30.0;

double angle_radians = angle_degrees * (M_PI / 180.0);

double sin_result = sin(angle_radians);

double cos_result = cos(angle_radians);

double tan_result = tan(angle_radians);

printf("sin(%.2f degrees) = %.2f\n", angle_degrees, sin_result);
printf("cos(%.2f degrees) = %.2f\n", angle_degrees, cos_result);
printf("tan(%.2f degrees) = %.2f\n", angle_degrees, tan_result);
return 0;
}

This code calculates the sine, cosine, and tangent of an angle given in degrees by first converting the angle to radians. It then uses the sin(), cos(), and tan() functions from the math library to perform the calculations and prints the results along with the angle in degrees. Finally, it returns 0 to indicate successful execution.

In other words, Math functions in C are optimized, efficient, and accurate implementations of various mathematical operations, leveraging the underlying hardware and compiler optimizations. They are essential for scientific computing, numerical analysis, and machine learning applications.

Some advanced math functions in C include:

5. exp(x): Computes the exponential function (e^x)

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

int main() {
double x = 1.0;
double result = exp(x);
printf("Exponential of %.2f is %.2f\n", x, result);
return 0;
}

This code calculates the exponential function ( e^x ) where ( x ) is 1.0, using the exp() function from the math library. It then prints the result, which is approximately 2.72, along with the input value. Finally, it returns 0 to indicate successful execution.

6. log(x): Calculates the natural logarithm of x

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

int main() {
double x= 10.0;
double result = log(number);
printf("Natural logarithm of %.2f is %.2f\n", x, result);
return 0;
}

This code calculates the natural logarithm of the number 10.0 using the log() function from the math library, stores the result in the variable result, and then prints it along with the input value. Finally, it returns 0 to indicate successful execution.

7. fmod(x, y): Computes the remainder of x divided by y

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

int main() {
double x= 10.0, y= 3.0;
double result = fmod(x, y);
printf("Remainder of %.2f divided by %.2f is %.2f\n", x, y, result);
return 0;
}

This code calculates the remainder when 10.0 is divided by 3.0 using the fmod() function from the math library, stores the result in the variable result, and then prints it along with the input values. Finally, it returns 0 to indicate successful execution.

8. ceil(x), floor(x): Rounds x up or down to the nearest integer

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

int main() {
double x= 4.3;
double result = ceil(number);
printf("Ceiling value of %.2f is %.2f\n", number, result);
return 0;
}
#include <stdio.h>
#include <math.h>

int main() {
double x = 4.7;
double result = floor(x);
printf("Floor value of %.2f is %.2f\n", x, result);
return 0;
}

returns the smallest integer greater than or equal to x, while floor(x) returns the largest integer less than or equal to x. For example, ceil(4.3) returns 5, and floor(4.7) returns 4.

9. acos(x): Returns the arc cosine of x.

Example:

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

int main() {
double x = 0.5;
double result = acos(x);
printf("The arc cosine of 0.5 is: %f\n", result);
return 0;
}

returns the arc cosine of x, which is the angle whose cosine is x. For example, acos(0.5) returns the angle in radians whose cosine is 0.5, approximately 1.047 radians or 60 degrees.

10. asin(x): Returns the arc sine of x.

Example:

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

int main() {
double x = 0.5;
double result = asin(x);
printf("The arc sine of 0.5 is: %f\n", result);
return 0;
}

returns the arc sine of x, which is the angle whose sine is x. For example, asin(0.5) returns the angle in radians whose sine is 0.5, approximately 0.523 radians or 30 degrees.

11. atan(x): Returns the arc tangent of x.

Example:

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

int main() {
double x = 1;
double result = atan(x);
printf("The arc tangent of 1 is: %f\n", result);
return 0;
}

returns the arc tangent of x, which is the angle whose tangent is x. For example, atan(1) returns the angle in radians whose tangent is 1, approximately 0.785 radians or 45 degrees.

12. atan2(y, x): Returns the arc tangent of y/x.

Example:

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

int main() {
double y = 2;
double x = 3;
double result = atan2(y, x);
printf("The arc tangent of 2/3 is: %f\n", result);
return 0;
}

returns the arc tangent of y/x in the range [-π, π]. It considers the signs of both y and x to determine the quadrant of the result. For example, atan2(2, 3) returns the angle in radians whose tangent is 2/3, approximately 0.588 radians or 33.69 degrees.

13. ceil(x): Returns the smallest integer greater than or equal to x.

Example:

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

int main() {
double x = 3.14;
double result = ceil(x);
printf("The smallest integer greater than or equal to 3.14 is: %f\n", result);
return 0;
}

Returns the smallest integer greater than or equal to x. Example: ceil(3.14) returns 4.

14. cos(x): Returns the cosine of x.

Example:

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

int main() {
double x = 3.14;
double result = cos(x);
printf("The cosine of 3.14 is: %f\n", result);
return 0;
}

Returns the cosine of x. Example: cos(3.14) returns approximately -0.999.

15. cosh(x): Returns the hyperbolic cosine of x.

Example:

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

int main() {
double x = 1;
double result = cosh(x);
printf("The hyperbolic cosine of 1 is: %f\n", result);
return 0;
}

Returns the hyperbolic cosine of x. Example: cosh(1) returns approximately 1.543.

16. exp(x): Returns the exponential of x.

Example:

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

int main() {
double x = 1;
double result = exp(x);
printf("The exponential of 1 is: %f\n", result);
return 0;
}

Returns the exponential of x. Example: exp(1) returns approximately 2.718.

17. _fabs(x)_: Returns the absolute value of x.

Example:

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

int main() {
double x = -5;
double result = fabs(x);
printf("The absolute value of -5 is: %f\n", result);
return 0;
}

Returns the absolute value of x. Example: fabs(-5) returns 5.

18. _floor(x)_: Returns the largest integer less than or equal to x.

Example:

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

int main() {
double x = 3.14;
double result = floor(x);
printf("The largest integer less than or equal to 3.14 is: %f\n", result);
return 0;
}

Returns the largest integer less than or equal to x. Example: floor(3.14) returns 3.

19. fmod(x, y): Returns the remainder of x divided by y.

Example:

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

int main() {
double x = 5;
double y = 2;
double result = fmod(x, y);
printf("The remainder of 5 divided by 2 is: %f\n", result);
return 0;
}

Returns the remainder of x divided by y. Example: fmod(5, 2) returns 1.

20. frexp(x, exp): Returns the mantissa and exponent of x.

Example:

#include <math.h>
#include <stdio.h>
-
int main() {
double x = 3.14;
int exp;
double result = frexp(x, &exp);
printf("The mantissa and exponent of 3.14 are: %f and %d\n", result, exp);
return 0;
}

Returns the mantissa and exponent of x. Example: frexp(3.14, &exp) returns the mantissa 0.785 and the exponent 2.

These functions are crucial for tasks like:

- Scientific simulations
- Data analysis
- Machine learning algorithms
- Cryptography

I’ve covered a few math functions in C here, and I’m eager to expand our exploration! Feel free to share more functions or request explanations in the comments below. Let’s continue uncovering the world of mathematical operations in programming together!”

Amazing, you have reached the end and have already become more smarter, valuable, and productive just by learning about someMath Functions in in C

The next step is to implement them. Good luck!
Thanks for reading; if you liked my content and want to support me, the best way is to
1. Follow me on medium

2. Connect With Me On X, LinkedIn and Github, where I keep sharing such free amazing content on programming.

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.