Skip to content

Commit

Permalink
add more examples to readme and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
archermarx committed Nov 10, 2023
1 parent 5e8802d commit 6f44762
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,41 @@ Download the [header file](https://github.com/archermarx/quadratic/include/heade

## Usage

To solve a quadratic equation with parameters `a`, `b`, and `c`, call the `solve` function in the `quadratic` namespace. This function returns a ```std::pair<T, T>```, where `T` is the input type.
If there are no real solutions, then the pair will contain two NaNs. If there is only one solution, then it will be contained in the first element, while the second element of the pair will be NaN.
To solve a quadratic equation with parameters `a`, `b`, and `c`, call the `solve` function in the `quadratic` namespace.This function returns a ```std::pair<T, T>```, where `T` is the input type. `T` can be any floating point type.

```cpp
float a = 1.0f;
float b = 0.0f;
float c = -1.0f;
auto [x1, x2] = quadratic::solve(a, b, c); // [-1, 1]
```

If there are no real solutions, then the pair will contain two NaNs.

```cpp
long double a = 1.0;
long double b = 0.0;
long double c = 1.0;
auto [x1, x2] = quadratic::solve(a, b, c); // [nan, nan]
```

If there is only one solution, it will be contained in the first element of the returned pair, while the second element of the pair will be a NaN.

```cpp
double a = 1.0;
double b = 1.0;
double b = 2.0;
double c = 1.0;
auto [x1, x2] = quadratic::solve(a, b, c);
auto [x1, x2] = quadratic::solve(a, b, c); // [-1, nan]
```

When the equation has two solutions, the first solution will be the smaller of the two, i.e. `x1 < x2`.

```cpp
double a = 1.0;
double b = -1.0;
double c = 6.0;
auto [x1, x2] = quadratic::solve(a, b, c) // [-2, 3]
```
Robust C++ quadratic equation solver.
When the equation has two solutions, the first solution will be the smaller of the two, i.e. `x1 < x2`. The `solve` function is templated and should work with any floating point type.

## Performance

Expand Down
47 changes: 44 additions & 3 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ class quadratic_test {
quadratic_test(T _a, T _b, T _c) : a(_a), b(_b), c(_c), x1(NaN<T>), x2(NaN<T>) {}

void validate() {
auto solution = solve(a, b, c);
CHECK(isapprox(x1, solution.first));
CHECK(isapprox(x2, solution.second));
auto [_x1, _x2] = solve(a, b, c);
CHECK(isapprox(x1, _x1));
CHECK(isapprox(x2, _x2));

if (!isapprox(x1, _x1) || !isapprox(x2, _x2)){
std::cout << "Quadratic a = " << a << ", b = " << b << ", c = " << c << ": " << std::endl;
std::cout << "Expected solution: " << x1 << ", " << x2 << std::endl;
std::cout << "Computed solution: " << _x1 << ", " << _x2 << std::endl;
}
return;
}
};
Expand Down Expand Up @@ -216,6 +222,41 @@ TEST_CASE("two solutions") {
}
}

TEST_CASE("readme") {
std::vector<quadratic_test<float>> testsf{
quadratic_test<float>(1.0f, 0.0f, -1.0f, -1.0f, 1.0f),
quadratic_test<float>(1.0f, 0.0f, 1.0f),
quadratic_test<float>(1.0f, 2.0f, 1.0f, -1.0f),
quadratic_test<float>(1.0f, -1.0f, -6.0f, -2.0f, 3.0f)
};

for (auto &test : testsf) {
test.validate();
}

std::vector<quadratic_test<double>> testsd{
quadratic_test<double>(1.0, 0.0, -1.0, -1.0, 1.0),
quadratic_test<double>(1.0, 0.0, 1.0),
quadratic_test<double>(1.0, 2.0, 1.0, -1.0),
quadratic_test<double>(1.0, -1.0, -6.0, -2.0, 3.0)
};

for (auto &test : testsd) {
test.validate();
}

std::vector<quadratic_test<long double>> testsl{
quadratic_test<long double>(1.0l, 0.0l, -1.0l, -1.0l, 1.0l),
quadratic_test<long double>(1.0l, 0.0l, 1.0l),
quadratic_test<long double>(1.0l, 2.0l, 1.0l, -1.0l),
quadratic_test<long double>(1.0l, -1.0l, -6.0l, -2.0l, 3.0l)
};

for (auto &test : testsl) {
test.validate();
}
}

template <typename T>
std::pair<T, T> quad_naive(T a, T b, T c) {
if (a == 0) {
Expand Down

0 comments on commit 6f44762

Please sign in to comment.