57 throw std::invalid_argument(
"quadratic coefficient cannot be 0");
60 long double discriminant = b * b - 4 * a * c;
61 std::array<std::complex<long double>, 2> solutions{0, 0};
63 if (discriminant == 0) {
64 solutions[0] = -b * 0.5 / a;
65 solutions[1] = -b * 0.5 / a;
73 if (discriminant > 0) {
76 solutions[0] = std::complex<long double>{
77 (-b - std::sqrt(discriminant)) * 0.5 / a, 0};
78 solutions[1] = std::complex<long double>{
79 (-b + std::sqrt(discriminant)) * 0.5 / a, 0};
84 solutions[0] = std::complex<long double>{
85 -b * 0.5 / a, -std::sqrt(-discriminant) * 0.5 / a};
86 solutions[1] = std::complex<long double>{
87 -b * 0.5 / a, std::sqrt(-discriminant) * 0.5 / a};
101 std::array<std::complex<long double>, 2> expected,
102 size_t precision = 10) {
103 long double exponent = std::pow(10, precision);
104 input[0].real(std::round(input[0].real() * exponent));
105 input[1].real(std::round(input[1].real() * exponent));
106 input[0].imag(std::round(input[0].imag() * exponent));
107 input[1].imag(std::round(input[1].imag() * exponent));
109 expected[0].real(std::round(expected[0].real() * exponent));
110 expected[1].real(std::round(expected[1].real() * exponent));
111 expected[0].imag(std::round(expected[0].imag() * exponent));
112 expected[1].imag(std::round(expected[1].imag() * exponent));
114 assert(input == expected);
124 std::cout <<
"Input: \n"
128 "Expected output: \n"
129 "(1, 0), (1, 0)\n\n";
130 std::array<std::complex<long double>, 2> equalCase{
131 std::complex<long double>{1, 0}, std::complex<long double>{1, 0}};
135 std::cout <<
"Input: \n"
139 "Expected output: \n"
140 "(-2, -1), (-2, 1)\n\n";
141 std::array<std::complex<long double>, 2> complexCase{
142 std::complex<long double>{-2, -1}, std::complex<long double>{-2, 1}};
146 std::cout <<
"Input: \n"
150 "Expected output: \n"
151 "(-4.7912878475, 0), (-0.2087121525, 0)\n\n";
152 std::array<std::complex<long double>, 2> floatCase{
153 std::complex<long double>{-4.7912878475, 0},
154 std::complex<long double>{-0.2087121525, 0}};
158 std::cout <<
"Input: \n"
162 "Expected output: \n"
163 "(-0.5, -0.8660254038), (-0.5, 0.8660254038)\n\n";
164 std::array<std::complex<long double>, 2> ifloatCase{
165 std::complex<long double>{-0.5, -0.8660254038},
166 std::complex<long double>{-0.5, 0.8660254038}};
169 std::cout <<
"Exception test: \n"
174 "Expected output: Exception thrown \n";
177 }
catch (std::invalid_argument& e) {
178 std::cout <<
"Exception thrown successfully \n";
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.