@
lastchance Thank you.
Just to get a feel for how that might work, I tried this - though of course it is limited by the floating-point type I used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
typedef long double numeric;
numeric calc_digit(unsigned int k)
{
unsigned int k8 = 8 * k;
numeric a = 4.0 / (k8 + 1);
numeric b = 2.0 / (k8 + 4);
numeric c = 1.0 / (k8 + 5);
numeric d = 1.0 / (k8 + 6);
numeric sum = a - b - c - d;
for (unsigned i=0; i<k; ++i)
{
sum /= 16;
}
return sum;
}
int main()
{
cout << fixed << setprecision(20);
numeric total = 0.0;
const int terms = 15;
for (int k=0; k<terms; ++k)
{
numeric digit = calc_digit(k);
total += digit;
cout << setw(30) << digit
<< setw(30) << total << '\n';
}
cout << "\n\ntotal = " << total << '\n';
}
|
3.13333333333333333322 3.13333333333333333322
0.00808913308913308913 3.14142246642246642245
0.00016492392411510059 3.14158739034658152306
0.00000506722085385878 3.14159245756743538179
0.00000018789290093772 3.14159264546033631946
0.00000000776775121518 3.14159265322808753471
0.00000000034479329305 3.14159265357288082780
0.00000000001609187716 3.14159265358897270498
0.00000000000077957030 3.14159265358975227519
0.00000000000003887115 3.14159265358979114644
0.00000000000000198323 3.14159265358979312966
0.00000000000000010310 3.14159265358979323266
0.00000000000000000544 3.14159265358979323808
0.00000000000000000029 3.14159265358979323830
0.00000000000000000002 3.14159265358979323830
total = 3.14159265358979323830 |
@
CHancock I read (fairly quickly) through the code for your Fraction class. Most of it looks reasonable, apart from the
to_double()
function which does integer division, and though it returns a type
double
, the value it contains will always be an integer.
The comments are misleading. almost all the member functions are labelled as "constructor". There are just two constructors:
1 2 3
|
Fraction();
Fraction(long long _numberator, long long _denominator);
|
I did a quick stand-alone test of the class and noticed in one case it have a positive numerator and negative denominator. Not sure whether that matters, though I'd tend to have the denominator always positive. My test wasn't thorough, there may be other issues.
Also, it is not a good idea to put "
using namespace std;
" inside a header file. I would remove it and instead put
std::
in the one place where it is needed (std::string).