YouTip LogoYouTip

C Function Exp

# C Library Function - exp() The `exp()` function is a built-in mathematical function in the C standard library ``. It is used to calculate the exponential value of a given number, which is $e$ raised to the power of $x$ ($e^x$), where $e$ is the base of the natural logarithm (approximately equal to `2.71828`). This function is widely used in scientific computing, financial modeling, physics simulations, statistics, and engineering applications. --- ## Declaration The `` header file provides three variations of the exponential function to support different floating-point precisions: ```c #include double exp(double x); float expf(float x); long double expl(long double x); ``` ### Parameters * **`x`**: The floating-point value representing the exponent. ### Return Value * Returns the value of $e^x$. * If the result is too large to be represented (overflow), the function returns positive infinity (`HUGE_VAL`) and sets `errno` to `ERANGE`. * If the result is too small (underflow), the function returns `0.0`. --- ## Basic Example The following example demonstrates how to use the standard `exp()` function to calculate $e^0$, $e^1$, and $e^2$. ```c #include #include int main() { double x = 0; printf("e raised to the power of %lf is %lf\n", x, exp(x)); printf("e raised to the power of %lf is %lf\n", x + 1, exp(x + 1)); printf("e raised to the power of %lf is %lf\n", x + 2, exp(x + 2)); return 0; } ``` ### Output ```text e raised to the power of 0.000000 is 1.000000 e raised to the power of 1.000000 is 2.718282 e raised to the power of 2.000000 is 7.389056 ``` --- ## Advanced Examples ### 1. Calculating Exponential Values for an Array This example demonstrates how to iterate through an array of floating-point values and compute the exponential value for each. ```c #include #include int main() { double values[] = {0, 0.5, 1, 1.5, 2}; int num_values = sizeof(values) / sizeof(values); for (int i = 0; i < num_values; i++) { double x = values; double result = exp(x); printf("exp(%f) = %f\n", x, result); } return 0; } ``` #### Output ```text exp(0.000000) = 1.000000 exp(0.500000) = 1.648721 exp(1.000000) = 2.718282 exp(1.500000) = 4.481689 exp(2.000000) = 7.389056 ``` #### Code Explanation * An array named `values` is defined with five floating-point numbers. * The size of the array is dynamically calculated using `sizeof(values) / sizeof(values)`. * A `for` loop iterates through each element, computes $e^x$ using `exp()`, and prints the formatted output. --- ## Error Handling (Overflow Detection) Because exponential functions grow extremely fast, passing a large positive number to `exp()` can easily cause a **range error (overflow)**. When an overflow occurs: 1. The function returns `HUGE_VAL` (positive infinity). 2. The global variable `errno` is set to `ERANGE` (defined in ``). Here is how you can safely handle overflow errors in your code: ```c #include #include #include int main() { double x = 1000.0; errno = 0; // Reset errno before the function call double result = exp(x); if (errno == ERANGE) { printf("Overflow error: exp(%f) result is out of range.\n", x); } else { printf("exp(%f) = %f\n", x, result); } return 0; } ``` ### Output ```text Overflow error: exp(1000.000000) result is out of range. ``` #### Code Explanation * The input `1000.0` is extremely large for an exponential calculation ($e^{1000}$ exceeds the maximum limit of a double-precision float). * `errno` is cleared to `0` before calling `exp(x)`. * After the call, we check if `errno` equals `ERANGE`. If true, we handle the overflow gracefully instead of letting the program proceed with an infinite value (`inf`). --- ## Summary * **Purpose**: The `exp(x)` function computes the natural exponential function $e^x$. * **Variants**: Use `expf()` for `float` and `expl()` for `long double` to optimize performance and precision. * **Safety**: Always be cautious of overflow errors when working with large input values. Use `` and check for `ERANGE` to write robust, production-grade C code.
← C Function FrexpC Function Tanh β†’