Difference between revisions of "Quadratic Solver"

From CodeCodex

(New page: // Quadratic Solver for PHP by Gazza 2004-03-21 // Useage: for 3x^2 + 4x + 5 = 0, use quadratic(3, 4, 5, 'root1') and quadratic(3, 4, 5, 'root2'), // or simply just quadratic(3, 4, 5, 'bot...)
 
(Implementations)
 
(5 intermediate revisions by 5 users not shown)
Line 1: Line 1:
// Quadratic Solver for PHP by Gazza 2004-03-21
+
酵素ドリンク
// Useage: for 3x^2 + 4x + 5 = 0, use quadratic(3, 4, 5, 'root1') and quadratic(3, 4, 5, 'root2'),
+
 
// or simply just quadratic(3, 4, 5, 'both').
+
酵素は体内でも作られますが、一生で作られる量に限度があり、また、非常に壊れやすいため食べ物から摂るのが難しく、サプリメントからとるのがおススメです!
// The $root argument doesn't define which value it will be in relation to the other - I could
+
// easily have called them John and Jane, instead of 1st and 2nd.
+
//
+
function quadratic($a, $b, $c, $root)
+
{
+
$precision = 3; // Change this value for a different decimal places rounding.
+
+
$bsmfac = $b*$b-4*$a*$c;
+
if ($bsmfac < 0) { // Accounts for complex roots.
+
$plusminusone = " + "; $plusminustwo = " - ";
+
$bsmfac *=-1;
+
$complex=(sqrt($bsmfac)/(2*$a));
+
if ($a < 0){ //if negative imaginary term, tidies appearance.
+
$plusminustwo = " + ";
+
$plusminusone = " - ";
+
$complex *= -1;
+
} // End if ($a < 0)
+
$lambdaone = round(-$b/(2*$a), $precision).$plusminusone.round($complex, $precision).'i';
+
$lambdatwo = round(-$b/(2*$a), $precision).$plusminustwo.round($complex, $precision).'i';
+
} // End if ($bsmfac < 0)
+
+
else if ($bsmfac == 0) { // Simplifies if b^2 = 4ac (real roots).
+
$lambdaone = round(-$b/(2*$a), $precision);
+
$lambdatwo = round(-$b/(2*$a), $precision);
+
} // End else if (bsmfac == 0)
+
+
else { // Finds real roots when b^2 != 4ac.
+
$lambdaone = (-$b+sqrt($bsmfac))/(2*$a);
+
$lambdaone = round($lambdaone, $precision);
+
$lambdatwo = (-$b-sqrt($bsmfac))/(2*$a);
+
$lambdatwo = round($lambdatwo, $precision);
+
} // End else
+
+
// Return what is asked for.
+
if ($root == 'root1') {return $lambdaone;}
+
if ($root == 'root2') {return $lambdatwo;}
+
if ($root == 'both') {return $lambdaone. ' and ' .$lambdatwo;}
+
} // End function
+

Latest revision as of 06:09, 24 September 2012

酵素ドリンク

酵素は体内でも作られますが、一生で作られる量に限度があり、また、非常に壊れやすいため食べ物から摂るのが難しく、サプリメントからとるのがおススメです!