On Mon, Sep 20, 2010 at 05:52:06PM -0400, bearophile wrote: > Recently Bradley Mitchell in D.learn newsgroup has tried to implement the > Quake fast inverse square root algorithm in D, and has found D lack the C++ > reinterpret cast: > http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=21901
Did anyone suggest porting the C++ line for line? int xi = *reinterpret_cast<int *>( &x ); and int xi = cast(int)cast(void*)x; aren't the same thing! Maybe he tried this, but webnews is crap and won't load up the rest of that thread. It seems like int xi = * cast(int*) (&x); should do the trick. And same for the cast back to float. So the final program is: ============ import std.stdio; float fastInvSqrt( float x ) { const int INV_SQRT_N = 1597292357; const float MULT = 1.000363245811462f; float mx = 0.5f * MULT * x; int xi = * cast(int*) (&x); /// note this line! xi = INV_SQRT_N - (xi >> 1); x = xi; x = * cast(float*) (&xi); /// this one too! return x * (1.5f * MULT - mx * x * x); } void main(string[] args) { float a = fastInvSqrt( 9.0f ); writefln("%f", a); } =========== Running it gives the same result as the C++ version on my box. This looks like human error in the porting to me.