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_EDITOR
                if (needTime != 19)
#else
                if (needTime != 20)//.Net服务器和安卓手机
#endif
                    Log.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_EDITOR
            if (x != 230.665939331055f)
#else
            if (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算法不同,就它不同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值