Debian Codegraph @

Help/guide/tutorial/Step5/MathFunctions/MathFunctions.cxx

raw
1 #include <cmath>
2 #include <format>
3
4 // TODO5: Replace <iostream> with <MathLogger.h>
5
6 #include <iostream>
7
8 namespace {
9
10 // TODO6: Instantiate a logger inside the anonymous namespace
11
12 // a hack square root calculation using simple operations
13 double mysqrt(double x)
14 {
15 if (x <= 0) {
16 return 0;
17 }
18
19 double result = x;
20
21 // do ten iterations
22 for (int i = 0; i < 10; ++i) {
23 if (result <= 0) {
24 result = 0.1;
25 }
26 double delta = x - (result * result);
27 result = result + 0.5 * delta / result;
28
29 // TODO7: Use the logger to log the message
30 std::cout << std::format("Computing sqrt of {} to be {}\n", x, result);
31 }
32 return result;
33 }
34 }
35
36 namespace mathfunctions {
37 double sqrt(double x)
38 {
39 #ifdef TUTORIAL_USE_STD_SQRT
40 return std::sqrt(x);
41 #else
42 return mysqrt(x);
43 #endif
44 }
45 }