currently I am trying to print out the answer to an equation using CUDA.
This equation is (x+y)^2 / xy
An example of the outputs I am getting are in this image attached.
__global__ void proof() {
int x = 1;
int y = 1;
int multi_number = 1000;
while (true) {
long eq = ((pow(x + y, 2)) / (x * y));
if (y >= multi_number) {
if (x >= multi_number) {
printf("\nProof is true for all cases.");
break;
}
}
if (x >= multi_number) {
x = 1;
y = y + 1;
}
printf("\nEquation being used: (%d", x);
printf("+%d", y);
printf(")^2 / %d", x);
printf("*%d", y);
printf(" >= 4");
printf("\n%d", eq); // printing the equations answer
if (eq < 4) {
printf("\nProof Failed: %d", x);
printf(", %d", y);
break;
}
x = x + 1;
}
}
I have currently tried rewriting the equation in multiple different ways, this did not work.
For the failed test (55+55)^2 / 55*55 I was expecting 4 to be printed instead of 3.
An example of a correct answer would be (1+1)^2 / 1*1 = 4
>Solution :
In a nutshell, pow() (in CUDA device code, at least) does not have the accuracy you need/desire when using truncation. I just answered a very similar question here.
The reason for the failure is that the result of ((pow(x + y, 2)) / (x * y)) is not 110 like you would expect, it is 109, when converted to a long value via truncation.
According to my testing, you can work around this by changing this line:
long eq = ((pow(x + y, 2)) / (x * y));
to this:
long eq = ((x+y)*(x+y)) / (x * y);