I have a weird behaviour in my kernel that I can’t understand and hope someone here can put some light on it for me =)
The basic problem is that a float number divided by the same float number does not give 1 as an answer. I’ll show the code part with some debugging code I’ve added.
[codebox]else if (codop == OP_DIV) {
op2 = stack[--stackPointer];
op1 = stack[--stackPointer];
if (op2 == 0.0) stack[stackPointer++] = 1.0;
else stack[stackPointer++] = op1/op2;//__fdividef(op1, op2);
//my debugging
if (debugProgram >= 0 && (*bar2).date == 20050510 && (*bar2).time >= 145500 && (*bar2).time < 145600) {
cuPrintf("Debugging inside : op1 %.8f , op2 , %.8f , kvot %.8f \n", op1, op2, stack[stackPointer-1]);
}[/codebox]
On this particular date and time 20050510 145500 op1 and op2 is the same, namely 4626.52783203. So I get the output line.
Debugging inside : op1 4626.52783203 , op2 , 4626.52783203 , kvot 0.99999994 . Before I used __fdividef(op1, op2) that is why it is commented out, I just wanted to try if I got another result if I just used / - operator but I get the same result whichever I use. In another part of the kernel I just hardcoded the following to test if I got the same weird result there:
[codebox] float aa = 4626.52783203f;
float bb = 4626.52783203f;
cuPrintf("divisionenkernel %.8f \n", aa/bb);[/codebox]
But there I get the output “divisionenkernel 1.00000000”.
Another weird thing, which I don’t know if it does matter, is that I read in this value at an earlier stage from a file and it only has 8 decimals in the file but if I print the value with
“cuPrintf(“Debugging inside : op1 %.25f , op2 , %.25f , kvot %.25f \n”, op1, op2, stack[stackPointer-1]);” I get the output
“Debugging inside : op1 4626.5278320312500000000000000 , op2 , 4626.5278320312500000000000000 , kvot 0.9999999403953552246093750”. I have no idea where the 125 in the end comes from.
If I use the long value at the other debugging place I still get 1 as a result,
[codebox] float aa = 4626.5278320312500000000000000f;
float bb = 4626.5278320312500000000000000f;
cuPrintf("divisionenkernel %.8f \n", aa/bb);[/codebox] still gives "divisionenkernel 1.00000000".
So in my opinion I am doing the exact same calculation at two different places in the kernel but I get two different results?
I’m totally confused here and would really appreciate any input. First I thought it was because of some floating precision issue but it seems weird that the same calculation gives different results.