BKNMU Junagadh | BCA Sem 1 | Unit 3
Complete Guide to Predefined Math Functions in C language
Detailed breakdown of Mathematical Functions (pow, sqrt, ceil, floor, fabs, fmod, log, exp) defined in math.h for BCA Unit 3
#include <math.h> header file. This post provides a clear, exam-oriented overview of these utilities for **BKNMU BCA Semester 1 (Unit 3)**.
The math.h library organizes its utilities into four primary functional categories, corresponding to the quadrants in your thumbnail:
1. Power & Roots
Used to calculate advanced exponents ($x^2$, $a^b$) and square roots. **Essential Rule: Most input/output values are of type `double`.**
2. Rounding Utilities
Precision control functions that round floating-point numbers up to the next integer (`ceil`) or down to the previous integer (`floor`). Returns a double.
3. Absolute & Remainder
Advanced Arithmetic: `fabs` finds the absolute value of a floating-point number. `fmod` calculates the remainder of floating-point division (like %).
4. Exponential & Log
Used for scientific and statistical calculations: `exp` computes $e^x$, natural log is found with `log()`, and base-10 log with `log10()`.
Here is a detailed breakdown of the primary functions supplied by the math.h standard library, detailing their specific parameter requirements and return behaviors.
| Function Prototype | Functional Description | Input Parameter (Inputs) | Return Value (Output) (Typ. Double) |
|---|---|---|---|
double pow(double b, double e) | Calculates the result of base raised to the exponent power ($base^{exponent}$). | Base (double), Exponent (double). | The power result (e.g., pow(2.0, 3.0) = 8.0). |
double sqrt(double x) | Computes the square root of a non-negative floating-point number. | Number (double, must be $\geq 0$). | The square root result (e.g., sqrt(16.0) = 4.0). |
double ceil(double x) | Rounding: Returns the smallest integer value that is greater than or equal to x (Rounds UP). | Floating point value (double). | Rounded integer (returned as double, e.g., ceil(3.2) = 4.0). |
double floor(double x) | Rounding: Returns the largest integer value that is less than or equal to x (Rounds DOWN). | Floating point value (double). | Rounded integer (returned as double, e.g., floor(3.7) = 3.0). |
double fabs(double x) | Computes the absolute value of a **floating-point number**. Use `abs()` for integers. | Floating point value (double). | The positive value (e.g., fabs(-5.5) = 5.5). |
double fmod(double x, double y) | Calculates the remainder of floating-point division ($x / y$). Like the `%` operator for floats. | Dividend (x double), Divisor (y double). | The floating point remainder. |
double exp(double x) | Computes the exponential value of Euler's number 'e' raised to the power of x ($e^x$). | Exponent value (double). | The exponential result (e.g., exp(1.0) $\approx$ 2.718). |
double log(double x) | Computes the natural logarithm (base e) of a number. (For base-10, use `log10()`). | Value (double, must be $> 0$). | The natural logarithm result. |
math.h are designed to accept and return values of type `double` (high-precision floating point). While C automatically promotes other types like float or int to `double` during the function call, you must ensure that your return variable is declared as `double` to maintain accuracy.
1. Using Power, Roots, and Rounding Functions:
#include <stdio.h>
#include <math.h> // Required for math functions
int main() {
double base = 3.0, exp_val = 4.0, result;
double num = 25.0, root_val;
double val_ceil = 10.1, val_floor = 10.9;
// 1. pow(): Calculate 3^4
result = pow(base, exp_val);
printf("%.1f raised to %.1f = %.1f\n", base, exp_val, result); // 81.0
// 2. sqrt(): Find square root of 25
root_val = sqrt(num);
printf("Square root of %.1f = %.1f\n", num, root_val); // 5.0
// 3. ceil(): Rounding UP
printf("Ceiling of %.1f = %.1f\n", val_ceil, ceil(val_ceil)); // 11.0
// 4. floor(): Rounding DOWN
printf("Floor of %.1f = %.1f\n", val_floor, floor(val_floor)); // 10.0
return 0;
}
2. Using fabs(), exp(), and log() in a program:
#include <stdio.h>
#include <math.h>
int main() {
double neg_val = -9.5, exp_result, log_result;
// 1. fabs(): Absolute value
printf("Absolute value of %.1f = %.1f\n", neg_val, fabs(neg_val)); // 9.5
// 2. exp(): Euler's number 'e' to the power of 1
exp_result = exp(1.0);
printf("e^1.0 $\approx$ %.4f\n", exp_result); // 2.7183
// 3. log(): Natural log of Euler's number (base e)
log_result = log(exp_result);
printf("ln(e^1.0) = %.1f\n", log_result); // 1.0 (inverse operations)
return 0;
}
Step 1: Header Preprocessor Directive
The preprocessor loads declarations from math.h. This allows the compiler to understand the mathematical functions, their prototypes, and strict data type requirements before processing your code.
Step 2: Linking with Math Library (Crucial on Linux/GCC)
Standard libraries (like I/O) link automatically. However, mathematical functions (defined in `libm`) are not in the default library list in some environments (like GCC on Linux). To fix "undefined reference to pow/sqrt" errors, you must explicitly link using the `-lm` flag during compilation: gcc myprogram.c -o myprogram -lm.
Step 3: Internal Optimized Execution
Precompiled binary instructions mapped to hardware floating-point units execute standard operations (e.g., CORDIC algorithms for sin/cos or optimized lookup tables), fulfilling the low-level request instantly.
fmod() function from math.h.
sqrt() with a negative input (e.g., `sqrt(-1.0)`) is invalid in C and will result in a **Domain Error**, with the function returning a special, invalid floating-point output value: NaN (Not a Number). You must ensure input is $\geq 0$.
- Full Header Name: Mathematical Functions Header (
math.h). - Header Type: Standard ANSI C library preprocessor directive (use
<>). - Mandatory Data Type Rule: Almost all functions strictly accept and return values of type **`double`**.
- Key Functions to Remember:
pow(base, exp),sqrt(x),ceil(x),floor(x),fabs(x),fmod(x, y),exp(x),log(x),log10(x). - Rounding Distinction: `ceil` rounds UP to next integer; `floor` rounds DOWN to previous integer.
- GCC Compilation Rule: Explicitly link on Linux/GCC using the `-lm` flag to resolve linker errors.