DSP

【黑科技】快速开方

2019-07-13 18:24发布

class="markdown_views prism-tomorrow-night"> 一个很玄学的代码片。。
实测比cmath里面的sqrt要快 float qsqrt(float x) { if(x == 0) return 0; float result = x; float xhalf = 0.5f*result; int i = *(int*)&result; i = 0x5f375a86- (i>>1); result = *(float*)&i; result = result*(1.5f-xhalf*result*result); result = result*(1.5f-xhalf*result*result); return 1.0f/result; }