Software & Finance





C# - Using Exponent Series and Eulers Number to find EXP Value





 

I have given here a C# program to find the value of EXP using Exponent Series and without using the library function.

 

I have used Eulers Number to find out the value of Exponent.

 

This program will print the values got from the series function MyExp1 and Eulers Number MyExp2 and library function sin.

exp(x) series = 1 + x + x^2 / 2! + x^3 / 3! + x^4 / 4! + x^5 / %! + .....

exp(x) series = power(2.71828, x)

 

Source Code


 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace SoftwareAndFinance

{

    class Math

    {

        const double PI = 3.14159265;

        const double EulersNumber = 2.71828;

 

        // exp(x) series = 1 + x + x^2 / 2! + x^3 / 3! + x^4 / 4!

        static public double MyExp1(double x)

        {

            double f = x;

            double result = 1 + x;

            double fact = 1;

            int i = 0;

            for (i = 2; i < 20; i++)

            {

                fact *= i;

                f *= x;

                result += f / fact;

            }

            return result;

        }

 

        // exp(x) series = power(2.71828, x)

        public static double MyExp2(double x)

        {

            return System.Math.Pow(EulersNumber, x);

        }

 

        static void Main(string[] args)

        {

 

            for (double i = -2; i <= 3; i += 0.2)

            {

                Console.WriteLine("{0,8:f2} = {1,8:f4}  {2,8:f4}  {3,8:f4}", i, System.Math.Exp(i), MyExp1(i), MyExp2(i));

            }

 

        }

    }

}

Click here to get the C# project along with executable

 

Output


 

       -2.00 =   0.1353    0.1353    0.1353

       -1.80 =   0.1653    0.1653    0.1653

   -1.60 =   0.2019    0.2019    0.2019

   -1.40 =   0.2466    0.2466    0.2466

   -1.20 =   0.3012    0.3012    0.3012

   -1.00 =   0.3679    0.3679    0.3679

   -0.80 =   0.4493    0.4493    0.4493

   -0.60 =   0.5488    0.5488    0.5488

   -0.40 =   0.6703    0.6703    0.6703

   -0.20 =   0.8187    0.8187    0.8187

    0.00 =   1.0000    1.0000    1.0000

    0.20 =   1.2214    1.2214    1.2214

    0.40 =   1.4918    1.4918    1.4918

    0.60 =   1.8221    1.8221    1.8221

    0.80 =   2.2255    2.2255    2.2255

    1.00 =   2.7183    2.7183    2.7183

    1.20 =   3.3201    3.3201    3.3201

    1.40 =   4.0552    4.0552    4.0552

    1.60 =   4.9530    4.9530    4.9530

    1.80 =   6.0496    6.0496    6.0496

    2.00 =   7.3891    7.3891    7.3890

    2.20 =   9.0250    9.0250    9.0250

    2.40 =  11.0232   11.0232   11.0232

    2.60 =  13.4637   13.4637   13.4637

    2.80 =  16.4446   16.4446   16.4446

Press any key to continue . . .