相关疑难解决方法(0)

为什么在由parantheses分隔和由语句分隔时,C#中的浮点精度会有所不同?

我知道浮点精度在常规情况下是如何工作的,但我偶然发现了我的C#代码中的奇怪情况.

为什么result1和result2在这里不是完全相同的浮点值?


const float A;   // Arbitrary value
const float B;   // Arbitrary value

float result1 = (A*B)*dt;

float result2 = (A*B); 
result2 *= dt;
Run Code Online (Sandbox Code Playgroud)

这个页面我认为浮动算术是左关联的,这意味着值以从左到右的方式进行评估和计算.

完整的源代码涉及XNA的Quaternions.我不认为我的常量是什么以及VectorHelper.AddPitchRollYaw()的作用.如果我以相同的方式计算三角形俯仰/滚转/偏航角度,测试通过就好了,但是由于代码低于它不通过:


X
  Expected: 0.275153548f
  But was:  0.275153786f
Run Code Online (Sandbox Code Playgroud)

[TestFixture]
    internal class QuaternionPrecisionTest
    {
        [Test]
        public void Test()
        {
            JoystickInput input;
            input.Pitch = 0.312312432f;
            input.Roll = 0.512312432f;
            input.Yaw = 0.912312432f;
            const float dt = 0.017001f;

            float pitchRate = input.Pitch * PhysicsConstants.MaxPitchRate;
            float rollRate = input.Roll * PhysicsConstants.MaxRollRate;
            float yawRate = input.Yaw * PhysicsConstants.MaxYawRate;

            Quaternion …
Run Code Online (Sandbox Code Playgroud)

c# floating-point precision

5
推荐指数
2
解决办法
1508
查看次数

标签 统计

c# ×1

floating-point ×1

precision ×1