From 6f44762efbda7dc5a152cf123748b1b59ce41003 Mon Sep 17 00:00:00 2001 From: Thomas Marks Date: Thu, 9 Nov 2023 22:45:33 -0500 Subject: [PATCH] add more examples to readme and update tests --- README.md | 36 ++++++++++++++++++++++++++++++------ tests/tests.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f44bad8..b0a311a 100644 --- a/README.md +++ b/README.md @@ -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```, 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```, 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 diff --git a/tests/tests.cpp b/tests/tests.cpp index c822c37..a1beb79 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -58,9 +58,15 @@ class quadratic_test { quadratic_test(T _a, T _b, T _c) : a(_a), b(_b), c(_c), x1(NaN), x2(NaN) {} 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; } }; @@ -216,6 +222,41 @@ TEST_CASE("two solutions") { } } +TEST_CASE("readme") { + std::vector> testsf{ + quadratic_test(1.0f, 0.0f, -1.0f, -1.0f, 1.0f), + quadratic_test(1.0f, 0.0f, 1.0f), + quadratic_test(1.0f, 2.0f, 1.0f, -1.0f), + quadratic_test(1.0f, -1.0f, -6.0f, -2.0f, 3.0f) + }; + + for (auto &test : testsf) { + test.validate(); + } + + std::vector> testsd{ + quadratic_test(1.0, 0.0, -1.0, -1.0, 1.0), + quadratic_test(1.0, 0.0, 1.0), + quadratic_test(1.0, 2.0, 1.0, -1.0), + quadratic_test(1.0, -1.0, -6.0, -2.0, 3.0) + }; + + for (auto &test : testsd) { + test.validate(); + } + + std::vector> testsl{ + quadratic_test(1.0l, 0.0l, -1.0l, -1.0l, 1.0l), + quadratic_test(1.0l, 0.0l, 1.0l), + quadratic_test(1.0l, 2.0l, 1.0l, -1.0l), + quadratic_test(1.0l, -1.0l, -6.0l, -2.0l, 3.0l) + }; + + for (auto &test : testsl) { + test.validate(); + } +} + template std::pair quad_naive(T a, T b, T c) { if (a == 0) {