Unity Mono和.Net平台浮点算法的区别
static void TestFloat(){{//float speed=2.0f/20;float speed = 0.1f;float distance = 2.0f;long needTime = (long)(distance / speed);Log.Debug($"needTime={needTime}");
#if UNITY_EDITORif (needTime != 19)
#elseif (needTime != 20)//.Net服务器和安卓手机
#endifLog.Warning("平台浮点算法变了");}}static void TestFixInt(){{FixInt fi2 = 20000;FixInt fi1 = 10000;if (fi1 / fi2 != 0.5f){Log.Warning($"TestFixInt,{fi2}/{fi1} != 0.5");}}{FixInt fi2 = 20000;FixInt fi1 = 10000;if (fi2 / fi1 != 2){Log.Warning($"TestFixInt,{fi2}/{fi1} != 2");}}{const long l = 20000;FixInt fi = new(6000);if (l < fi){Log.Warning($"TestFixInt,{l} < {fi}");}}{FixInt fi3 = new(3);FixInt fi5 = new(5);if (fi3 + 1000 < fi5){Log.Warning($"TestFixInt,{fi3} , {fi5}");}}{const long l = 3000;FixInt fi = l;if (fi.RawLong != l){Log.Warning($"TestFixInt,{fi.RawLong} != {l}");}}}static void TestGetPolygonFormation(){var points = TeamFormationComponentSystem.GetPolygonFormation(new(),new() { x = 230.33540344238281f, y = -0.0000019073486328125f, z = 62.280677795410156f },new() { x = -6.1755828857421875f, y = -0.045724689960479736f, z = 9.5355415344238281f },4,2);List<float3> list = new(){new(){x = 230.429f, y = 0.0f, z = 62.135f },new(){ x = 233.077f, y = 0.0f, z = 60.689f },new(){ x = 233.317f, y = 0.0f, z = 57.679f },new(){ x = 230.669f, y = 0.0f, z = 59.125f },};if (points.Count != list.Count){Log.Warning($"TestGetPolygonFormation,{points} != {list}");}for (int i = 0; i <points.Count; ++i){var cal = points[i];var want = list[i];if (!cal.Equals(want)){Log.Warning($"TestGetPolygonFormation,{cal} != {want}");}}}static void TestGetPointOnEllipse(){var radius = 2.82842707633972f;var intervalAngle = 90;var radian = -32.9286422729492f + (intervalAngle * 3);var dChangZhouAngle = -45f;var xShaft = 0.5f * radius;var yShaft = 1f * radius;var ptCenter = new float2(231.872909545898f, 59.9066619873047f);var cal = VectorHelper.GetPointOnEllipse(ptCenter,xShaft,yShaft,radian,dChangZhouAngle); //算出旋转后的向量float3 want = new() { x = 230.669f, y = 0.0f, z = 59.125f };if (!cal.Equals(want)){Log.Warning($"TestGetPointOnEllipse,{cal} != {want}");}}static void TestMath(){var radius = 2.82842707633972f;var intervalAngle = 90;var radian = -32.9286422729492f + (intervalAngle * 3);var dChangZhouAngle = -45f;var xShaft = 0.5f * radius;var yShaft = 1f * radius;var ptCenter = new float2(231.872909545898f, 59.9066619873047f);radian -= dChangZhouAngle;dChangZhouAngle *= VectorHelper.Mathf.Deg2Rad;radian *= VectorHelper.Mathf.Deg2Rad;float dLiXin = math.atan2(yShaft * math.sin(radian), xShaft * math.cos(radian)); //离心角if (dLiXin != -1.46427190303802f){Log.Warning($"TestMath,{dLiXin} ");}if (yShaft != 2.8284270763397221f){Log.Warning($"TestMath,{yShaft} ");}if (dLiXin != -1.4642719030380249f){Log.Warning($"TestMath,{dLiXin} ");}if (dChangZhouAngle != -0.78539818525314298f){Log.Warning($"TestMath,{dChangZhouAngle} ");}if (xShaft != 1.4142135381698611f){Log.Warning($"TestMath,{xShaft} ");}var cosLiXin = math.cos(dLiXin);var sinChangZhouAngle = math.sin(dChangZhouAngle);var sinLiXin = math.sin(dLiXin);var cosChangZhouAngle = math.cos(dChangZhouAngle);if (cosLiXin != 0.10632307827472701f){Log.Warning($"TestMath,{xShaft} ");}if (sinChangZhouAngle != -0.70710676908492998f){Log.Warning($"TestMath,{xShaft} ");}if (sinLiXin != -0.99433165788650502f){Log.Warning($"TestMath,{xShaft} ");}if (cosChangZhouAngle != 0.70710676908492998f){Log.Warning($"TestMath,{xShaft} ");}var x = yShaft * cosLiXin * sinChangZhouAngle + xShaft * sinLiXin * cosChangZhouAngle + ptCenter.x;
#if UNITY_EDITORif (x != 230.665939331055f)
#elseif (x != 230.66592407226563f)
#endif{Log.Warning($"平台浮点加法或乘法算法变了,{x} ");}float y = yShaft * math.cos(dLiXin) * math.cos(dChangZhouAngle) - xShaft * math.sin(dLiXin) * math.sin(dChangZhouAngle) + ptCenter.y;}
结论
.Net和安卓手机IL2CPP算法相同
Windows下Unity的Mono算法不同,就它不同