@@ -90,6 +90,7 @@ The implementation of the algorithm would look like
9090// this C++ code snippet is later referred to as <<algorithm>>
9191#include " newtonraphson.hpp"
9292#include " algebra.hpp"
93+ #include < math.h>
9394
9495using namespace algebra ;
9596
@@ -103,7 +104,8 @@ double NewtonRaphson::solve(double xin)
103104{
104105 double x = xin;
105106 double delta_x = equation(x) / derivative(x);
106- while (abs(delta_x) >= tolerance)
107+
108+ while (fabs(delta_x) >= tolerance)
107109 {
108110 delta_x = equation(x) / derivative(x);
109111
@@ -122,6 +124,7 @@ We are now ready to call the algorithm in a simple CLI program. It would look li
122124```{.cpp file=src/cli-newtonraphson.cpp}
123125// this C++ snippet is stored as src/newtonraphson.cpp
124126#include<bits/stdc++.h>
127+ #include <iomanip>
125128
126129<<algorithm>>
127130
@@ -133,7 +136,10 @@ int main()
133136 rootfinding::NewtonRaphson finder(epsilon);
134137 double x1 = finder.solve(x0);
135138
139+ std::cout << std::fixed;
140+ std::cout << std::setprecision(6);
136141 std::cout << "The value of the root is : " << x1 << std::endl;
142+
137143 return 0;
138144}
139145```
@@ -153,7 +159,7 @@ Run with
153159Should output
154160
155161``` shell
156- The value of the root is : -1.62292
162+ The value of the root is : -1.000000
157163```
158164
159165A C++ algorithm is a collection of functions/classes that can perform a mathematical computation.
@@ -267,7 +273,7 @@ Content-type: application/json
267273
268274{
269275 " guess" : -20.0,
270- " root" : -1.622923986083026
276+ " root" : -1.0000001181322415
271277}
272278```
273279
@@ -307,7 +313,7 @@ Should return the following JSON document as a response
307313``` json
308314{
309315 "guess" : -20 ,
310- "root" :-1.62292
316+ "root" :-1.0000001181322415
311317}
312318```
313319
@@ -377,7 +383,8 @@ from newtonraphsonpy import NewtonRaphson
377383
378384finder = NewtonRaphson(epsilon=0.001)
379385root = finder.solve(guess=-20)
380- print(root)
386+ print ("{0:.6f}".format(root))
387+
381388```
382389
383390The Python example can be run with
0 commit comments