我知道浮点精度在常规情况下是如何工作的,但我偶然发现了我的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)