Last active
August 29, 2015 14:08
-
-
Save gabrieljmj/e60f9693b1af2971dbcb to your computer and use it in GitHub Desktop.
Solving Quadratic Formula with Bhaskara
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
print_r(quadEq(1, 2, 1));// a=1 b=2 c=3 (x² + 2x + 3 = 0) | |
//Result: array(-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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