Concept

How does PHP compute the roots of a quadratic equation?

ComputerScienceOne / Computing Quadratic Roots

"We need to take care that we correctly adapt the formula so it accurately reflects the order of operations. We also need to use the math library’s square root function.\n\n$root1 = (-$b + sqrt($b*$b - 4*$a*$c) ) / (2*$a);\n\n$root2 = (-$b - sqrt($b*$b - 4*$a*$c) ) / (2*$a);\n\nFinally, we print the output using printf(). The full program can be found in Code Sample 37.5.\n\n<?php\n\n/**\n\n* This program computes the roots to a quadratic equation\n\n* using the quadratic formula.\n\n*/\n\nprintf(\"Please enter a: \");\n\n$a = floatval(trim(fgets(STDIN)));\n\nprintf(\"Please enter b: \");\n\n$b = floatval(trim(fgets(STDIN)));\n\nprintf(\"Please enter c: \");\n\n$c = floatval(trim(fgets(STDIN)));\n\n$root1 = (-$b + sqrt($b*$b - 4*$a*$c) ) / (2*$a);\n\n$root2 = (-$b - sqrt($b*$b - 4*$a*$c) ) / (2*$a);\n\nprintf(\"The roots of %fx^2 + %fx + %f are:\n\n\", $a, $b, $c);\n\nprintf(\" root1 = %f\n\n\", $root1);\n\nprintf(\" root2 = %f\n\n\", $root2);\n\n?>\n\nCode Sample 37.5.: Quadratic Roots Program in PHP"

Related Ideas

How does PHP compute the roots of a quadratic equation? | ComputerScienceOne | Bifalgorithm | Bifalgorithm