I've written a piece of code to carry out the quadratic equation:
function quadratic($a,$b,$c) {
$mb = $b - ($b*2);
$bs = $b * $b;
$fac = ($a * $c) * 4;
$ans1 = ($mb + sqrt(($bs - $fac))) / (2 * $a);
$ans2 = ($mb - sqrt(($bs - $fac))) / (2 * $a);
echo ("Your <b>+</b> value is: " . $ans1 . "<br />");
echo ("Your <b>-</b> value is: " . $ans2);
}
The problem is that, if for example a=2, b=4, c=8, both answers are outputted as NAN. Any ideas as to how to fix this so that I get an actual number output?
$a * $c * 4 = 64
$bs = 4 * 4 = 16
sqrt(($bs - $fac))) = sqrt(-48)
You cant take the sqrt of a negative number, it is not defined, hence the result is NaN.
Futhermore your formula can be simplified as:
$mb = $b - ($b*2) = -$b
So instad of $mb
you can simply use -$b
.
Besides that, your formula is correct for the quadratic equation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With