Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ The implementation of the algorithm would look like
// this C++ code snippet is later referred to as <<algorithm>>
#include "newtonraphson.hpp"
#include "algebra.hpp"
#include <math.h>

using namespace algebra;

Expand All @@ -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);

Expand All @@ -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<bits/stdc++.h>
#include <iomanip>

<<algorithm>>

Expand All @@ -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;
}
```
Expand All @@ -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.
Expand Down Expand Up @@ -267,7 +273,7 @@ Content-type: application/json

{
"guess": -20.0,
"root": -1.622923986083026
"root": -1.0000001181322415
}
```

Expand Down Expand Up @@ -307,7 +313,7 @@ Should return the following JSON document as a response
```json
{
"guess": -20,
"root":-1.62292
"root":-1.0000001181322415
}
```

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/cgi-newtonraphson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// this C++ code snippet is later referred to as <<algorithm>>
#include "newtonraphson.hpp"
#include "algebra.hpp"
#include <math.h>

using namespace algebra;

Expand All @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion src/cli-newtonraphson.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// this C++ snippet is stored as src/newtonraphson.cpp
#include<bits/stdc++.h>
#include <iomanip>

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

using namespace algebra;

Expand All @@ -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);

Expand All @@ -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;
}
Binary file modified src/js/newtonraphsonwasm.wasm
Binary file not shown.
4 changes: 3 additions & 1 deletion src/py-newtonraphson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// this C++ code snippet is later referred to as <<algorithm>>
#include "newtonraphson.hpp"
#include "algebra.hpp"
#include <math.h>

using namespace algebra;

Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/py/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

finder = NewtonRaphson(epsilon=0.001)
root = finder.solve(guess=-20)
print(root)
print ("{0:.6f}".format(root))
4 changes: 3 additions & 1 deletion src/wasm-newtonraphson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// this C++ code snippet is later referred to as <<algorithm>>
#include "newtonraphson.hpp"
#include "algebra.hpp"
#include <math.h>

using namespace algebra;

Expand All @@ -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);

Expand Down