0

For my deterministic physics engine, I need to confirm that calculations with doubles in C# are consistent enough across multiple platforms. Does anyone know how much the following functions differ in results? On my computer as a Windows 32 bit application, these are the results (Note: Pseudo-code):

double x = 123.123;
x * 2 = 246.246
Math.Pow (x, 2) = 15159.273129
Math.Sqrt (x) = 11.0960803890383
Math.Sin (x) = -0.56537391969734

I still don't know if the same results will turn up on other platforms or other languages (I'm using C#). If there's any more information needed, I'll supply it as promptly as possible. Please, if there's anyone with the knowledge or time to help test the calculations on other platforms, help me get past this uncertainty.

JPtheK9
  • 275
  • 2
  • 12
  • This is not an exact duplicate but you may get some insight from the question and answers in [Cross Platform Floating Point Consistancy](http://stackoverflow.com/questions/20963419/cross-platform-floating-point-consistancy). – Simon Feb 10 '15 at 02:57
  • http://stackoverflow.com/questions/28389634/is-double-math-deterministic-enough-for-physics-simulations http://stackoverflow.com/questions/7365790/how-can-floating-point-calculations-be-made-deterministic http://stackoverflow.com/questions/20963419/cross-platform-floating-point-consistancy – phuclv Feb 10 '15 at 03:36
  • Oh, thank you. I'll check those links out. – JPtheK9 Feb 10 '15 at 05:12

1 Answers1

0

The C# standard, ECMA-334, says in section 11.1.6 Floating point types "Floating-point operations can be performed with higher precision than the result type of the operation.".

Given that sort of rule, you cannot be sure of getting exactly the same answer everywhere. "can" means some implementations may use higher precision than others, for the same calculation.

Even if testing shows that all current implementations get the same answers for all your calculations across a range of inputs, the next compiler release on any platform might change that. If you really need identical results, you need to pick a library or similar that promises identical results.

Calculations vary in how sensitive they are to small changes in inputs. At the extreme are simulations of some physical systems such as weather, where tiny changes in inputs can expand to big changes in results.

One way to get a feeling for the behavior of your simulation is to deliberately perturb some values by e.g. one part in 1e15. Does that change the results enough to matter? However, there will always be a risk that some inputs will lead to less stable conditions than others.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • Oh, thanks for the info. I only need accuracy enough as for the differences to not be noticeable though. What's the maximum the result of a calculation can differ? I only need it to be accurate enough for a video game. – JPtheK9 Feb 10 '15 at 03:13
  • That is going to be extremely dependent on what calculations you are doing. – Patricia Shanahan Feb 10 '15 at 03:16
  • Do you think it will be alright for the calculations listed above? I've estimated about 7,200,000 of each in a single game. – JPtheK9 Feb 10 '15 at 04:07
  • It is not a matter of the individual calculations, but of the overall effect of the program and the nature of the algorithms. You can get into trouble with nothing but addition and subtraction. – Patricia Shanahan Feb 10 '15 at 04:09
  • I know I should probably choose a more reliable method but if I could, I'd rather use the simplest way. Do you know think I can round to maybe the 3rd decimal digit and forget about the accuracy? If not, do you have any recommendations for more deterministic math? Thanks a lot for the help so far. – JPtheK9 Feb 10 '15 at 04:21
  • I think the advice in my answer about testing the effects of perturbations is the best I can do. I am not a C# expert so I don't know what packages are available. Java in strictfp mode and using the StringMath library would do the job. – Patricia Shanahan Feb 10 '15 at 04:25
  • I think I'll use some heavy perturbation (though to be honest, I'm not sure exactly what that means). Thanks a lot for the information and ideas. I'll implement the rounding and post a follow-up on the results. – JPtheK9 Feb 10 '15 at 06:20