Log in

View Full Version : X = Ax^2 + Bx + C



boxxertrumps
11-30-2006, 10:03 PM
I am creating a Quadratic equation script, and i have this so far..
<?php
$a = $_GET["a"];
$b = $_GET["b"];
$c = $_GET["c"];
$negb = -$b;
$2a = 2 * $a;
$square = sqrt(pow($b,2) -4 * $a * $c);
if is_nan($square) == 1 {echo(No Roots!)};
else {
$add = $negb + $square;
$sub = $negb - $square;
$x1 = $add / $2a;
$x2 = $sub / $2a;
echo($x1 ."<br />". $x2)};
?>
i hope noone got hurt... please excuse my overuse of variables...

and im also trying to figure out how to create a guest page on my site... IE bulliten board where people can write what they want, delete what they want, etc. but with less restrictions.
all i need to do is figure out how to change <> into ampersand characters and back when posting, then dump the written junk into a php file.

shachi
12-01-2006, 12:15 PM
figure out how to change <> into ampersand characters and back when posting, then dump the written junk into a php file.

What do you mean?

Twey
12-01-2006, 02:22 PM
<?php
function quadraticRoots($a, $b, $c) {
$d = $b * -1;
$e = sqrt(($b * $b) - (4 * $a * $c)) / (2 * $a);
return array($d + $e, $d - $e);
}
?> ... if I remember correctly.

boxxertrumps
12-01-2006, 03:54 PM
X= (-B +and- squareroot(b^2-4ac))/2a
the script is okay, right?

im not sure yours is correct unless you divide the entire thing by 2a...
didn't think to create a function....

chb03c
12-01-2006, 08:19 PM
X= (-B +and- squareroot(b^2-4ac))/2a
the script is okay, right?

im not sure yours is correct unless you divide the entire thing by 2a...
didn't think to create a function....


There is a major problem everyone is missing with this function and that is there are speacial cases using the QF. (ie b^2-4*a*c == 0) there are others too. But you will experience errors if you do not address theses speacial cases.

Twey
12-01-2006, 10:40 PM
Oh, you are right. I apologise.
<?php
function quadraticRoots($a, $b, $c) {
$d = $b * -1;
$e = sqrt(($b * $b) - (4 * $a * $c));
return $e === 0 and array(NAN, NAN) or array(($d + $e) / (2 * $a), ($d - $e) / (2 * $a));
}
?>

boxxertrumps
12-03-2006, 02:41 AM
modified the script to accept input then echo results

<?php
$a = $_GET["a"];
$b = $_GET["b"];
$c = $_GET["c"];
function quadraticRoots($a, $b, $c) {
$d = $b * -1;
$e = sqrt(($b * $b) - (4 * $a * $c));
return $e === 0 and array(NAN, NAN) or array(($d + $e) / (2 * $a), ($d - $e) / (2 * $a));
};
echo $e[1] ."<br/>". $e [2];
?>