EN
C# / .NET - Math.E property example
0
points
The Math.E
property returns e mathematical constant (2.718281828459045...
).
e
is called Euler's number or Napier's constant. However, it was discovered by Jacob Bernoulli. It is a mathematical constant used as the base of the natural logarithm.
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine( Math.E ); // 2.718281828459045
Console.WriteLine( Math.Exp(1) ); // 2.718281828459045
Console.WriteLine( Math.Exp(2) ); // 7.38905609893065
Console.WriteLine( Math.Exp(3) ); // 20.085536923187668
}
}
1. Documentation
Syntax |
|
Result | e number (2.718281828459045... ). |
Description |
|
2. e
number approximation example
To calculate e
The following function with infinity series can be used to get a better precision infinite number of iterations with big precision numbers.
using System;
public class Program
{
static double ComputeE(int iterations)
{
double e = 0;
for (int i = 0; i < iterations; i++)
{
double divider = 1;
for (int j = 0; j < i; j++)
divider *= (j + 1);
e += (1.0 / divider);
}
return e;
}
public static void Main(string[] args)
{
Console.WriteLine( ComputeE(1) ); // 1
Console.WriteLine( ComputeE(2) ); // 2
Console.WriteLine( ComputeE(5) ); // 2.708333333333333
Console.WriteLine( ComputeE(10) ); // 2.7182815255731922
Console.WriteLine( ComputeE(20) ); // 2.7182818284590455
Console.WriteLine( ComputeE(50) ); // 2.7182818284590455
}
}