From 73627884351971836dfdee747927678ee3b62dc5 Mon Sep 17 00:00:00 2001 From: Faruk D Date: Tue, 9 Jun 2020 15:59:27 +0200 Subject: [PATCH 1/4] fix c++ code and displayed precision --- README.md | 17 +++++++++++++++-- src/cgi-newtonraphson.cpp | 10 +++++++++- src/cli-newtonraphson.cpp | 11 ++++++++++- src/py-newtonraphson.cpp | 7 ++++++- src/py/example.py | 2 +- src/wasm-newtonraphson.cpp | 7 ++++++- 6 files changed, 47 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 81b2bcc..91feb9b 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,11 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + std::cout << "initial guess: " << xin << std::endl; + std::cout << "initial tolerance: " << tolerance << std::endl; + + while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); @@ -122,6 +127,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 +139,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; } ``` @@ -220,6 +229,7 @@ A response should consist of the content type such as ``application/json`` or `` // this C++ snippet is stored as src/cgi-newtonraphson.hpp #include #include +#include #include <> @@ -241,6 +251,8 @@ int main(int argc, char *argv[]) nlohmann::json response; response["guess"] = guess; response["root"] = root; + std::cout << std::fixed; + std::cout << std::setprecision(6); std::cout << response.dump(2) << std::endl; return 0; } @@ -377,7 +389,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..a248c24 100644 --- a/src/cgi-newtonraphson.cpp +++ b/src/cgi-newtonraphson.cpp @@ -1,11 +1,13 @@ // this C++ snippet is stored as src/cgi-newtonraphson.hpp #include #include +#include #include // this C++ code snippet is later referred to as <> #include "newtonraphson.hpp" #include "algebra.hpp" +#include using namespace algebra; @@ -19,7 +21,11 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + std::cout << "initial guess: " << xin << std::endl; + std::cout << "initial tolerance: " << tolerance << std::endl; + + while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); @@ -49,6 +55,8 @@ int main(int argc, char *argv[]) nlohmann::json response; response["guess"] = guess; response["root"] = root; + std::cout << std::fixed; + std::cout << std::setprecision(6); std::cout << response.dump(2) << std::endl; return 0; } \ No newline at end of file diff --git a/src/cli-newtonraphson.cpp b/src/cli-newtonraphson.cpp index eba45c5..7b69d50 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,11 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + std::cout << "initial guess: " << xin << std::endl; + std::cout << "initial tolerance: " << tolerance << std::endl; + + while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); @@ -38,6 +44,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/py-newtonraphson.cpp b/src/py-newtonraphson.cpp index 4ab1b40..d0db33d 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,11 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + std::cout << "initial guess: " << xin << std::endl; + std::cout << "initial tolerance: " << tolerance << std::endl; + + 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..8eb613c 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,11 @@ double NewtonRaphson::solve(double xin) { double x = xin; double delta_x = equation(x) / derivative(x); - while (abs(delta_x) >= tolerance) + + std::cout << "initial guess: " << xin << std::endl; + std::cout << "initial tolerance: " << tolerance << std::endl; + + while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); From bac102eeb313fe8e8d98244d608c031a0606d8cb Mon Sep 17 00:00:00 2001 From: Faruk D Date: Tue, 9 Jun 2020 16:13:32 +0200 Subject: [PATCH 2/4] donot shot initial values and update outputs --- README.md | 9 +++------ src/cgi-newtonraphson.cpp | 3 --- src/cli-newtonraphson.cpp | 3 --- src/py-newtonraphson.cpp | 3 --- src/wasm-newtonraphson.cpp | 3 --- 5 files changed, 3 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 91feb9b..ef94f10 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,6 @@ double NewtonRaphson::solve(double xin) double x = xin; double delta_x = equation(x) / derivative(x); - std::cout << "initial guess: " << xin << std::endl; - std::cout << "initial tolerance: " << tolerance << std::endl; - while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); @@ -162,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. @@ -279,7 +276,7 @@ Content-type: application/json { "guess": -20.0, - "root": -1.622923986083026 + "root": -1.000000 } ``` @@ -319,7 +316,7 @@ Should return the following JSON document as a response ```json { "guess": -20, - "root":-1.62292 + "root":-1.000000 } ``` diff --git a/src/cgi-newtonraphson.cpp b/src/cgi-newtonraphson.cpp index a248c24..ded3d31 100644 --- a/src/cgi-newtonraphson.cpp +++ b/src/cgi-newtonraphson.cpp @@ -22,9 +22,6 @@ double NewtonRaphson::solve(double xin) double x = xin; double delta_x = equation(x) / derivative(x); - std::cout << "initial guess: " << xin << std::endl; - std::cout << "initial tolerance: " << tolerance << std::endl; - while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); diff --git a/src/cli-newtonraphson.cpp b/src/cli-newtonraphson.cpp index 7b69d50..6e266fc 100644 --- a/src/cli-newtonraphson.cpp +++ b/src/cli-newtonraphson.cpp @@ -20,9 +20,6 @@ double NewtonRaphson::solve(double xin) double x = xin; double delta_x = equation(x) / derivative(x); - std::cout << "initial guess: " << xin << std::endl; - std::cout << "initial tolerance: " << tolerance << std::endl; - while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); diff --git a/src/py-newtonraphson.cpp b/src/py-newtonraphson.cpp index d0db33d..af665d5 100644 --- a/src/py-newtonraphson.cpp +++ b/src/py-newtonraphson.cpp @@ -20,9 +20,6 @@ double NewtonRaphson::solve(double xin) double x = xin; double delta_x = equation(x) / derivative(x); - std::cout << "initial guess: " << xin << std::endl; - std::cout << "initial tolerance: " << tolerance << std::endl; - while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); diff --git a/src/wasm-newtonraphson.cpp b/src/wasm-newtonraphson.cpp index 8eb613c..992b7dc 100644 --- a/src/wasm-newtonraphson.cpp +++ b/src/wasm-newtonraphson.cpp @@ -19,9 +19,6 @@ double NewtonRaphson::solve(double xin) double x = xin; double delta_x = equation(x) / derivative(x); - std::cout << "initial guess: " << xin << std::endl; - std::cout << "initial tolerance: " << tolerance << std::endl; - while (fabs(delta_x) >= tolerance) { delta_x = equation(x) / derivative(x); From b5cd9775f2b3d4091ff44a1fbc7968ea8d5a598a Mon Sep 17 00:00:00 2001 From: Faruk D Date: Tue, 9 Jun 2020 16:19:54 +0200 Subject: [PATCH 3/4] disable precision for cgi-bin --- README.md | 5 +---- src/cgi-newtonraphson.cpp | 3 --- src/js/newtonraphsonwasm.wasm | Bin 26547 -> 26500 bytes 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/README.md b/README.md index ef94f10..af761eb 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,6 @@ A response should consist of the content type such as ``application/json`` or `` // this C++ snippet is stored as src/cgi-newtonraphson.hpp #include #include -#include #include <> @@ -248,8 +247,6 @@ int main(int argc, char *argv[]) nlohmann::json response; response["guess"] = guess; response["root"] = root; - std::cout << std::fixed; - std::cout << std::setprecision(6); std::cout << response.dump(2) << std::endl; return 0; } @@ -276,7 +273,7 @@ Content-type: application/json { "guess": -20.0, - "root": -1.000000 + "root": -1.0000001181322415 } ``` diff --git a/src/cgi-newtonraphson.cpp b/src/cgi-newtonraphson.cpp index ded3d31..eb8f410 100644 --- a/src/cgi-newtonraphson.cpp +++ b/src/cgi-newtonraphson.cpp @@ -1,7 +1,6 @@ // this C++ snippet is stored as src/cgi-newtonraphson.hpp #include #include -#include #include // this C++ code snippet is later referred to as <> @@ -52,8 +51,6 @@ int main(int argc, char *argv[]) nlohmann::json response; response["guess"] = guess; response["root"] = root; - std::cout << std::fixed; - std::cout << std::setprecision(6); std::cout << response.dump(2) << std::endl; return 0; } \ No newline at end of file diff --git a/src/js/newtonraphsonwasm.wasm b/src/js/newtonraphsonwasm.wasm index 0d74f8ca980ef276baf4dcae3620d20b46a792ff..9a34638d02253632bba1b5bbf75e6faf949d5d25 100644 GIT binary patch delta 2886 zcmbVNdvu&t5x;YHv)SyoyO}hv-DIx9?fIZnAS98skIf2Kwr>zLz}|VrlCn1 zEJiodLMawO2Sj{utPe!}Lpggyk4jIgtq-&m8j1+jqEu9%+JY#w(mMAhWs@U+c--AH z-*0|1cjnH_{qAOu!mgum&qANf3`1r;48!mvS1*)_bMPhzx8wMNZt0dnfEfEo@UkYP zPfFqNFMCiH0kqSot8P^Xu3OjB-@mnQ-Il%$-Gk}At>R_y^mcDyRlEY8O`EoCAGkrB zgMxv3y8+$IB)7;(ESXakV~r>ZPeIklv?Hu(TN4oBcu@M|~*Prz~b zE4&2H!cq7O{0V*wPr{$!kMKM^1-}E|cfZAL&l9OL+Q}FAUi=9-H%V8<8J;RMWOwBG z0El*ZM&JSR|W%G)Xv7}f<03tHtgtk6D&zE-JHF6}UMT4e+y+K*K_ zfKeU9m@ZVVgiL5;E4IoOR5aoZ&DFZZ@Y8guc_>}R=CW4Vj#`(GF(LD)(_!<7=nA%1 zw8~D5(=l&1Orw^9Jusd27p#RDROP!L4%7F1)!}L_lqhr@fD9%k;FZywJ}uHC&Rn)+Ka6c~n5Ti+rMj`pTp9u+tTbu+f8cT&f4_O_gTVI~C5N691#g2-oIL z$Pm}&V2dtdIa^OLlwTJsw<wi9p6uIxACx!n3b(>JF0aa0-XbYDO#wDi zG&l%NbOdM8kAo>_rVGKt&_;&}Kgzfh%V7>(i-RziJvPB1YAO1TyR^``j?Pxr*=G8+ zTe)$b&Qp2#Q!bUBtc+$CO!0$rnVzX;ssy^QlU5g3XHqJTmn)6)mBRO_7%osze7~x~ zg(`xJR3&~uRp1pWj32}g=?2xHLYRkLx=}T%az-E4O{z&vQDx}CD|NGKRwb&G@l`sh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6DHR!;1ZRRUeJO7igTy@WpJHd!l8*kPh=@D-3N( zE{@7XAI~=HNwrE0t5-yYpJmUQwaD+N^CB4j?o=B6+|a9OJw2BCkgAixyr(W!+tq*! zs($Iq``&c=(JUQ4d@=33DA*6w5!nTc)B%iTu8etY=|>=7%mSX~ZFCdf1`}?L0lo<_ z?q?=Feu4{X2fhVHlGY{YgCba|1m0$ABbw}(p9K67fyI6%SiYUngJ7V+48iE*K|xP} zHODzK+{`>)j?aK$Cxy{TY1R1bHR&Him_*XkzYc=WX#$^y|64LBiU5=01Q-cMwNW`% zA-|wPC z8c!xl;J4~*B1wF0_41kt@N=DkJ_P2s!nqVA$7nG&D)Xfh^OGUc98+Jdj*;7VbqIc^ zUayu@_=K8W6D7R3Ch9r{+Ka0|Wx}ejS{<&rfpn4D*_8Z>+63Xj+Bzh=?2hWne0V~A zt=0-xTUjtwPn@IHp-PLvhW;5?LrQC2@buS#6d74@z9PNJax%548Y!Jp!jGOOGPENX(`0ezCIj!DVxRR`w zw1#QyYi)OvkAoU^I`hreJTJVFInuGmpy`?w+wK0pgLz!)J2D~hAL{t^(M9L82lQ{& ztAyu)UM1Q*pfmd}9j1U`GD&B{q_c5C(z(k5JV$nP$nmV2-x-;wovkAu=Y{$dl`XZd m*m(*tY}x6MbU7#_U4}Ssp)1Yk2LDs->--WJnfa@)_Wv8;dw{b5 From 854ed6fb2562df24f84dad5f34576aa8113150a7 Mon Sep 17 00:00:00 2001 From: Stefan Verhoeven Date: Tue, 9 Jun 2020 16:21:19 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index af761eb..ad08cc3 100644 --- a/README.md +++ b/README.md @@ -313,7 +313,7 @@ Should return the following JSON document as a response ```json { "guess": -20, - "root":-1.000000 + "root":-1.0000001181322415 } ```