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.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main(string[] args)
6
{
7
Console.WriteLine( Math.E ); // 2.718281828459045
8
9
Console.WriteLine( Math.Exp(1) ); // 2.718281828459045
10
Console.WriteLine( Math.Exp(2) ); // 7.38905609893065
11
Console.WriteLine( Math.Exp(3) ); // 20.085536923187668
12
}
13
}
Syntax |
xxxxxxxxxx 1 namespace System 2 { 3 public static class Math 4 { 5 // ... 6 public static const double E = 2.7182818284590451; 7 // ... 8 } 9 } |
Result | e number (2.718281828459045... ). |
Description |
|
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.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double ComputeE(int iterations)
6
{
7
double e = 0;
8
9
for (int i = 0; i < iterations; i++)
10
{
11
double divider = 1;
12
13
for (int j = 0; j < i; j++)
14
divider *= (j + 1);
15
16
e += (1.0 / divider);
17
}
18
19
return e;
20
}
21
22
public static void Main(string[] args)
23
{
24
Console.WriteLine( ComputeE(1) ); // 1
25
Console.WriteLine( ComputeE(2) ); // 2
26
Console.WriteLine( ComputeE(5) ); // 2.708333333333333
27
Console.WriteLine( ComputeE(10) ); // 2.7182815255731922
28
Console.WriteLine( ComputeE(20) ); // 2.7182818284590455
29
Console.WriteLine( ComputeE(50) ); // 2.7182818284590455
30
}
31
}