diff --git a/README.md b/README.md index 81b2bcc..ad08cc3 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ The implementation of the algorithm would look like // this C++ code snippet is later referred to as <> #include "newtonraphson.hpp" #include "algebra.hpp" +#include using namespace algebra; @@ -103,7 +104,8 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); @@ -122,6 +124,7 @@ We are now ready to call the algorithm in a simple CLI program. It would look li ```{.cpp file=src/cli-newtonraphson.cpp} // this C++ snippet is stored as src/newtonraphson.cpp #include +#include <> @@ -133,7 +136,10 @@ int main() rootfinding::NewtonRaphson finder(epsilon); double x1 = finder.solve(x0); + std::cout << std::fixed; + std::cout << std::setprecision(6); std::cout << "The value of the root is : " << x1 << std::endl; + return 0; } ``` @@ -153,7 +159,7 @@ Run with Should output ```shell -The value of the root is : -1.62292 +The value of the root is : -1.000000 ``` A C++ algorithm is a collection of functions/classes that can perform a mathematical computation. @@ -267,7 +273,7 @@ Content-type: application/json { "guess": -20.0, - "root": -1.622923986083026 + "root": -1.0000001181322415 } ``` @@ -307,7 +313,7 @@ Should return the following JSON document as a response ```json { "guess": -20, - "root":-1.62292 + "root":-1.0000001181322415 } ``` @@ -377,7 +383,8 @@ from newtonraphsonpy import NewtonRaphson finder = NewtonRaphson(epsilon=0.001) root = finder.solve(guess=-20) -print(root) +print ("{0:.6f}".format(root)) + ``` The Python example can be run with diff --git a/src/cgi-newtonraphson.cpp b/src/cgi-newtonraphson.cpp index 04a5feb..eb8f410 100644 --- a/src/cgi-newtonraphson.cpp +++ b/src/cgi-newtonraphson.cpp @@ -6,6 +6,7 @@ // this C++ code snippet is later referred to as <> #include "newtonraphson.hpp" #include "algebra.hpp" +#include using namespace algebra; @@ -19,7 +20,8 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); diff --git a/src/cli-newtonraphson.cpp b/src/cli-newtonraphson.cpp index eba45c5..6e266fc 100644 --- a/src/cli-newtonraphson.cpp +++ b/src/cli-newtonraphson.cpp @@ -1,9 +1,11 @@ // this C++ snippet is stored as src/newtonraphson.cpp #include +#include // this C++ code snippet is later referred to as <> #include "newtonraphson.hpp" #include "algebra.hpp" +#include using namespace algebra; @@ -17,7 +19,8 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); @@ -38,6 +41,9 @@ int main() rootfinding::NewtonRaphson finder(epsilon); double x1 = finder.solve(x0); + std::cout << std::fixed; + std::cout << std::setprecision(6); std::cout << "The value of the root is : " << x1 << std::endl; + return 0; } \ No newline at end of file diff --git a/src/js/newtonraphsonwasm.wasm b/src/js/newtonraphsonwasm.wasm index 0d74f8c..9a34638 100644 Binary files a/src/js/newtonraphsonwasm.wasm and b/src/js/newtonraphsonwasm.wasm differ diff --git a/src/py-newtonraphson.cpp b/src/py-newtonraphson.cpp index 4ab1b40..af665d5 100644 --- a/src/py-newtonraphson.cpp +++ b/src/py-newtonraphson.cpp @@ -5,6 +5,7 @@ // this C++ code snippet is later referred to as <> #include "newtonraphson.hpp" #include "algebra.hpp" +#include using namespace algebra; @@ -18,7 +19,8 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); diff --git a/src/py/example.py b/src/py/example.py index 12df20d..beb78c3 100644 --- a/src/py/example.py +++ b/src/py/example.py @@ -3,4 +3,4 @@ finder = NewtonRaphson(epsilon=0.001) root = finder.solve(guess=-20) -print(root) \ No newline at end of file +print ("{0:.6f}".format(root)) \ No newline at end of file diff --git a/src/wasm-newtonraphson.cpp b/src/wasm-newtonraphson.cpp index 32a697d..992b7dc 100644 --- a/src/wasm-newtonraphson.cpp +++ b/src/wasm-newtonraphson.cpp @@ -4,6 +4,7 @@ // this C++ code snippet is later referred to as <> #include "newtonraphson.hpp" #include "algebra.hpp" +#include using namespace algebra; @@ -17,7 +18,8 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x);