itsamemario
New member
- Joined
- Jun 17, 2017
- Messages
- 3
- Programming Experience
- Beginner
Hi everyone!
First of all, I need the mathematicians out there!
I have been working on a calculator / math quiz program in C#, and currently I am trying to allow the user to have quadratic equations (ax²+bx+c = 0) solved automatically with
the quadratic formula by inserting the values for a, b, and c.
It is already working, except for that one part that isn't.
Have an example:
Console.WriteLine("xPlus equals" + (-quadEq_b + (double)Math.Sqrt(discriminant) / 2*quadEq_a + "."));
I have been comparing the results doing the same math in my "real" calculator and the results don't match. (but the decimals do, for some reason) It seems, however, I have
found what is causing the results to be wrong, but don't know how to fix it. I tried to remove "/ 2*quadEq_a" and then compared the results again doing the same thing on my
real life calculator, and voilà, the results did match.
I suppose I am ignoring some rule to be followed if you want to divide a number by a multiplication (like in this case "2*quadEq_a")
Putting it in brackets, like "/ (2*quadEq_a")" didn't change anything, either.
I'd appreciate it if someone can help me with this Below you find all the code relevant for this problem.
Mario
First of all, I need the mathematicians out there!
I have been working on a calculator / math quiz program in C#, and currently I am trying to allow the user to have quadratic equations (ax²+bx+c = 0) solved automatically with
the quadratic formula by inserting the values for a, b, and c.
It is already working, except for that one part that isn't.
Have an example:
Console.WriteLine("xPlus equals" + (-quadEq_b + (double)Math.Sqrt(discriminant) / 2*quadEq_a + "."));
I have been comparing the results doing the same math in my "real" calculator and the results don't match. (but the decimals do, for some reason) It seems, however, I have
found what is causing the results to be wrong, but don't know how to fix it. I tried to remove "/ 2*quadEq_a" and then compared the results again doing the same thing on my
real life calculator, and voilà, the results did match.
I suppose I am ignoring some rule to be followed if you want to divide a number by a multiplication (like in this case "2*quadEq_a")
Putting it in brackets, like "/ (2*quadEq_a")" didn't change anything, either.
I'd appreciate it if someone can help me with this Below you find all the code relevant for this problem.
Mario
// Quadratic Formula values double quadEq_b; double quadEq_c; double quadEq_a; double discriminant; QuadraticEquationSolver: Console.WriteLine("A quadratic equation in its normal form looks like this: \n ax^2+bx+c = 0 \n In order to solve for x, you need to declare the values for a, b, and c \n These values are then plugged into the quadratic formula. \n Please insert a value for a:"); quadEq_a = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Please insert a value for b:"); quadEq_b = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Please insert a value for c:"); quadEq_c = Convert.ToDouble(Console.ReadLine()); discriminant = (double)Math.Pow(quadEq_b, 2) - 4 * quadEq_a * quadEq_c; if (discriminant < 0) { Console.WriteLine("The discriminant is negative, therefore no solution for x exists. \n Press any key to return to the Main Menu."); Console.ReadKey(); goto Start; } if (discriminant == 0) { Console.WriteLine("The discriminant equals zero, therefore one solution for x exists."); Console.ReadKey(); Console.WriteLine(discriminant); Console.ReadKey(); goto QuadFormOneSolution; } if (discriminant > 0) { Console.WriteLine("The discriminant is positive, therefore two solutions for x exist."); Console.ReadKey(); Console.WriteLine(discriminant); Console.ReadKey(); goto QuadFormTwoSolutions; } QuadFormOneSolution: Console.WriteLine("x equals" + -quadEq_b + (double)Math.Sqrt(discriminant) / 2*quadEq_a + "."); Console.ReadKey(); QuadFormTwoSolutions: Console.WriteLine("xPlus equals" + (-quadEq_b + (double)Math.Sqrt(discriminant) / 2*quadEq_a + ".")); Console.WriteLine("xMinus equals" + (-quadEq_b - (double)Math.Sqrt(discriminant) / 2*quadEq_a + ".")); Console.ReadKey();
Last edited by a moderator: