Quadratic Relation Solver

Iconoclast

New member
http://www.swfcabin.com/open/1243708772
This would be a third major revision I have done.
What does anyone think? Did I screw this up to Hell?

The new algorithm is:
Code:
if (a == 0)
{
}
else if (c == 0)
{
	if (b == 0)
	{
		x = 0;
	}
	else
	{
		x = 0 + ", " + -b / a;
	}
}
else if (b == 0)
{
	if (c / a < 0)
	{
		x = "+/-" + Math.sqrt(-c / a);
	}
	else if (c / a > 0)
	{
		x = "+/-" + Math.sqrt(+c / a) + "i";
	}
}
else
{
	if (b == 4 * a * c / b)
	{
		x = -b / (2 * a);
	}
	else if (b < 4 * a * c / b)
	{
		x = -b / (2 * a) + " +/- " + Math.sqrt(4 * a * c - b * b) / (2 * a) + "i";
	}
	else if (b > 4 * a * c / b)
	{
		x = (-Math.sqrt(b * b - 4 * a * c) - b) / (2 * a) + ", " + (Math.sqrt(b * b - 4 * a * c) - b) / (2 * a);
	}
}
 

Isildur

New member
Of course the quadratic formula is undefined for a=0, but perhaps you might nevertheless want to give the user the value of the single root?
That is, for a case where a=0 so it's really just a straight line, perhaps use "x = -c/b" to give the answer?
 

Iconoclast

New member
I would like to re-add this, but I had some other thoughts.
To avoid confusion I will re-add it, thanks, just once I look into other changes to publish along with it. (I can't really reupload a modified version without uploading another copy.)

They primarily stem from the fact that a polynomial is defined such that the leading coefficient cannot be zero.
In a perspective of a plug-in engine for something that must process a quadratic problem, it could be dangerous to not return the correct results due to a being zero--in which case the function is linear.

I have also spent some time trying to, before cheating, derive the Cubic Formula, (I also have some very wide-canvas images of the Quartic Formula saved, but there is a more efficient and practical algorithm that will give imaginary solutions when radicals have the negative parameter.) so maybe I would look into modifying this to offer support for solving linear, quadratic, cubic, and quartic functions? In this case if (a == 0) they can just choose the degree of their wishes.

As far as real number solutions go, the Rational Root Theorem (if I can figure out how to code it with my basic coding knowledge...some parts are finished) will solve any polynomial, but I want support for imaginary and all solutions as well first. Perhaps I should be worrying more about rational zero theorem.

As far as the efficiency of my code, though, I have been wanting to know, even as far as syntax, where I could improve.
 
Top Bottom