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:
Note: We need to import
Foundationin our code to use theexp()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 Swiftimport Foundation// Positive Integerprint ("The value of exp(4.0) : ", exp(4.0));//zeroprint ("The value of exp(0.0) : ", exp(0.0));// Negative Integerprint ("The value of exp(-25.0) : ", exp(-25.0));//Positive double valueprint ("The value of exp(4.4) : ", exp(4.4));//Negative double valueprint ("The value of exp(-4.5) : ", exp(-4.5));
Code explanation
- Line 2: We add the
Foundationheader required for theexp()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().