Library for strict floating point Math in .NET

前端 未结 2 1951
刺人心
刺人心 2021-02-15 01:36

I have algorithm/computation in Java and unit test for it. The unit test expects result with some precision/delta. Now I ported the algo into .NET and would like to use same uni

2条回答
  •  情歌与酒
    2021-02-15 02:27

    I have extensively researched this issue earlier this year as I wanted to know if it was possible to base a multiplayer simulation on floating-point arithmetic in .NET. Some of my findings may be useful to you:

    It is possible to emulate "strict" mode by inserting redundant casts everywhere, but this appears to be a brittle, C#-specific and tedious solution.

    The 32-bit JIT emits x87 instructions, but the 64-bit JIT emits SSE instructions. This is true both on Microsoft and Mono's implementation. Unlike x87, SSE floating arithmetic is reproducible.

    I believe System.Math simply calls the equivalent C runtime functions, although I've been unable to step into the assembly to verify this (if someone knows how to do this, please check!). The C runtime uses SSE versions of transcendental functions when possible, except in a few cases, notably sqrt (but writing a wrapper via instrinsics for that is trivial). By the very nature of SSE these must be reproducible. It is programatically possible to determine whether the C runtime is using its SSE implementation rather than x87.

    For the remaining transcendental functions not available in SSE (fmod, sinh, cosh, tanh), it might be that they do not cause reproducibility issues if no further operation is performed on x87 with their result.

    So in short, sticking with 64-bit CLR should solve the problem for arithmetic; for transcendental functions, most are implemented in SSE already and I'm not even sure that that's necessary if you don't perform any x87 arithmetic with the results.

提交回复
热议问题