In C#, Math.Exp(d) returns e raised to the power d. Here e is a special number, about 2.718281828459045, stored as the constant Math.E. So Math.Exp(0) is 1 and Math.Exp(1) is about 2.7183.
Think of growth that keeps feeding itself, like bacteria or continuously compounded interest. The base e is the natural choice for that kind of growth. Exp is the opposite of the natural logarithm Log(): Exp(Log(5)) gives back 5. It is the same as Math.Pow(Math.E, d), but more direct.
This is a static member of the static class System.Math. You call it with the class name, such as Math.Exp(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Exp is in every .NET version. It takes and returns double.
Syntax
Pass the power. The answer is a double that is always positive.
C#
double a = Math.Exp(0); // 1
double b = Math.Exp(1); // 2.718281828459045 (same as Math.E)
double c = Math.Exp(-1); // about 0.3679
Call
What it does
Math.Exp(0)
Returns 1, because any number to the power 0 is 1.
Math.Exp(1)
Returns about 2.718281828459045, which is Math.E.
Math.Exp(-1000)
Returns 0. The answer is too small to store, so it rounds to zero.
Math.Exp(710)
Returns Infinity. The answer is too big for a double.
The result is never negative. Exp grows very fast: above about 709.78 a double overflows and you get Infinity, without any exception. Below about -745 the answer underflows to 0. Round non-whole results before printing them.
Library declarations
C#
public static double Exp(double d);
Parameters
Parameter
Description
d
Required. The power to raise e to. It may be negative, zero, or positive.
Return Value
Return Value
Description
double
e to the power d. Infinity for a very large d, 0 for a very negative d (or negative infinity), NaN for NaN.
C# Examples
Example 1 — Exp of 0, 1, 2, and -1
Call Exp on a few powers and round the answers to 4 decimal places.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
Console.WriteLine(Math.Exp(0));
Console.WriteLine(Math.Round(Math.Exp(1), 4).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Round(Math.Exp(2), 4).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Round(Math.Exp(-1), 4).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Round(Math.E, 4).ToString(CultureInfo.InvariantCulture));
}
}
Output
1
2.7183
7.3891
0.3679
2.7183
How It Works
Power 0.Math.Exp(0) is exactly 1. This is the one clean value you can rely on.
Power 1 is e.Math.Exp(1) is e itself, 2.718281828..., so rounding gives 2.7183. The last line shows the constant Math.E gives the same text.
Growth.Math.Exp(2) is e x e, about 7.3891. Each step up in the power multiplies the answer by e.
Negative power.Math.Exp(-1) is 1 divided by e, about 0.3679. The answer is small but never negative.
Example 2 — Undo a Log, and Grow Continuously
Show that Exp reverses Log, then use it for continuously compounded growth.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
Console.WriteLine(Math.Round(Math.Exp(Math.Log(5)), 6));
Console.WriteLine(Math.Round(Math.Exp(Math.Log(20)), 6));
double principal = 1000;
double rate = 0.05;
double years = 2;
double grown = principal * Math.Exp(rate * years);
Console.WriteLine(Math.Round(grown, 2).ToString(CultureInfo.InvariantCulture));
}
}
Output
5
20
1105.17
How It Works
Log then Exp.Math.Log(5) asks "e to which power gives 5?". Math.Exp of that power gives 5 back. Rounding removes tiny noise.
Any positive number. The same works for 20. The pair of methods undo each other for every positive number.
Continuous growth. The formula is amount = principal x erate x years. Here the exponent is 0.05 x 2 = 0.1.
The result.Exp(0.1) is about 1.10517, so 1000 grows to 1105.17. Rounding to 2 decimals gives a money-style answer.
Example 3 — Too Big, Too Small, and NaN
See the largest and smallest powers a double can handle, and how infinity and NaN pass through.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Exp(-1000));
Console.WriteLine(Math.Exp(710));
Console.WriteLine(Math.Exp(double.NegativeInfinity));
Console.WriteLine(Math.Exp(double.NaN));
Console.WriteLine(Math.Exp(709) > 1E300);
}
}
Output
0
Infinity
0
NaN
True
How It Works
Underflow. e to the power -1000 is far smaller than the smallest double, so the answer becomes exactly 0. No exception is thrown.
Overflow. e to the power 710 is larger than double.MaxValue, so you get Infinity.
Special inputs. Negative infinity as the power gives 0, and NaN in gives NaN out.
Just under the limit. The power 709 still fits. The answer is about 8.2 followed by 307 zeros, which is bigger than 1E300, so the test is True.
Frequently Asked Questions
It returns e raised to a power, where e is about 2.718281828459045. Math.Exp(0) is 1 and Math.Exp(1) is Math.E.
Exp always uses the base e. Math.Pow lets you choose any base. Math.Exp(x) is the same as Math.Pow(Math.E, x) but more direct.
The natural logarithm, Math.Log. Math.Log(Math.Exp(x)) gives back x, and Math.Exp(Math.Log(x)) gives back x for positive x.
The answer is larger than the biggest double (about 1.8 x 10 to the power 308). A double overflow gives Infinity instead of throwing an exception.
No. Math is a static class, so you call Math.Exp(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.