C Library – exp() Function
In C programming language, the exp() function is a built-in function of <math.h> library which is used to calculate the value of e (Euler’s Number = 2.71828) raised to the power x where x is any real number.
double exp(double x);
- Arguments: This function takes only one argument of type double.
- Return Type: This function returns the value of type double.
e raised to the power 4.00 = 54.60
Time Complexity: O(logN)
Space Complexity: O(1)
Note: We have to include <math.h> library to use this function otherwise exp() will be treated as undefined function and the compiler will show error.
Function Response to Different Parameters
Case 1: Using some other type as an argument
For integer 65 = 16948892444103337803358666752.000 For character A = 16948892444103337803358666752.000 For long double 65.000000 = 16948892444103337803358666752.000 For boolean 1 = 2.718
As we can see, whether we enter an integer, character, or even boolean value gives the correct return. This is because the compiler implicitly converts any other type to double when performing calculations.
Note: Even though this method works for math.h functions, it is recommended that we pass only arguments of type double to these functions. This method doesnt work for string literal as they cannot be converted to any numeric value by the compiler.
Case 2: When the return value is very small
Very Small Value: 0.000000
The return value is rounded off to zero when the return value is very small.
Case 3: Value returned larger than double type
Very Large value: inf
For very large values inf (a very large number that cannot be represented by float or double) or error is shown depending on the compiler. We can use the expl() function which returns a long double type.
expl() function in C
The expl() function is also a pre-defined function of <math.h> library of C programming language. This function is used instead of the exp function if we have to return a value larger than double type.
Very Large Value: 26881171418161354483964208709276842846060544.000000
Using pow() function to mimic exp() function
e raised to the power 5.00 = 148.41