it is actually Nguyen Truong Chung again.
Thank you all very much for your answers so far. I really appreciate your answers, which enlightened me the path to continue debugging! At the moment, I somehow found out the reason of the desync, but without concrete solutions yet. I wish to provide you with information I got, and hopefully I can get some more in-sights.
1. Finding:
I have this function that use cos. I printed the log like this:
void rotateZ( float angle )
{
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 like this:
On iPad4: Vector3D::SelfRotateZ(404800d2) bf7ff708, 3c8782bc
On iPhone4: Vector3D::SelfRotateZ(404800d2) bf7ff709, 3c8782bc
2. Re-testing:
And the story does not stop here because:
- I tried these 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 guess what? I got this: bf7ff708
I put that code in the update loop of the game and the result I got was still bf7ff708 at every loop.
What is more? The value 0x404800d2 is an initialize value of the game, so every time the game starts, the two desync lines above are always present there.
3: The questioning:
So, I decided to forget what happened above, and temporarily replaced sin, cos function with simple Taylor implementations I found on dreamcode.net. The desync no longer happened.
It seems that the cos function is not even deterministic on the same iPhone 4 (OS version 5).
My question is: Do we have an explanation why cos function returns different result for the same input on a same phone? Here I have the input 0x404800d2, and two different outputs: bf7ff708 and bf7ff709. However, I cannot reproduce the result bf7ff709 by simply coding.
I guess I need the source code of math functions of the OS (floating-point version) in order to understand this clearly. Is above problem I found enough as a bug report?