C library functions -exp()

C standard library - <math.h>

Description

C Library Functionsdouble exp(double x)returneofxthe value of the power.

exp()is the C standard library<math.h>a function used to calculate e raised to the power of x, where e is the base of the natural logarithm (approximately 2.71828). This is a commonly used mathematical function, widely applied in scientific computing, finance, engineering, and other fields.

Declaration

Below is the declaration of the exp() function.

#include <math.h>

double exp(double x);
float expf(float x);
long double expl(long double x);

Parameter

  • x: the input real number, representing the exponent to be calculated.

Return Value

This function returns e raised to the power of x.

Example

The example below demonstrates the usage of the exp() function.

Example

#include <stdio.h>
#include <math.h>

int main ()
{
   double x = 0;
 
   printf("e to the power of %lf is %lf\n", x, exp(x));
   printf("e to the power of %lf is %lf\n", x+1, exp(x+1));
   printf("e to the power of %lf is %lf\n", x+2, exp(x+2));
   
   return(0);
}

Let us compile and run the above program, which will produce the following result:

e 的 0.000000 次幂是 1.000000
e 的 1.000000 次幂是 2.718282
e 的 2.000000 次幂是 7.389056

Calculate the exponents of multiple values

The following example shows how to calculate the exponents of multiple values:

Example

#include <stdio.h>
#include <math.h>

int main() {
    double values[] = {0, 0.5, 1, 1.5, 2};
    int num_values = sizeof(values) / sizeof(values[0]);

    for (int i = 0; i < num_values; i++) {
        double x = values[i];
        double result = exp(x);
        printf("exp(%f) = %f\n", x, result);
    }

    return 0;
}

Let us compile and run the above program, which will produce the following result:

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

  • Define an array containing multiple valuesvalues。
  • UsageforLoop through each value, calculate the exponent value, and print the result.

Error Handling

exp()If the input value is too large, the function may cause overflow. In this case, the function returns positive infinity, anderrnoSet toERANGE. You can useerrnoto check whether an overflow error has occurred.

The following example demonstrates how to handle overflow errors:

Example

#include <stdio.h>
#include <math.h>
#include <errno.h>

int main() {
    double x = 1000.0;
    errno = 0;  // Reset errno
    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;
}

Let us compile and run the above program, which will produce the following result:

exp(1000.000000) = inf

Code explanation

  • The code attempts to calculateexp(1000.0), this is an input value that may cause overflow.
  • errnois reset to 0, and then callexp(x)。
  • CheckserrnoWhether it equalsERANGE, if so, print an overflow error message.
  • If there are no errors, print the calculation result.

Summary

exp()The function is used to calculate e raised to the power of x, and is an important tool for handling exponential operations. When using it, care should be taken to handle possible overflow errors. Through proper useexp()function, which can yield accurate results in mathematical calculations, physical simulations, and engineering applications.

C standard library - <math.h>

other extensions