lines
lines

Reputation: 151

Are all C++ fixed-point operations deterministic?

I'm writing a small RTS engine in C++ and want to use lockstep synchronisation.

As floating point determinism is something I can't even hope to achieve, I have to use fixed point math.

How deterministically (over different compilers and cpus) are typical operations on unsigned ints defined?

I'm especially interested in division, as that would incur rounding.

Upvotes: 9

Views: 990

Answers (1)

user1084944
user1084944

Reputation:

The size of the basic integer types varies on different platforms. You can avoid this problem by using uint32_t and similar types.

Signed integer overflow is undefined behavior, although overflow is well-defined for unsigned integral types (you do the arithmetic modulo 2^N). Even still, you have to watch out for it, because modular arithmetic is usually not what you were trying to do.

I believe the standard once upon a time left open exactly how division is rounded (although I'm not sure if "positive / positive" was ever left open). But I think this is standardized now, and even if not, rounding towards 0 is nearly universal.

But you can use numeric_limits to check. If this documentation is to be believed, the standard does guarantee rounding towards zero.

Upvotes: 6

Related Questions