How deterministic is floating point inaccuracy?

后端 未结 10 1458
长发绾君心
长发绾君心 2020-11-27 06:51

I understand that floating point calculations have accuracy issues and there are plenty of questions explaining why. My question is if I run the same calculation twice, can

10条回答
  •  眼角桃花
    2020-11-27 07:03

    Since your question is tagged C#, it's worth emphasising the issues faced on .NET:

    1. Floating point maths is not associative - that is, (a + b) + c is not guaranteed to equal a + (b + c);
    2. Different compilers will optimize your code in different ways, and that may involve re-ordering arithmetic operations.
    3. In .NET the CLR's JIT compiler will compile your code on the fly, so compilation is dependent upon the version of the .NET on the machine at runtime.

    This means, that you shouldn't rely upon your .NET application producing the same floating point calculation results when run on different versions of the .NET CLR.

    For example, in your case, if you record the initial state and inputs to your simulation, then install a service pack that updates the CLR, your simulation may not replay identically the next time you run it.

    See Shawn Hargreaves's blog post Is floating point math deterministic? for further discussion relevant to .NET.

提交回复
热议问题