如题,想用 constexpr 里 O(1)\mathcal O(1)O(1) 的 sqrt,代码里要 int 字节上强转 float,运用指针没办法,改用 reinterpret_cast 还是 CE,有无解决方案(不要说这个是没有用的花活,但是我就是要用,你可以当这个是个语法求助?)
constexpr
sqrt
int
float
reinterpret_cast
constexpr int InvSqrt(float x) { float xhalf = 0.5f*x; int i = *(int*)&x; // get bits for floating VALUE // int i=reinterpret_cast<int>(x); // i = 0x5f375a86- (i>>1); // gives initial guess y0 // x = *(float*)&i; // convert bits BACK to float // x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy return x; } constexpr int x=InvSqrt(1210);