How to resolve the algorithm Roots of a quadratic function step by step in the C++ programming language
How to resolve the algorithm Roots of a quadratic function step by step in the C++ programming language
Table of Contents
Problem Statement
Write a program to find the roots of a quadratic equation, i.e., solve the equation
a
x
2
b x + c
0
{\displaystyle ax^{2}+bx+c=0}
. Your program must correctly handle non-real roots, but it need not check that
a ≠ 0
{\displaystyle a\neq 0}
. The problem of solving a quadratic equation is a good example of how dangerous it can be to ignore the peculiarities of floating-point arithmetic. The obvious way to implement the quadratic formula suffers catastrophic loss of accuracy when one of the roots to be found is much closer to 0 than the other. In their classic textbook on numeric methods Computer Methods for Mathematical Computations, George Forsythe, Michael Malcolm, and Cleve Moler suggest trying the naive algorithm with
a
1
{\displaystyle a=1}
,
b
−
10
5
{\displaystyle b=-10^{5}}
, and
c
1
{\displaystyle c=1}
. (For double-precision floats, set
b
−
10
9
{\displaystyle b=-10^{9}}
.) Consider the following implementation in Ada: As we can see, the second root has lost all significant figures. The right answer is that X2 is about
10
− 6
{\displaystyle 10^{-6}}
. The naive method is numerically unstable. Suggested by Middlebrook (D-OA), a better numerical method: to define two parameters
q
a c
/
b
{\displaystyle q={\sqrt {ac}}/b}
and
f
1
/
2 +
1 − 4
q
2
/
2
{\displaystyle f=1/2+{\sqrt {1-4q^{2}}}/2}
and the two roots of the quardratic are:
− b
a
f
{\displaystyle {\frac {-b}{a}}f}
and
− c
b f
{\displaystyle {\frac {-c}{bf}}}
Task: do it better. This means that given
a
1
{\displaystyle a=1}
,
b
−
10
9
{\displaystyle b=-10^{9}}
, and
c
1
{\displaystyle c=1}
, both of the roots your program returns should be greater than
10
− 11
{\displaystyle 10^{-11}}
. Or, if your language can't do floating-point arithmetic any more precisely than single precision, your program should be able to handle
b
−
10
6
{\displaystyle b=-10^{6}}
. Either way, show what your program gives as the roots of the quadratic in question. See page 9 of "What Every Scientist Should Know About Floating-Point Arithmetic" for a possible algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Roots of a quadratic function step by step in the C++ programming language
The provided code is a C++ program that solves quadratic equations of the form ax^2 + bx + c = 0, where a, b, and c are real numbers. Here's a detailed explanation of the code:
-
Header Includes:
- The code includes the
<iostream>
header for input and output operations. - It also includes
<utility>
for thestd::pair
type, which is used to store the two solutions of the quadratic equation. <complex>
is included for working with complex numbers, which are used to represent solutions with imaginary parts.
- The code includes the
-
Type Alias:
typedef std::complex<double> complex;
defines a new type aliascomplex
that represents a complex number with double-precision floating-point components.
-
Function
solve_quadratic_equation
:- This function takes three double-precision floating-point numbers
a
,b
, andc
as input, representing the coefficients of the quadratic equation. - It first divides
b
andc
bya
to simplify the equation. - Then, it calculates the discriminant, which is the expression
b*b-4*c
. - If the discriminant is negative, the equation has no real solutions, so it returns a pair of complex solutions using
std::make_pair
. - If the discriminant is non-negative, it calculates one of the two real solutions.
- Finally, it returns a pair of solutions, one of which is the calculated real solution, and the other is the other solution computed as
c / solution1
.
- This function takes three double-precision floating-point numbers
-
Main Function:
- In the
main
function, it calls thesolve_quadratic_equation
function witha = 1
,b = -1e20
, andc = 1
as input. This represents the equationx^2 - 1e20x + 1 = 0
. - It stores the returned pair of solutions in the
result
variable. - Finally, it prints the two solutions to the console.
- In the
In this specific case, the equation has two complex solutions: (-1e20 - 2i) / 2
and (-1e20 + 2i) / 2
. The code prints these solutions to the console.
Source code in the cpp programming language
#include <iostream>
#include <utility>
#include <complex>
typedef std::complex<double> complex;
std::pair<complex, complex>
solve_quadratic_equation(double a, double b, double c)
{
b /= a;
c /= a;
double discriminant = b*b-4*c;
if (discriminant < 0)
return std::make_pair(complex(-b/2, std::sqrt(-discriminant)/2),
complex(-b/2, -std::sqrt(-discriminant)/2));
double root = std::sqrt(discriminant);
double solution1 = (b > 0)? (-b - root)/2
: (-b + root)/2;
return std::make_pair(solution1, c/solution1);
}
int main()
{
std::pair<complex, complex> result = solve_quadratic_equation(1, -1e20, 1);
std::cout << result.first << ", " << result.second << std::endl;
}
You may also check:How to resolve the algorithm Calendar step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Comments step by step in the TXR programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Even or odd step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Random Latin squares step by step in the Go programming language