Skip to content

Commit 86c0c4d

Browse files
authored
Merge pull request #79 from NLESC-JCER/fix-precision
fix c++ code and displayed precision
2 parents 935240b + 854ed6f commit 86c0c4d

File tree

7 files changed

+29
-10
lines changed

7 files changed

+29
-10
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

9495
using 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
153159
Should output
154160

155161
```shell
156-
The value of the root is : -1.62292
162+
The value of the root is : -1.000000
157163
```
158164

159165
A 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
378384
finder = NewtonRaphson(epsilon=0.001)
379385
root = finder.solve(guess=-20)
380-
print(root)
386+
print ("{0:.6f}".format(root))
387+
381388
```
382389

383390
The Python example can be run with

src/cgi-newtonraphson.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// this C++ code snippet is later referred to as <<algorithm>>
77
#include "newtonraphson.hpp"
88
#include "algebra.hpp"
9+
#include <math.h>
910

1011
using namespace algebra;
1112

@@ -19,7 +20,8 @@ double NewtonRaphson::solve(double xin)
1920
{
2021
double x = xin;
2122
double delta_x = equation(x) / derivative(x);
22-
while (abs(delta_x) >= tolerance)
23+
24+
while (fabs(delta_x) >= tolerance)
2325
{
2426
delta_x = equation(x) / derivative(x);
2527

src/cli-newtonraphson.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// this C++ snippet is stored as src/newtonraphson.cpp
22
#include<bits/stdc++.h>
3+
#include <iomanip>
34

45
// this C++ code snippet is later referred to as <<algorithm>>
56
#include "newtonraphson.hpp"
67
#include "algebra.hpp"
8+
#include <math.h>
79

810
using namespace algebra;
911

@@ -17,7 +19,8 @@ double NewtonRaphson::solve(double xin)
1719
{
1820
double x = xin;
1921
double delta_x = equation(x) / derivative(x);
20-
while (abs(delta_x) >= tolerance)
22+
23+
while (fabs(delta_x) >= tolerance)
2124
{
2225
delta_x = equation(x) / derivative(x);
2326

@@ -38,6 +41,9 @@ int main()
3841
rootfinding::NewtonRaphson finder(epsilon);
3942
double x1 = finder.solve(x0);
4043

44+
std::cout << std::fixed;
45+
std::cout << std::setprecision(6);
4146
std::cout << "The value of the root is : " << x1 << std::endl;
47+
4248
return 0;
4349
}

src/js/newtonraphsonwasm.wasm

-47 Bytes
Binary file not shown.

src/py-newtonraphson.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// this C++ code snippet is later referred to as <<algorithm>>
66
#include "newtonraphson.hpp"
77
#include "algebra.hpp"
8+
#include <math.h>
89

910
using namespace algebra;
1011

@@ -18,7 +19,8 @@ double NewtonRaphson::solve(double xin)
1819
{
1920
double x = xin;
2021
double delta_x = equation(x) / derivative(x);
21-
while (abs(delta_x) >= tolerance)
22+
23+
while (fabs(delta_x) >= tolerance)
2224
{
2325
delta_x = equation(x) / derivative(x);
2426

src/py/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
finder = NewtonRaphson(epsilon=0.001)
55
root = finder.solve(guess=-20)
6-
print(root)
6+
print ("{0:.6f}".format(root))

src/wasm-newtonraphson.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// this C++ code snippet is later referred to as <<algorithm>>
55
#include "newtonraphson.hpp"
66
#include "algebra.hpp"
7+
#include <math.h>
78

89
using namespace algebra;
910

@@ -17,7 +18,8 @@ double NewtonRaphson::solve(double xin)
1718
{
1819
double x = xin;
1920
double delta_x = equation(x) / derivative(x);
20-
while (abs(delta_x) >= tolerance)
21+
22+
while (fabs(delta_x) >= tolerance)
2123
{
2224
delta_x = equation(x) / derivative(x);
2325

0 commit comments

Comments
 (0)