Consider the following (mocked-up) values:
int x = FP(20.5);
int y = FP(12.5);
Now, suppose these represented a 2d vector. I would find the length of the vector using Pythag's theoreom:
len = sqrt(x^2 + y^2);
Hence:
float xx = ( ((float)FPMUL(x, x) - 0.5f) / FP_ONE );
float yy = ( ((float)FPMUL(y, y) - 0.5f) / FP_ONE );
int length = FP(sqrt(xx + yy));
By my reckoning, 'length' should represent the length of the vector. But... according to my tests, it doesn't. More often than not, the result is 0, or a power of 2.
I for the life of me can't work out what's going on, I think it's one of those bugs that it so glaringly obvious you don't see it.
Can anyone see anything wrong? If not, I'll assume I need to do more digging in my code... but it's REALLY confused me. I won't post the code - I'm certain enough that the code above is the problem.
int x = FP(20.5);
int y = FP(12.5);
Now, suppose these represented a 2d vector. I would find the length of the vector using Pythag's theoreom:
len = sqrt(x^2 + y^2);
Hence:
float xx = ( ((float)FPMUL(x, x) - 0.5f) / FP_ONE );
float yy = ( ((float)FPMUL(y, y) - 0.5f) / FP_ONE );
int length = FP(sqrt(xx + yy));
By my reckoning, 'length' should represent the length of the vector. But... according to my tests, it doesn't. More often than not, the result is 0, or a power of 2.
I for the life of me can't work out what's going on, I think it's one of those bugs that it so glaringly obvious you don't see it.
Can anyone see anything wrong? If not, I'll assume I need to do more digging in my code... but it's REALLY confused me. I won't post the code - I'm certain enough that the code above is the problem.