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 can a C program compute the roots of a quadratic equation?ComputerScienceOne · Computing Quadratic Roots
- How can PHP pass variables by reference to return multiple quadratic roots?ComputerScienceOne · Quadratic Roots
- What section is listed as 2.7?ComputerScienceOne · Quadratic Roots
- How can a general-purpose PHP function round values to a specified decimal place?ComputerScienceOne · Quadratic Roots
- How does the Java quadratic roots program check errors and calculate its roots?ComputerScienceOne · Quadratic Roots Revisited