constexpr 里的按位强转
  • 板块灌水区
  • 楼主蒟酱厂妹
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/11/9 12:54
  • 上次更新2023/10/27 03:39:07
查看原帖
constexpr 里的按位强转
310818
蒟酱厂妹楼主2022/11/9 12:54

如题,想用 constexprO(1)\mathcal O(1)sqrt,代码里要 int 字节上强转 float,运用指针没办法,改用 reinterpret_cast 还是 CE,有无解决方案(不要说这个是没有用的花活,但是我就是要用,你可以当这个是个语法求助?)

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);
2022/11/9 12:54
加载中...