Back to C Programming
2026-04-228 min read

Common mathematics functions

Learn Common mathematics functions step by step with clear examples and exercises.

Title: A full guide to Common Mathematics Functions in C Programming

Why This Matters

Mastering common mathematics functions is essential for solving real-world problems, enhancing your problem-solving skills, and acing programming interviews. These functions enable you to perform various operations like basic arithmetic, exponential calculations, trigonometry, and more using the C programming language.

Prerequisites

Before delving into mathematical functions in C, it's essential that you have a good understanding of:

  1. Basic C syntax and data types
  2. Variables and operators
  3. Control structures (if-else, loops)
  4. Functions and function prototypes
  5. Arrays and pointers
  6. File Input/Output (I/O) operations
  7. Understanding of floating-point numbers and their peculiarities in C
  8. Basic concepts of trigonometry and exponents
  9. Familiarity with the mathematical notation used in the functions' names

Core Concept

Basic Operations

C provides several built-in functions for basic mathematical operations:

  1. abs(x) - absolute value of x
  2. labs(x) - long absolute value of x
  3. llabs(x) - long long absolute value of x
  4. imaxabs(x, y) - maximum absolute value between x and y
  5. fabs(x) - floating-point absolute value of x
  6. div(a, b) - division operation that returns a struct containing quotient and remainder
  7. lldiv(a, b) - long long division operation that returns a struct containing quotient and remainder
  8. imaxdiv(a, b) - maximum division between two integers (returns a struct)
  9. fmod(x, y) - floating-point modulus operation
  10. remquo(x, y, p) - modulus operation with an optional divisor precision parameter
  11. fma(x, y, z) - floating-point multiplication and addition
  12. fdim(x, y) - floating-point difference (x - y if x >= y; 0 otherwise)

Exponential Functions

C offers functions for various exponential calculations:

  1. exp(x) - Euler's number e raised to the power of x
  2. exp10(x) - base-10 exponentiation
  3. exp2(x) - base-2 exponentiation
  4. expm1(x) - (e^x - 1)
  5. exp10m1(x) - (10^x - 1)
  6. exp2m1(x) - (2^x - 1)
  7. log(x) - natural logarithm of x
  8. log10(x) - base-10 logarithm of x
  9. log2(x) - base-2 logarithm of x
  10. log1p(x) - (log(1+x))
  11. logb(x) - binary logarithm of x (base 2)
  12. modf(x, y) - separates x into its integer and fractional parts

Trigonometric Functions

C provides functions for various trigonometric calculations:

  1. sin(x), cos(x), tan(x) - sine, cosine, tangent of angle x (in radians)
  2. asin(x), acos(x), atan(x) - inverse sine, inverse cosine, inverse tangent of x
  3. atan2(y, x) - arctangent of y/x in radians (returns quadrant)
  4. sinh(x), cosh(x), tanh(x) - hyperbolic sine, hyperbolic cosine, hyperbolic tangent of x
  5. asinh(x), acosh(x), atanh(x) - inverse hyperbolic sine, inverse hyperbolic cosine, inverse hyperbolic tangent of x

Floating-Point Manipulation Functions

C offers functions for various floating-point manipulations:

  1. ceil(x), floor(x), round(x) - rounding to the nearest integer (towards zero or away from zero)
  2. lround(x), llround(x) - long and long long rounding to the nearest integer
  3. nearbyint(x) - rounding to the nearest representable value
  4. rint(x), lrint(x), llrint(x) - rounding towards zero or away from zero, with long and long long support
  5. trunc(x) - truncating x (discarding fractional part)
  6. copysign(x, y) - returns the value of x with the sign of y
  7. canonicalize(x) - normalizes a NaN value to positive or negative infinity

Worked Example

Let's consider a simple example that demonstrates some of these functions:

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

int main() {
double x = 3.14;
int y = 5;

printf("Absolute value: %.2f\n", abs(x));
printf("Floating-point absolute value: %.2f\n", fabs(x));
printf("Sine of %.2f: %.2f\n", x, sin(x));
printf("Cosine of %.2f: %.2f\n", x, cos(x));
printf("Tangent of %.2f: %.2f\n", x, tan(x));
printf("Floating-point modulus: %.2f\n", fmod(x, 2.0));

double a = 10;
double b = -5;

printf("Maximum division: %d\n", imaxdiv(a, b).quot);
printf("Floating-point multiplication and addition: %.2f\n", fma(x, y, 1.0));

return 0;
}

Common Mistakes

  1. Forgetting to include the math library: Remember to include #include at the beginning of your code to use mathematical functions.
  2. Not handling errors: Some mathematical functions may produce undefined results (like division by zero or invalid arguments). Always check for these cases and handle them appropriately.
  3. Using the wrong function: Make sure you're using the correct function for your specific needs, as some functions have similar names but perform different operations.
  4. Misunderstanding the return types: Some functions (like div() and ldiv()) return a struct containing quotient and remainder, so you should use the appropriate members to access their values.
  5. Not accounting for rounding errors: Floating-point arithmetic can lead to rounding errors, especially when performing complex calculations or comparing floating-point numbers for equality.
  6. Incorrectly using trigonometric functions: Be aware that trigonometric functions in C use radians instead of degrees and handle negative angles properly.
  7. Ignoring the domain restrictions: Some mathematical functions have specific domain restrictions, so it's important to ensure your input values are within their valid ranges.
  8. Not understanding the precision limitations: Floating-point numbers have limited precision, which can lead to inaccurate results when performing complex calculations or comparing floating-point numbers for equality.
  9. Misusing floating-point arithmetic: Be aware of the peculiarities of floating-point arithmetic in C, such as the order of operations and the representation of special values like NaN and infinity.
  10. Not considering the performance implications: Some mathematical functions can have a significant impact on the performance of your program, so it's important to choose the most appropriate function for your specific needs.

Practice Questions

  1. Write a program that calculates the square root of a number using sqrt().
  2. Write a program that finds the greatest common divisor (GCD) of two numbers using fma() and fdim().
  3. Write a program that checks if a number is an integer power of 2 using log2().
  4. Write a program that calculates the factorial of a number using mathematical functions.
  5. Write a program that finds the roots of a quadratic equation given its coefficients.
  6. Write a program that solves a system of linear equations using Gaussian elimination and the built-in matrix operations in C.
  7. Write a program that calculates the area under a curve using numerical integration (e.g., Simpson's rule or trapezoidal rule).
  8. Write a program that implements Newton's method for finding the root of a function.
  9. Write a program that solves a differential equation using Euler's method or Runge-Kutta methods.
  10. Write a program that generates random numbers from a normal distribution (Gaussian distribution) using the Box-Muller transform.

FAQ

  1. Why can't I use division (/) for floating-point division?
  • In C, the / operator performs integer division when either operand is an integral type. To perform floating-point division, you should use the / operator with a floating-point type or the fdiv() function.
  1. What happens if I pass non-finite values (like NaN or infinity) to mathematical functions?
  • Some mathematical functions return special values like NaN or infinity when given non-finite arguments. It's essential to handle these cases appropriately, as they can lead to unexpected results.
  1. Why is there a difference between abs() and fabs()?
  • abs() is used for integer values, while fabs() is used for floating-point numbers. The two functions are different because the absolute value of an integer can be easily computed using bitwise operations, but floating-point numbers require more complex calculations.
  1. Why does the sin() function use radians instead of degrees?
  • The sin() function in C is defined to work with angles in radians because it's a more natural and consistent choice for mathematical functions. Degrees can be converted to radians using multiplication by PI / 180.
  1. Why does the exp() function return such a large number?
  • The exp() function returns Euler's number e raised to the power of x, which is approximately 2.71828. For large values of x, this results in an extremely large number.
  1. Why does the log() function return a negative value for some inputs?
  • The natural logarithm (base e) can return negative values when the input is less than 1. This is because the logarithm measures the inverse relationship between numbers and their exponents, so smaller numbers require larger exponents (and thus negative logarithms).
  1. What are some common mistakes to avoid when using trigonometric functions?
  • Be aware that trigonometric functions in C use radians instead of degrees, handle negative angles properly, and ensure your input values are within their valid ranges.
  1. Why is there a difference between sin() and asin()?
  • The sin() function computes the sine of an angle, while the asin() function computes the inverse sine (the angle whose sine is the given value).
  1. What are some common mistakes to avoid when using exponential functions?
  • Be aware that exponential functions can return extremely large or small numbers for large inputs and small inputs near zero, respectively. Also, ensure your input values are within their valid ranges and handle cases where the base is less than or equal to zero appropriately.
  1. What are some common mistakes to avoid when using floating-point manipulation functions?
  • Be aware of the peculiarities of floating-point arithmetic in C, such as the order of operations and the representation of special values like NaN and infinity. Also, ensure your input values are within their valid ranges and handle cases where the input is not a number appropriately.