Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- // 6x^2 + 11x - 35 = 0 | SolveQuadratic(6, 11, -35);
- // quadratic equation is a second order of polynomial equation in a single variable
- // x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
- public static void SolveQuadratic(double a, double b, double c)
- {
- double sqrtpart = b * b - 4 * a * c;
- double x, x1, x2, img;
- if (sqrtpart > 0)
- {
- x1 = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
- x2 = (-b - System.Math.Sqrt(sqrtpart)) / (2 * a);
- Console.WriteLine("Two Real Solutions: {0,8:f4} or {1,8:f4}", x1, x2);
- }
- else if (sqrtpart < 0)
- {
- sqrtpart = -sqrtpart;
- x = -b / (2 * a);
- img = System.Math.Sqrt(sqrtpart) / (2 * a);
- Console.WriteLine("Two Imaginary Solutions: {0,8:f4} + {1,8:f4} i or {2,8:f4} + {3,8:f4} i", x, img, x, img);
- }
- else
- {
- x = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
- Console.WriteLine("One Real Solution: {0,8:f4}", x);
- }
- }
- static void Main()
- {
- 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));
- }
- Console.WriteLine();
- Console.WriteLine();
- // 6x^2 + 11x - 35 = 0
- SolveQuadratic(6, 11, -35);
- // 5x^2 + 6x + 1 = 0
- SolveQuadratic(5, 6, 1);
- // 2x^2 + 4x + 2 = 0
- SolveQuadratic(2, 4, 2);
- // 5x^2 + 2x + 1 = 0
- SolveQuadratic(5, 2, 1);
- Console.WriteLine();
- Console.WriteLine();
- Console.ReadLine();
- }
- }
- }
- // Note: found on the Internet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement