性能是系统的1倍,误差大概2%,可斟酌使用。
[StructLayout(LayoutKind.Explicit)]
public struct C1
{
[FieldOffset(0)]
public int _int;
[FieldOffset(0)]
public float _float;
}
[StructLayout(LayoutKind.Explicit)]
public struct C2
{
[FieldOffset(0)]
public int _int;
[FieldOffset(0)]
public float _float;
}
static float Sqrt(float x)
{
C1 c1 = new C1();
C2 c2 = new C2();
c1._float = x;
c2._float = x;
c1._int = 0x1FBCF800 + (c1._int >> 1);
c2._int = 0x5f3759df - (c2._int >> 1);
return 0.5f * (c1._float + (x * c2._float));
}