What is the exp() function in Swift?

Overview

In Swift, the exp() function returns the mathematical e raised to the power of a specific number. Here, Euler’s number e is the base of the natural logarithm.

The mathematical representation of the exp() function is shown below:

Mathematical representation of the exp() function

Note: We need to import Foundation in our code to use the exp() function. We can import it like this: import Foundation.

Syntax

// number should be float, double or real.
exp(number)
Syntax of exp() function

Parameters

This function requires a number as a parameter.

Return value

This function returns the mathematical e raised to the power of number sent as a parameter.

Example

The code below shows the exp() function in Swift:

import Swift
import Foundation
// Positive Integer
print ("The value of exp(4.0) : ", exp(4.0));
//zero
print ("The value of exp(0.0) : ", exp(0.0));
// Negative Integer
print ("The value of exp(-25.0) : ", exp(-25.0));
//Positive double value
print ("The value of exp(4.4) : ", exp(4.4));
//Negative double value
print ("The value of exp(-4.5) : ", exp(-4.5));

Code explanation

  • Line 2: We add the Foundation header required for the exp() function.
  • Line 4: We calculate the exponent value of the positive integer using exp().
  • Line 7: We calculate the exponent value of the zero using exp().
  • Line 10: We calculate the exponent value of the negative integer using exp().
  • Line 13: We calculate the exponent value of the positive double value using exp().
  • Line 16: We calculate the exponent value of the negative double value using exp().

    Free Resources