mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Added test cases
This commit is contained in:
parent
685469d2c4
commit
a1433a9318
@ -52,20 +52,20 @@ std::array<std::complex<long double>, 2> quadraticEquation(long double a,
|
|||||||
long double discriminant = b * b - 4 * a * c;
|
long double discriminant = b * b - 4 * a * c;
|
||||||
std::array<std::complex<long double>, 2> solutions{0, 0};
|
std::array<std::complex<long double>, 2> solutions{0, 0};
|
||||||
|
|
||||||
// Complex root (discriminant < 0)
|
/// Complex root (discriminant < 0)
|
||||||
// Note that the left term (-b / 2a) is always real. The imaginary part
|
/// Note that the left term (-b / 2a) is always real. The imaginary part
|
||||||
// appears when b^2 - 4ac < 0, so sqrt(b^2 - 4ac) has no real roots. So, the
|
/// appears when b^2 - 4ac < 0, so sqrt(b^2 - 4ac) has no real roots. So,
|
||||||
// imaginary component is i * (+/-)sqrt(abs(b^2 - 4ac)) / 2a.
|
/// the imaginary component is i * (+/-)sqrt(abs(b^2 - 4ac)) / 2a.
|
||||||
if (discriminant < 0) {
|
if (discriminant < 0) {
|
||||||
// Since b^2 - 4ac is < 0, for faster computation, -discriminant is
|
/// Since b^2 - 4ac is < 0, for faster computation, -discriminant is
|
||||||
// enough to make it positive.
|
/// enough to make it positive.
|
||||||
solutions[0] = std::complex<long double>{
|
solutions[0] = std::complex<long double>{
|
||||||
-b * 0.5 / a, -std::sqrt(-discriminant) * 0.5 / a};
|
-b * 0.5 / a, -std::sqrt(-discriminant) * 0.5 / a};
|
||||||
solutions[1] = std::complex<long double>{
|
solutions[1] = std::complex<long double>{
|
||||||
-b * 0.5 / a, std::sqrt(-discriminant) * 0.5 / a};
|
-b * 0.5 / a, std::sqrt(-discriminant) * 0.5 / a};
|
||||||
} else {
|
} else {
|
||||||
// Since discriminant > 0, there are only real roots. Therefore,
|
/// Since discriminant > 0, there are only real roots. Therefore,
|
||||||
// imaginary component = 0.
|
/// imaginary component = 0.
|
||||||
solutions[0] = std::complex<long double>{
|
solutions[0] = std::complex<long double>{
|
||||||
(-b - std::sqrt(discriminant)) * 0.5 / a, 0};
|
(-b - std::sqrt(discriminant)) * 0.5 / a, 0};
|
||||||
solutions[1] = std::complex<long double>{
|
solutions[1] = std::complex<long double>{
|
||||||
|
Loading…
Reference in New Issue
Block a user