Skip to content

Instantly share code, notes, and snippets.

@gabrieljmj
Last active August 29, 2015 14:08
Show Gist options
  • Save gabrieljmj/e60f9693b1af2971dbcb to your computer and use it in GitHub Desktop.
Save gabrieljmj/e60f9693b1af2971dbcb to your computer and use it in GitHub Desktop.
Solving Quadratic Formula with Bhaskara
<?php
print_r(quadEq(1, 2, 1));// a=1 b=2 c=3 (x² + 2x + 3 = 0)
//Result: array(-1)
<?php
// Using Bhaskara
function quadEq($a, $b, $c) {
$delta = pow($b, 2) - (4 * $a * $c);
if ($delta >= 0) {
if ($delta == 0) {
$x = -$b / 2 * $a;
return array($x);
}
$x1 = (-$b + sqrt($delta)) / 2 * $a;
$x2 = (-$b - sqrt($delta)) / 2 * $a;
return array($x1, $x2);
}
return array('NAN', 'NAN');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment