#include #include struct valores{ float termo; // i-esimo termo double invsqrt; // valor calculado via InvSqrt double unionSqrt; // valor calculado via Union InvSqrt double delta; // abs(invsqrt - unionSqrt), erro entre a aproximacao e o valor real } typedef Valores; const int QUANTIDADETESTADA = 100000; union casting { int i; float f; }; float InvSqrt (float x) { union casting cast; float xhalf = 0.5f*x; cast.f = x; // int i = *(int*)&x; cast.i = 0x5f3759df - ((cast.i)>>1); // x = *(float*)&i; return cast.f*(1.5f - xhalf*cast.f*cast.f); // x = x*(1.5f - xhalf*x*x); } float _InvSqrt (float x) { float xhalf = 0.5f*x; int i = *(int*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; } int main(void){ Valores vars[QUANTIDADETESTADA]; float termos = 0.f; for(int i=0; i