OK, I have a problem where my calculation never takes place, like it is supposed to. i'm trying to make a computation which solves for the roots of a quadratic equation using the quadratic formula, based on the values of a
, b
and c
entered in a primitive form.
Now, let me supply the code for my Model, view and Controller for this application:
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RootFinder.Models
{
public class QuadCalc
{
public double quadraticAValue { get; set; }
public double quadraticBValue { get; set; }
public double quadraticCValue { get; set; }
public double x1 { get; set; }
public double x2 { get; set; }
public void QuadCalculate(out double x1, out double x2)
{
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Calculate the inside of the square root
double insideSquareRoot = (quadraticBValue * quadraticBValue) - 4 * quadraticAValue * quadraticCValue;
if (insideSquareRoot < 0)
{
//There is no solution
x1 = double.NaN;
x2 = double.NaN;
}
else
{
//Compute the value of each x
//if there is only one solution, both x's will be the same
double sqrt = Math.Sqrt(insideSquareRoot);
x1 = (-quadraticBValue + sqrt) / (2 * quadraticAValue);
x2 = (-quadraticBValue - sqrt) / (2 * quadraticAValue);
}
}
}
}
View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Polynomial Root Finder - Quadratic
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Quadratic</h2>
<% using(Html.BeginForm("Quadratic", "Calculate")) %>
<% { %>
<div>
a: <%= Html.TextBox("quadraticAValue") %>
<br />
b: <%= Html.TextBox("quadraticBValue") %>
<br />
c: <%= Html.TextBox("quadraticCValue") %>
<br />
<input type="submit" id="quadraticSubmitButton" value="Calculate!" />
<br />
<p><%= ViewData["Root1"] %></p>
<p><%= ViewData["Root2"] %></p>
</div>
<% } %>
</asp:Content>
Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RootFinder.Models;
namespace RootFinder.Controllers
{
public class CalculateController : Controller
{
//
// GET: /Calculate/
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Quadratic()
{
ViewData["Root1"] = "";
ViewData["Root2"] = "";
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Quadratic(QuadCalc newQuadCalc)
{
ViewData["Root1"] = newQuadCalc.x1;
ViewData["Root2"] = newQuadCalc.x2;
return View();
}
public ActionResult Quartic()
{
return View();
}
public object x1 { get; set; }
public object x2 { get; set; }
}
}
Now, I'm trying to still work this out, but I believe the problem may lie in the assigning of the inputted values from the form, into a new object for computation. This could also be a problem considering that MVC will probably take in the form input a string stype, instead of a double, where in my model, I perform computations based off of the input being a double type.
I do not know where to make the string to double conversion if this even is necessary however.
Does anyone see why there is a problem.
The page outputs the following:
x1 = 0
x2 = 0