TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
quadratic_equations_complex_numbers.cpp File Reference

Calculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0. More...

#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <exception>
#include <iomanip>
#include <iostream>
Include dependency graph for quadratic_equations_complex_numbers.cpp:

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 

Functions

std::array< std::complex< long double >, 2 > math::quadraticEquation (long double a, long double b, long double c)
 Quadratic equation calculator.
 
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.
 
static void test ()
 Self-test implementations to test quadraticEquation function.
 
int main ()
 Main function.
 

Detailed Description

Calculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0.

Author
Renjian-buchai

@description Calculates any quadratic equation in form ax^2 + bx + c.

Quadratic equation: x = (-b +/- sqrt(b^2 - 4ac)) / 2a

Definition in file quadratic_equations_complex_numbers.cpp.

Function Documentation

◆ assertArray()

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.

Parameters
inputInput array of complex numbers. .
expectedExpected array of complex numbers.
precisionPrecision to be asserted. Default=10
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp.

Definition at line 100 of file quadratic_equations_complex_numbers.cpp.

102 {
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));
108
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));
113
114 assert(input == expected);
115}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 186 of file quadratic_equations_complex_numbers.cpp.

186 {
187 test(); // Run self-test implementation.
188 return 0;
189}
static void test()
Self-test implementations to test quadraticEquation function.

◆ test()

static void test ( )
static

Self-test implementations to test quadraticEquation function.

Note
There are 4 different types of solutions: Real and equal, real, complex, complex and equal.

Definition at line 122 of file quadratic_equations_complex_numbers.cpp.

122 {
123 // Values are equal and real.
124 std::cout << "Input: \n"
125 "a=1 \n"
126 "b=-2 \n"
127 "c=1 \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}};
132 assert(math::quadraticEquation(1, -2, 1) == equalCase);
133
134 // Values are equal and complex.
135 std::cout << "Input: \n"
136 "a=1 \n"
137 "b=4 \n"
138 "c=5 \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}};
143 assert(math::quadraticEquation(1, 4, 5) == complexCase);
144
145 // Values are real.
146 std::cout << "Input: \n"
147 "a=1 \n"
148 "b=5 \n"
149 "c=1 \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}};
155 assertArray(math::quadraticEquation(1, 5, 1), floatCase);
156
157 // Values are complex.
158 std::cout << "Input: \n"
159 "a=1 \n"
160 "b=1 \n"
161 "c=1 \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}};
167 assertArray(math::quadraticEquation(1, 1, 1), ifloatCase);
168
169 std::cout << "Exception test: \n"
170 "Input: \n"
171 "a=0 \n"
172 "b=0 \n"
173 "c=0\n"
174 "Expected output: Exception thrown \n";
175 try {
177 } catch (std::invalid_argument& e) {
178 std::cout << "Exception thrown successfully \n";
179 }
180}
std::array< std::complex< long double >, 2 > quadraticEquation(long double a, long double b, long double c)
Quadratic equation calculator.
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.