My ASP.NET MVC web application is supposed to calculate the roots of a second-degree polynomial (quadratic) but I'm erroneously getting Nan
responses. I believe this is due to my improper setup, so let me post some of my code here:
View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RootFinder.Models.QuadCalc>" %>
<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("quadAValue", Model.quadraticAValue) %>
<br />
b: <%= Html.TextBox("quadBValue", Model.quadraticBValue) %>
<br />
c: <%= Html.TextBox("quadCValue", Model.quadraticCValue) %>
<br />
<input type="submit" id="quadraticSubmitButton" value="Calculate!" />
<br />
<br />
<strong>Root 1:</strong>
<p><%= Model.x1 %></p>
<br />
<strong>Root 2:</strong>
<p><%= Model.x2 %></p>
</div>
<% } %>
</asp:Content>
Controller (handles all controlling of requests for calculations like the quadratic):
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();
}
[HttpGet]
public ViewResult Quadratic()
{
return View(new QuadCalc());
}
[HttpPost]
public ViewResult Quadratic(QuadCalc newQuadCalc)
{
newQuadCalc.QuadCalculate();
return View(newQuadCalc);
}
public ActionResult Cubic()
{
return View();
}
public ActionResult Quartic()
{
return View();
}
}
}
Model (performing calculation):
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()
{
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Calculate discriminant
double insideSquareRoot = (quadraticBValue * quadraticBValue) - 4 * quadraticAValue * quadraticCValue;
if (insideSquareRoot < 0)
{
//No real solution
x1 = double.NaN;
x2 = double.NaN;
}
else if (insideSquareRoot == 0)
{
//One Solution
double sqrtOneSolution = Math.Sqrt(insideSquareRoot);
x1 = (-quadraticBValue + sqrtOneSolution) / (2 * quadraticAValue);
x2 = double.NaN;
}
else if (insideSquareRoot > 0)
{
//Two Solutions
double sqrtTwoSolutions = Math.Sqrt(insideSquareRoot);
x1 = (-quadraticBValue + sqrtTwoSolutions) / (2 * quadraticAValue);
x2 = (-quadraticBValue - sqrtTwoSolutions) / (2 * quadraticAValue);
}
}
}
}
So, let's say in the View, the end-user enters a=1
, b=0
and c=0
, my application will output Nan
for both Root1 and Root2. I think this may be due to the fact that I have not properly connected my end-user input into the instance variables of my newQuadCalc object...