int main() { using std::array; using std::complex; using std::cout;
array<complex<long double, 2> solutions = quadraticEquation(1, 2, 1); cout << solutions[0] << " " << solutions[1] << "\n";
solutions = quadraticEquation(1, 1, 1); // Reusing solutions. cout << solutions[0] << " " << solutions[1] << "\n"; return 0; }
Output: (-1, 0) (-1, 0) (-0.5,0.866025) (-0.5,0.866025)
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <exception>
#include <iomanip>
#include <iostream>
long double b,
long double c) {
if (a == 0) {
}
long double discriminant = b * b - 4 * a * c;
if (discriminant == 0) {
solutions[0] = -b * 0.5 / a;
solutions[1] = -b * 0.5 / a;
return solutions;
}
if (discriminant > 0) {
(-b -
std::sqrt(discriminant)) * 0.5 / a, 0};
(-b +
std::sqrt(discriminant)) * 0.5 / a, 0};
return solutions;
}
-b * 0.5 / a, -
std::sqrt(-discriminant) * 0.5 / a};
-b * 0.5 / a,
std::sqrt(-discriminant) * 0.5 / a};
return solutions;
}
}
size_t precision = 10) {
long double exponent =
std::pow(10, precision);
input[0].real(
std::round(input[0].real() * exponent));
input[1].real(
std::round(input[1].real() * exponent));
input[0].imag(
std::round(input[0].imag() * exponent));
input[1].imag(
std::round(input[1].imag() * exponent));
expected[0].real(
std::round(expected[0].real() * exponent));
expected[1].real(
std::round(expected[1].real() * exponent));
expected[0].imag(
std::round(expected[0].imag() * exponent));
expected[1].imag(
std::round(expected[1].imag() * exponent));
assert(input == expected);
}
"a=1 \n"
"b=-2 \n"
"c=1 \n"
"Expected output: \n"
"(1, 0), (1, 0)\n\n";
"a=1 \n"
"b=4 \n"
"c=5 \n"
"Expected output: \n"
"(-2, -1), (-2, 1)\n\n";
"a=1 \n"
"b=5 \n"
"c=1 \n"
"Expected output: \n"
"(-4.7912878475, 0), (-0.2087121525, 0)\n\n";
"a=1 \n"
"b=1 \n"
"c=1 \n"
"Expected output: \n"
"(-0.5, -0.8660254038), (-0.5, 0.8660254038)\n\n";
"Input: \n"
"a=0 \n"
"b=0 \n"
"c=0\n"
"Expected output: Exception thrown \n";
try {
std::cout <<
"Exception thrown successfully \n";
}
}
return 0;
}
static void test()
Self-test implementations.
Definition: generate_parentheses.cpp:82
int main()
Main function.
Definition: generate_parentheses.cpp:110
std::array< std::complex< long double >, 2 > quadraticEquation(long double a, long double b, long double c)
Quadratic equation calculator.
Definition: quadratic_equations_complex_numbers.cpp:53
void assertArray(std::array< std::complex< long double >, 2 > input, std::array< std::complex< long double >, 2 > expected, size_t precision=10)
Asserts an array of complex numbers.
Definition: quadratic_equations_complex_numbers.cpp:100