ama*_*ara 1 c java translation
从这篇文章.这是代码:
float InvSqrt(float x){ // line 0
float xhalf = 0.5f * x;
int i = *(int*)&x; // store floating-point bits in integer
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method
x = *(float*)&i; // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
return x;
}
Run Code Online (Sandbox Code Playgroud)
......我甚至不知道那是C还是C++.[好吧显然它是C,谢谢]有人可以为我翻译成Java吗?它(只是,我希望)第2和第4行令我感到困惑.
pol*_*nts 10
您想要使用这些方法:
并且可能存在问题strictfp等.
它大致是这样的:(注意:这是未经测试的!)
float InvSqrt(float x){ // line 0
float xhalf = 0.5f * x;
int i = Float.floatToIntBits(x); // store floating-point bits in integer
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method
x = Float.intBitsToFloat(i); // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
return x;
}
Run Code Online (Sandbox Code Playgroud)
这些线用于之间进行转换float,并int作为位模式.Java有静态方法java.lang.Float- 其他一切都是相同的.
static float InvSqrt(float x) { // line 0
float xhalf = 0.5f * x;
int i = Float.floatToIntBits(x); // store floating-point bits in integer
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method
x = Float.intBitsToFloat(i); // convert new bits into float
x = x * (1.5f - xhalf * x * x); // One round of Newton's method
return x;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
601 次 |
| 最近记录: |