Unexpected Value for the Calculation of Catalan's Constant - c++

Here's the function:
arbFloat catalan(){
arbFloat output, n = 0, first, second;
std::string preComp, postComp;
do{
preComp = resizeArbtoString(first);
first += (1.0 / pow(2.0, 4.0 * n)) * (
(- 1.0 / (2.0 * pow(8.0*n + 2.0, 2.0))) +
(1.0 / (4.0 * pow(8.0*n + 3.0, 2.0))) +
(- 1.0 / (8.0 * pow(8.0*n + 5.0, 2.0))) +
(1.0 / (8.0 * pow(8.0*n + 6.0, 2.0))) +
(-1.0 / (16.0 * pow(8.0*n + 7.0, 2.0))) +
(1.0 / (2.0 * pow(8.0*n + 1.0, 2.0)))
);
postComp = resizeArbtoString(first);
n++;
} while(preComp != postComp);
n = 0;
do{
preComp = resizeArbtoString(second);
second += (1.0 / pow(2.0 , 12.0 * n)) * (
(1.0 / (16.0 * pow(8.0*n + 2, 2.0))) +
(1.0 / (64.0 * pow(8.0*n + 3, 2.0))) +
(-1.0 / (512.0 * pow(8.0*n + 5, 2.0))) +
(-1.0 / (1024.0 * pow(8.0*n + 6, 2.0))) +
(1.0 / (4096.0 * pow(8.0*n + 7, 2.0))) +
(1.0 / (8.0 * pow(8.0*n + 1, 2.0)))
);
postComp = resizeArbtoString(second);
n++;
} while(preComp != postComp);
output = (3.0*first) - (2.0*second);
return output;
}
This code is meant to calculate the value of Catalan's constant. I am using the method that quickly converges, compared to the slowly-converging equation.
arbFloat is a Boost multi-precision cpp_dec_float with the precision of 100 decimal places:
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>> arbFloat
The function resizeArbtoString() simply converts the variable to a string, and resizes it to account for the first decimal and decimal mark. It isn't important here, and I have verified that it does not impact the result.
I get the value of first before and after calculations to stop an infinite loop when the precision is bypassed (same for the variable second). The do-while loop checks this. This is what makes it an iterative method.
The two do-while loops separately get the values of the two summations in the equation (image below). Then the output variable multiplies the first summation by 3, and the second summation by -2, then adds them together.
This function is returning the value of:
0.9159456632705320620288006061761625716626752650763000222738355046579667073981960968090933049341304381
When the value should instead be:
.915965594177219015054603514932384110774149374281672134266498119621763019776254769479356512926115106248574
(correct constant value via OEIS)
Here is the equation that I referenced when creating this function:
Why is this returning an imprecise value?

Your 1.0 / 4096.0 term in second should be -1.0 / 4096.0.

Related

How is it possible that L1/TEX Hit Rate be 0% while L2 Hit Rate is above 80%?

I have a tiled dense matrix multiplication code, AxB=C, where the values of A are hard-coded in the code. This code goes over elements of A and B and calculates C like the below:
__forceinline__ __device__ void minimalExample(float* B, float *C_local) {
C_local[0] += 4.0f * B[threadIdx.x];
C_local[0] += 9.0f * B[1024 + threadIdx.x];
C_local[1] += 5.0f * B[threadIdx.x];
C_local[1] += 9.0f * B[1024 + threadIdx.x];
C_local[2] += 7.0f * B[threadIdx.x];
C_local[2] += 3.0f * B[1024 + threadIdx.x];
C_local[3] += 3.0f * B[threadIdx.x];
C_local[3] += 1.0f * B[1024 + threadIdx.x];
C_local[4] += 7.0f * B[threadIdx.x];
C_local[4] += 8.0f * B[1024 + threadIdx.x];
C_local[5] += 4.0f * B[threadIdx.x];
C_local[5] += 5.0f * B[1024 + threadIdx.x];
C_local[6] += 9.0f * B[threadIdx.x];
C_local[6] += 7.0f * B[1024 + threadIdx.x];
C_local[7] += 9.0f * B[threadIdx.x];
C_local[7] += 2.0f * B[1024 + threadIdx.x];
}
__global__ void matrixMultiplication(float *B, float *C)
{
float C_local[32] = {0};
minimalExample(B, C_local);
float *C_tile = C;
C_tile[0 + threadIdx.x] = C_local[0];
C_tile[1024 + threadIdx.x] = C_local[1];
C_tile[2048 + threadIdx.x] = C_local[2];
C_tile[3072 + threadIdx.x] = C_local[3];
C_tile[4096 + threadIdx.x] = C_local[4];
C_tile[5120 + threadIdx.x] = C_local[5];
C_tile[6144 + threadIdx.x] = C_local[6];
C_tile[7168 + threadIdx.x] = C_local[7];
}
where A is MxK, B is KxN, and C is MxN. M = 32, K = 32, and N = 128 are the dimensions of the involved matrices, and k = 2 is the tile size along K in the matrix multiplication.
For the sake of a minimal example, I have initialized the data in A only partially, a block of 8x8 in the top left corner, and B and C are dense 1024x1024 matrices. When I run the code and profile it, it shows that L1/TEX Hit Rate is 0%, but L2 Hit Rate is 94%.
Each row in B is used multiple times, and I think the local values in C should be kept in registers.
It is also notable that I use cudaMalloc() to initialize all elements of B (1024x1024).
I'm running my code on RTX 3080 TI with compute capability 8.6, my driver version is 525, and my cuda version is 12.0.
Thanks for your help.

How do I generate Bezier Curves and NURBS in C++ and import it as an igs?

I am new to C++ NURBS libary. I learnt generating line (by CLine, from nurbs.h ) and save it as igs. But in case of
multiple control points, how to generate a curve ? Every other tutorial using graphics.h
(putpixel), but couldnt find anything about igs.
This should be a simple problem. But I have no idea which function can help me here.
Thanks in advance.
We have 4 control points here to begin with.
for (float t = 0.0; t <= 1.0; t += 0.2) {
double xt = 0.0, yt = 0.0;
xt = pow(1 - t, 3) * x[0] + 3 * t * pow(1 - t, 2) * x[1] + 3 * pow(t, 2) * (1 - t) * x[2]
+ pow(t, 3) * x[3];
yt = pow(1 - t, 3) * y[0] + 3 * t * pow(1 - t, 2) * y[1] + 3 * pow(t, 2) * (1 - t) * y[2]
+ pow(t, 3) * y[3];
count = count + 1;
//Math::Vector4f c(xt, yt, 0);
for (int i = 1; i < 3; i++) {
listt[i][0]= xt;
listt[i][1]= yt;
Math::Vector4f a(listt[i][0], listt[i][1],0);
myvector.push_back (&a);
}
}
......
.....
igs.Write("test.igs");
--- This is to create the points, but after that I dont know how to use the points to create a Bezier curve .

C++: Get value of variable after performing divide and assignment operator:

I am trying to get a value of a variable "pesoil" before its converted to to mm month^-1
pesoil = beta * rnsoil + CPAIR * RHOAIR * exp_h * vpdo / r_as;
pesoil /= (beta + PSY * exp_h * (1.0 + r_ss / r_as))); // W m^-2
pesoil *= (etimes * ndays[dm]) / LAMBDA; // convert to mm month^-1
can I do it like this? or will this mess up value stored in "pesoil"
epotential_W = (pesoil /= (beta + PSY * exp_h * (1.0 + r_ss / r_as))); // W m^-2

Argmax function in C++?

I am trying to make armgax function in C++
For example,
C = wage * hour * shock + t + a * R + (1 - delta) * d - d_f - a_f - fx1 * (1 - delta) * d;
double result;
result = (1 / (1 - sig)) * pow(pow(C, psi) * pow(d, 1 - psi), (1 - sig));
Suppose every variable except for 'd_f' and 'a_f' are given and both 'd_f' and 'a_f' some number(double) in [0, 24].
If I want to get the combination of 'd_f' and 'a_f' that maximizes 'result', is there a proper function that I can use?
Thanks.

Complex Number Division class

I'm trying to overload the operator to divide two complex numbers
Testing with 3+2i / 4 - 3i
complex g(3, 2);
complex f(4,-3);
cout << g / f << endl;
I added * -1.0 since we go
(4*4) + (3 * -3)i^2 in math which is 25
((3*4) + (3 * -3)* -1) is my intent
Testing I get -0.545455 - 1.72727i
While before I added the * 1.0 I got
0.24 +0.76i
Which is was very close to
0.24 + 0.68i
the answer
complex complex :: operator/ (complex& x) {
complex conjugate = x.conj();
double j = (real * conjugate.real) + (imag * conjugate.imag); // real
double u = (real * conjugate.imag) + (imag * conjugate.real); // imag
double h = (((conjugate.imag * imag)* -1.0) + (real * conjugate.real)) + ( (real*conjugate.imag) + (imag * conjugate.real));
return complex(j/h,u/h);
}
This is wrong:
double j = (real * conjugate.real) + (imag * conjugate.imag); // real
should be -, not +.
This is right:
double u = (real * conjugate.imag) + (imag * conjugate.real); // imag
Although both j and u are meaninglessly named.
What's going on here?
double h = (((conjugate.imag * imag)* -1.0) + (real * conjugate.real)) + ( (real*conjugate.imag) + (imag * conjugate.real));
The denominator is just x*conjugate which is:
double denom = x.real * x.real + x.imag * x.imag;
Side-note, you want to take your argument by reference to const.