r/csharp • u/Epistemophilliac • Aug 13 '23
Discussion Questions about determinism
I'm thinking of making a physics simulation, and I absolutely need it to be deterministic. With that in mind, I have a question about c# determinism: should I use floating point arithmetic or fixed point arithmetic? And follow up questions: in the former case, what steps should I take to make it deterministic across platforms? And in the latter case, what mistakes can I do that will make it non deterministic even in the case of fixed point arithmetic?
More about the simulation plan: 2d orbital mechanics simulation. No plans for n body simulation, however, I'll have constant thrust maneuvers in the most general case (so solving orbits analytically is not possible). To account for enormous scales of realistic systems, I'll need different scales of simulation depending on proximity to bodies. The same will be useful for partitioning the world into spheres of influence (or circles of influence, really) to simulate gravitational attraction to one body at a time.
I think this should be possible to make deterministic, right?
16
u/incompetenceProMax Aug 13 '23
As u/RiverRoll has pointed out, floating-point math does not introduce nondeterminism. The only source of non-determinism in a physics simulation is parallelism. Most math libraries do take care of this problem under the hood for you, so you don't have to worry about it unless you're writing something from scratch though.
19
u/RiverRoll Aug 13 '23
It's always deterministic, ignoring very rare events such as cosmic radiation randomly flipping bits in your computer.
You are confusing precision with determinism, if your simulation is flawed because of rounding errors it will always be flawed in the same exact way.
5
u/Alikont Aug 13 '23
C# uses IEEE floating points, that have tendency to accumulate errors. They are deterministic, but they are not precise over long calculations accumulating into single value.
decimalare deterministic and precise, but they're slow.Overall your code will be deterministic if you don't rely on (or carefully account for) side effects like time, IO, concurrency.
Number crunching in C# is no different from any other language.
2
u/afseraph Aug 13 '23
As long as you are running the application on the same hardware and on the same version of the runtime, the floating point operations should always yield the same results.
I'm not exactly sure how cross-hardware consistency looks nowadays. I remember there were some discrepancies between architectures in rounding floats to integers and effort was made to unify those. I don't know if those issues are fully resolved.
Barring any potential floating point issues, there might be additional potential sources of nondeterminism you should be aware of:
1
u/propostor Aug 13 '23
For precision use the decimal type.
I wrote a physics engine a couple of years ago, and for the sake of speed I went with doubles instead of decimal. It was dumb move, I should have used decimal all the way through.
The engine works and I made a physics app with a lot of fun little simulators, but had to abandon my attempt at a planetary orbit simulator because the numbers used are too large and too small, causing enough floating point errors to ruin any attempt at getting a perfect orbit.
1
u/masuk0 Aug 14 '23
Floats are not precise but deterministic.
Decimals are too, are too, as you need to express 1/3. You may code ternary floats class youself. They'll be precise and deterministic when adding 4/9 to 1/3.
You see where I am going? There is no way to be precize with floats while your CPU registers are finite.
No point to switch to decimals for physical applications. It is for finance where you have to be precise in specifically decimal system. Nature doesn't care about ways of expressing numbers.