This is actually Nguyen Truong Chung.
Thanks to everyone for the answers. I really appreciate your answers, which enlightened me as a way to continue debugging! At the moment, I somehow figured out the reason for desync, but without specific solutions. I want to give you the information I received, and I hope I can get some more photos.
1. Conclusion:
I have this function that uses cos. I printed the log as follows:
void rotateZ (angle of the float)
{
if( angle ) { const float sinTheta = sin( angle ); const float cosTheta = cos( angle ); // I logged here myLog( "Vector3D::SelfRotateZ(%x) %x, %x", *(unsigned int*)&angle, *(unsigned int*)&cosTheta, *(unsigned int*)&sinTheta ); .... }
}
Desync happened as follows:
On iPad4: Vector3D :: SelfRotateZ (404800d2) bf7ff708, 3c8782bc On iPhone4: Vector3D :: SelfRotateZ (404800d2) bf7ff709, 3c8782bc
2. Retesting:
And the story does not stop here, because:
- I tried this line of code at the beginning of the game:
{unsigned int zz = 0x404800d2;
float yy = 0; memcpy( &yy, &zz, 4 ); const float temp1 = cos( yy ); printf( "%x\n", *(unsigned int*)&temp1;
}
I ran the code above on the same iPhone4 and guessed what? I got this: bf7ff708
I put this code in the game update cycle, and the result was still bf7ff708 in each cycle.
What else? The value 0x404800d2 is the initialized value of the game, so every time the game starts, the two lines of esync above are always present there.
3: Poll:
So, I decided to forget what happened above, and temporarily replaced sin, cos function with simple Taylor implementations that I found on dreamcode.net. Desync is no longer happening.
It seems that the cos function is not deterministic even on one iPhone 4 (OS version 5).
My question is: do we have an explanation why the cos function returns a different result for the same input on the same phone? Here I have the input 0x404800d2 and two different outputs: bf7ff708 and bf7ff709. However, I cannot reproduce the result of bf7ff709 by simple coding.
It seems to me that I need the source code of the mathematical functions of the OS (floating point version) in order to clearly understand this. Is the above problem that I found enough as an error report?