Open In App

C Library – exp() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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




// C program to demonstrate the
// use of exp() function
#include <math.h>
#include <stdio.h>
 
int main()
{
    // variable a which can
    // be any real number
    double num = 4.0;
 
    // variable to store result
    double res;
 
    // usage of exp() function
    res = exp(num);
 
    // printing result
    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




// C program to check the
// return value of exp()
// for different input
// types
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
 
int main()
{
    // integer type
    int integer = 65;
 
    // character type
 
    // ASCII value of '^E' is 5
    // for boolean type
    char character = 'A';
 
    bool boolean = true; // true = 1
 
    // double type
    long double longDouble = 65.0;
 
    // printing result for integer argument
    printf("For integer %d = %-35.3lf\n", integer,
           exp(integer));
 
    // printing result for character argument
    printf("For character %c = %-35.3lf\n", character,
           exp(character));
 
    // printing result for long double argument
    printf("For long double %llf = %-15.3lf\n", longDouble,
           exp(longDouble));
 
    // printing result for boolean argument
    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




// C program to show the
// behaviour of exp() for
// very small return value
#include <math.h>
#include <stdio.h>
 
int main()
{
    // returning very small return value
    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




// C program to show the behaviour of exp() for very large
// return value
#include <math.h>
#include <stdio.h>
 
int main()
{
    // returning very large value
    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




// C program to show the usage of expl() function
#include <math.h>
#include <stdio.h>
 
int main()
{
    // returning very large value
    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




// using pow() function to get exponential
#include <math.h>
#include <stdio.h>
 
// defining Euler's Number
#define e 2.71828
 
int main()
{
    // defining any real number
    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
Previous
Next
Share your thoughts in the comments
Similar Reads