Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Quadratic formula
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Numerical calculation == The quadratic formula is exactly correct when performed using the idealized arithmetic of [[real number]]s, but when approximate arithmetic is used instead, for example pen-and-paper arithmetic carried out to a fixed number of decimal places or the [[Floating-point arithmetic|floating-point]] binary arithmetic available on computers, the limitations of the number representation can lead to substantially inaccurate results unless great care is taken in the implementation. Specific difficulties include [[catastrophic cancellation]] in computing the sum {{tmath|\textstyle -b \pm \sqrt\Delta}} if {{tmath|\textstyle b \approx \pm \sqrt\Delta}}; catastrophic calculation in computing the discriminant {{tmath|1=\textstyle \Delta = b^2 - 4ac}} itself in cases where {{tmath|\textstyle b^2 \approx 4ac}}; degeneration of the formula when {{tmath|a}}, {{tmath|b}}, or {{tmath|c}} is represented as zero or infinite; and possible [[Floating-point arithmetic#Exception handling|overflow or underflow]] when multiplying or dividing extremely large or small numbers, even in cases where the roots can be accurately represented.<ref>{{citation |last=Forsythe |first=George E. |author-link=George E. Forsythe |year=1969 |chapter=Solving a Quadratic Equation on a Computer |title=The Mathematical Sciences: A Collection of Essays |publisher=MIT Press |pages=138–152 |isbn=0-262-03026-8 |chapter-url=https://archive.org/details/1969.-bers-the-mathematical-sciences.-a-collection-of-essays/page/138/ }}</ref><ref name=goualard>{{cite tech report |mode=cs2 |last=Goualard |first=Frédéric |year=2023 |title=The Ins and Outs of Solving Quadratic Equations with Floating-Point Arithmetic |publisher=University of Nantes |id={{HAL|hal-04116310}} }}</ref> Catastrophic cancellation occurs when two numbers which are approximately equal are subtracted. While each of the numbers may independently be representable to a certain number of digits of precision, the identical leading digits of each number cancel, resulting in a difference of lower relative precision. When {{tmath|\textstyle b \approx \sqrt \Delta}}, evaluation of {{tmath|\textstyle -b + \sqrt \Delta}} causes catastrophic cancellation, as does the evaluation of {{tmath|\textstyle -b - \sqrt \Delta}} when {{tmath|\textstyle b \approx -\sqrt\Delta}}. When using the standard quadratic formula, calculating one of the two roots always involves addition, which preserves the working precision of the intermediate calculations, while calculating the other root involves subtraction, which compromises it. Therefore, naïvely following the standard quadratic formula often yields one result with less relative precision than expected. Unfortunately, introductory algebra textbooks typically do not address this problem, even though it causes students to obtain inaccurate results in other school subjects such as introductory chemistry.<ref>{{citation |last=Thompson, H. Bradford |year=1987 |title=Good numerical technique in chemistry: The quadratic equation |journal=Journal of Chemical Education |volume=64 |number=12 |page=1009 |doi=10.1021/ed064p1009 |bibcode=1987JChEd..64.1009T }}</ref> For example, if trying to solve the equation {{tmath|1=\textstyle x^2 - 1634x + 2 = 0}} using a pocket calculator, the result of the quadratic formula {{tmath|1=\textstyle x = 817 \pm \sqrt{667\,487} }} might be approximately calculated as:<ref> This example comes from: {{pb}} {{citation |title=Essentials of Numerical Analysis with Pocket Calculator Demonstrations |last=Henrici |first=Peter |author-link=Peter Henrici |year=1982 |place=New York |publisher=Wiley |page=13}}</ref> <math display=block>\begin{alignat}{3} x_1 &= 817 + 816.998\,776\,0 &&= 1.633\,998\,776 \times 10^3, \\ x_2 &= 817 - 816.998\,776\,0 &&= 1.224 \times 10^{-3}. \end{alignat}</math> Even though the calculator used ten decimal digits of precision for each step, calculating the difference between two approximately equal numbers has yielded a result for {{tmath|x_2}} with only four correct digits. One way to recover an accurate result is to use the identity {{tmath|1= x_1 x_2 = c/a}}. In this example {{tmath|x_2}} can be calculated as {{tmath|1= x_2 = 2 / x_1 = {} }}{{wbr}}{{tmath|1.223\,991\,125 \times 10^{-3}\!}}, which is correct to the full ten digits. Another more or less equivalent approach is to use the version of the quadratic formula with the square root in the denominator to calculate one of the roots (see {{slink|#Square root in the denominator}} above). Practical computer implementations of the solution of quadratic equations commonly choose which formula to use for each root depending on the sign of {{tmath|b}}.<ref>{{citation |last=Forsythe |first=George E. |title=How Do You Solve a Quadratic Equation |year=1966 |publisher=Stanford University |id=STAN-CS-66-40 (AD639052) |type=Tech report |url=https://apps.dtic.mil/sti/tr/pdf/AD0639052.pdf }}</ref> These methods do not prevent possible overflow or underflow of the floating-point exponent in computing {{tmath|\textstyle b^2}} or {{tmath|4ac}}, which can lead to numerically representable roots not being computed accurately. A more robust but computationally expensive strategy is to start with the substitution {{tmath|1=\textstyle x = -u \sgn(b)\sqrt{\vert c\vert} \big/\! \sqrt{\vert a\vert} }}, turning the quadratic equation into <math display=block> u^2 - 2 \frac{|b|}{2\sqrt{|a|}\sqrt{|c|}}u + \sgn(c) = 0, </math> where {{tmath|\sgn}} is the [[sign function]]. Letting {{tmath|1=\textstyle d = \vert b\vert\big/2\sqrt{\vert a\vert}\sqrt{\vert c\vert} }}, this equation has the form {{tmath|1=\textstyle u^2 - 2du \pm 1 = 0}}, for which one solution is {{tmath|1=\textstyle u_1 = d + \sqrt{d^2 \mp 1} }} and the other solution is {{tmath|1=\textstyle u_2 = \pm1 / u_1}}. The roots of the original equation are then {{tmath|1=\textstyle x_1 = -\sgn(b)\bigl(\sqrt{\vert c\vert} \big/\!\sqrt{\vert a\vert}~\!\bigr) u_1}} and {{tmath|1=\textstyle x_2 = -\sgn(b)\bigl(\sqrt{\vert c\vert} \big/\!\sqrt{\vert a\vert}~\!\bigr) u_2}}.<ref>{{citation |last=Baker |first=Henry G. |year=1998 |title=You Could Learn a Lot from a Quadratic: Overloading Considered Harmful |journal=SIGPLAN Notices |volume=33 |issue=1 |pages=30–38 |doi=10.1145/609742.609746 |doi-access=free }}</ref><ref>{{citation |last1=Mastronardi |first1=Nicola |last2=Van Dooren |first2=Paul |year=2015 |title=Revisiting the stability of computing the roots of a quadratic polynomial |journal=Electronic Transactions on Numerical Analysis |volume=44 |pages=73–83 |arxiv=1409.8072 |url=https://etna.math.kent.edu/volumes/2011-2020/vol44/abstract.php?vol=44&pages=73-82 }}</ref> With additional complication the expense and extra rounding of the square roots can be avoided by approximating them as powers of two, while still avoiding exponent overflow for representable roots.{{r|goualard}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)