- Code: Select all
`# constants:`

global infinity; infinity = float('inf')

global minus_infinity; minus_infinity = float('-inf')

global min_float; min_float = 1.1754943508222875e-38 # 0x00800000

global max_float; max_float = 3.4028234663852886e+38 # 0x7F7FFFFF

from math import sqrt

def InverseSqrt(x): # C4 game engine:

if (x < min_float): return infinity

r = sqrt(x) # __frsqrte(x)

r = 0.5 * r * (3.0 - x * r * r)

r = 0.5 * r * (3.0 - x * r * r)

return r

from ctypes import cast, pointer, c_int, POINTER, c_float

def InverseSqrt2(x): # http://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/

xhalf = 0.5*x

i = cast(pointer(c_float(x)), POINTER(c_int)).contents.value

i = 0x5f3759d5 - (i >> 1)

x = cast(pointer(c_int(i)), POINTER(c_float)).contents.value

x = x*(1.5 - xhalf*x*x)

return x

after the reference in the code, I found this:

http://stackoverflow.com/questions/1778 ... n-modern-c

I just need some comparisons to help me understand...

here's the C++ representation for the C4 engine:

- Code: Select all
`const float K::infinity = *(float *) &hex_7F800000;`

const float K::min_float = *(float *) &hex_00800000;

float C4::InverseSqrt(float x)

{

if (x < K::min_float)

{

return (K::infinity);

}

float r = __frsqrte(x);

r = 0.5F * r * (3.0F - x * r * r);

r = 0.5F * r * (3.0F - x * r * r);

return (r);

}

I just want to know if my translation is working right, and so far, both return different results :/

I'm not sure if __frsqrte() is defined locally or globally... >_>

(I know 50x more about Python than I do C++ sooo...)