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.
Syntax:
double exp(double x);
- Arguments: This function takes only one argument of type double.
- Return Type: This function returns the value of type double.
Example:
C
#include <math.h>
#include <stdio.h>
int main()
{
double num = 4.0;
double res;
res = exp (num);
printf ( "e raised to the power %.2lf = %.2lf" , num, res);
return 0;
}
|
Output
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
C
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
int main()
{
int integer = 65;
char character = 'A' ;
bool boolean = true ;
long double longDouble = 65.0;
printf ( "For integer %d = %-35.3lf\n" , integer,
exp (integer));
printf ( "For character %c = %-35.3lf\n" , character,
exp (character));
printf ( "For long double %llf = %-15.3lf\n" , longDouble,
exp (longDouble));
printf ( "For boolean %d = %-15.3lf\n" , boolean,
exp (boolean));
return 0;
}
|
Output
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
C
#include <math.h>
#include <stdio.h>
int main()
{
printf ( "Very Small Value: %lf\n" , exp (-700));
return 0;
}
|
Output
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
C
#include <math.h>
#include <stdio.h>
int main()
{
printf ( "Very Large Value: %lf" , exp (1000));
return 0;
}
|
Output
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.
Example:
C
#include <math.h>
#include <stdio.h>
int main()
{
printf ( "Very Large Value: %Lf" , expl(100));
return 0;
}
|
Output
Very Large Value: 26881171418161354483964208709276842846060544.000000
Using pow() function to mimic exp() function
We can use the pow() function of <math.h> library to replicate the task of exp() in C.
Example:
C
#include <math.h>
#include <stdio.h>
#define e 2.71828
int main()
{
double x = 5.0;
printf ( "e raised to the power %.2lf = %.2lf" , x,
pow (e, x));
return 0;
}
|
Output
e raised to the power 5.00 = 148.41
Last Updated :
21 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...