Experilous

Tl;dr:
If numerical determinism is important for your application, never use a PRNG to generate more than one function parameter at a time.

I just got bit by C++’s unfortunate underspecificity regarding the order of evaluation of function parameters. I was working on adapting a procedural generation algorithm (based loosely on my planet generator code) from floating point numbers to fixed point numbers. The goal was to ensure that regardless of compiler, OS, or CPU architecture, the same code would generate the same planet, if starting from the same pseudo-random number seed and using the same generation parameters. Despite IEEE 754 being an extensively designed and very mature standard, in practice there are more than enough other variables at play to make floating point unreliable from machine to machine if exact replication is required. Just search for “floating point determinism” for plenty of examples.

Quite obnoxiously, but not unexpectedly, the results with the new fixed point code were substantially different from the floating point results. Not completely different; I could tell that some steps were behaving nearly identically, which proved that most of my fixed point code was functioning correctly. Tracking down the source of the discrepancy was harder. Was my implementation of a cross product backwards, leading to blatantly wrong vectors? Or was it something more subtle, such as a chaotic variable tipping just enough across its threshold to lead to radically different behavior?

Here’s what the generated maps looked like, the original floating point on the left, the fixed point in the middle, and the image difference between them on the right:

Map generated with floating point numbers, fixed point numbers, and the image difference between them. Severe discrepancies, but the continents are the same shape.

After going through and attempting to compare results of individual computations from my old floating point code to the new fixed point code, and nearly running out of relevant bits of code to examine, I came upon the code that gave each of my tectonic plates different physical properties. I could tell by the map generated that the shape of the continents were the same regardless of number type, but the elevations at the plate boundaries were different. I had already checked all the code that estimates plate boundary stresses, the code that propagates those stresses inward away from the boundaries, and the code that translates those stresses into elevation values. The only thing left, then, was the code that generated the movements of the plates that would produce all the different stresses and elevations.

At first glance, nothing seemed out of the ordinary. I was simply randomly generating a few different values to determine the drift, spin, and oceanic properties of each plate. Given the nature my half-finished transition from float to fixed (I was still randomly generating floating point numbers, and just converting them to fixed point afterward), I couldn’t see any way in which I was grossly miscalculating anything here.

And then I remembered that C++ does not specify the order of evaluation of expressions passed in as function parameters. I was using the emplace_back() function of a std::vector to construct a std::tuple. Every single one of the five parameters were expressions that included a call to my pseudo-random number generator. Naturally, to get the same results out of a PRNG, you have to use the sequence of generated numbers in exactly the same order, or else things will work out quite differently. I decided to pull these expressions out and assign their results to five local variables first, and then emplace them into the vector once they were calculated.

After doing this, the map generated was something else different altogether. Continents shapes weren’t even the same, but I could tell that the shape of the underlying tectonic plates still were. That indicated that the oceanic variable was now being generated in a different order also, whereas before I guess it was consistent between the floating point and fixed point programs.

Knowing that this was sufficient to cause significant differences, I proceeded to determine if it was the only problem tripping me up. To do this, I simply fiddled around with the explicit order of the expressions, and was finally able to find an order that generated essentially the same results as I had achieved with the floating point code. You can tell by the image difference below that discrepancies are quite minor, and are pretty much what one might expect from changing the numeric type used in a non-chaotic simulation.

Map generated with floating point numbers, fixed point numbers, and the image difference between them. After correcting order of evaluations, discrepancies are minimal, more in line with what would be expected.

So after all that investigation into how I was messing up my fixed point calculations, it turns out I wasn’t getting them wrong at all! My compiler simply decided to change the order of function parameter evaluation on me after switching from floating point to fixed point. And if a single version of a single compiler on a single OS and architecture changes its mind, I can be certain that I’ll get different results on different compilers and for different platforms! I could have missed this little bug for a long time if I hadn’t ran into it while verifying the correctness of my fixed point calculations. I might have thought that all was proceeding smoothly, only to be dismayed when building the application for other platforms. Even then, I’d only notice if I were comparing results across platforms. More likely, I’d release the broken program and start getting bug reports from users sharing planet seeds with each other; not good!

The conclusion? If numerical determinism is important for your application, never use a PRNG to generate more than one function parameter at a time.

51 Comments

Comment by Robert de Forest — 2014/12/30 @ 17:49

Nice. I was not aware of this aspect of C++ and presumably it can vary between implementations and the wisdom applies to other languages as well. Semantics is tricky.

In my own code I tend to avoid putting anything other than a variable or constant in the arguments to a function, but I didn’t realize before now that there could even be a good reason for that. The one exception is in JavaScript where I in-line my callback functions if they’re small enough:

function thisThenThat(foo, that, bar) {
this(foo, function () { that(bar) });
}

But the declaration of and passing of the anonymous function has no side-effects, so in principle it’s the same.

I’m filing “function arguments with side effects” as a bad code smell in my book. That I don’t have. But should.

Thanks for the interesting post!

Trackback by daily life2018/09/27 @ 02:22

just click the following web page

Experilous: C , PRNGs, and Function Parameter Order of Evaluation

Trackback by Homepage2018/10/14 @ 06:27

… [Trackback]

[…] Informations on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by go here2018/12/04 @ 17:27

… [Trackback]

[…] Read More here|Read More|Read More Infos here|There you can find 34251 more Infos|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by flight network, Flight Network2018/12/04 @ 17:29

… [Trackback]

[…] Find More here|Find More|Find More Informations here|Here you can find 38196 additional Informations|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by Tampa real estate2018/12/04 @ 17:32

… [Trackback]

[…] Find More here|Find More|Read More Infos here|There you can find 41723 more Infos|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by buy real youtube views2018/12/04 @ 17:36

… [Trackback]

[…] Find More here|Find More|Find More Informations here|There you can find 69096 additional Informations|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by buy youtube subscribers2018/12/04 @ 17:40

… [Trackback]

[…] Find More here|Find More|Find More Infos here|Here you can find 31109 more Infos|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by best site to buy instagram followers2018/12/04 @ 17:42

… [Trackback]

[…] Find More on|Find More|Read More Infos here|Here you can find 36202 additional Infos|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by buy youtube likes2018/12/04 @ 17:44

… [Trackback]

[…] Find More here|Find More|Find More Informations here|There you can find 9418 more Informations|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by buy instagram followers2018/12/04 @ 17:49

… [Trackback]

[…] Read More here|Read More|Find More Infos here|There you will find 32955 additional Infos|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by nulled-scripts.ch2018/12/04 @ 17:52

… [Trackback]

[…] Read More on|Read More|Read More Infos here|There you will find 31315 more Infos|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by VIPCLUB8882018/12/04 @ 17:58

… [Trackback]

[…] Find More on|Find More|Find More Infos here|Here you will find 3440 more Infos|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by Buy Fake Passport Online2018/12/05 @ 05:29

… [Trackback]

[…] Read More on|Read More|Read More Infos here|Here you can find 7911 additional Infos|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by westernunion carding2018/12/05 @ 06:02

… [Trackback]

[…] Read More here|Read More|Find More Informations here|There you will find 50194 more Informations|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by high school science fair projects2018/12/05 @ 11:11

… [Trackback]

[…] Find More here|Find More|Read More Informations here|Here you can find 31660 more Informations|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by Pe Bible ingredients2018/12/05 @ 11:32

… [Trackback]

[…] Read More on|Read More|Read More Infos here|Here you will find 65256 additional Infos|Informations on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by 바둑이2018/12/05 @ 12:52

… [Trackback]

[…] Find More here|Find More|Find More Infos here|Here you will find 58543 more Infos|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by anavar steroid2018/12/05 @ 23:45

… [Trackback]

[…] Read More here|Read More|Find More Informations here|Here you can find 77328 additional Informations|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by preference shares2018/12/05 @ 23:52

… [Trackback]

[…] Find More here|Find More|Read More Infos here|There you will find 84111 more Infos|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by best full face snorkel mask review2018/12/06 @ 01:54

… [Trackback]

[…] Find More on|Find More|Read More Infos here|Here you can find 62567 additional Infos|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by Weitere Details2018/12/06 @ 03:06

… [Trackback]

[…] Read More on|Read More|Find More Informations here|There you will find 73882 more Informations|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by forex signals2018/12/06 @ 22:31

… [Trackback]

[…] Read More on|Read More|Find More Infos here|Here you will find 69648 more Infos|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by SEO Las Vegas2018/12/07 @ 02:07

… [Trackback]

[…] Read More on|Read More|Find More Infos here|There you will find 38062 more Infos|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by Armodafini2018/12/07 @ 17:34

… [Trackback]

[…] Read More on|Read More|Read More Infos here|There you will find 26880 additional Infos|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by hen party events2018/12/07 @ 17:36

… [Trackback]

[…] Find More here|Find More|Find More Informations here|Here you will find 43567 additional Informations|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by merchant accounts for cbd products2018/12/07 @ 17:40

… [Trackback]

[…] Read More here|Read More|Find More Infos here|There you can find 50599 more Infos|Informations on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by stag party ideas2018/12/07 @ 17:41

… [Trackback]

[…] Read More here|Read More|Read More Informations here|Here you can find 95911 more Informations|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by Phentermine kaufen2018/12/08 @ 17:29

… [Trackback]

[…] Read More here|Read More|Read More Infos here|Here you will find 37800 additional Infos|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by Professional SEO Services2018/12/08 @ 17:44

… [Trackback]

[…] Find More here|Find More|Read More Informations here|There you can find 13106 more Informations|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by food packaging machine2018/12/09 @ 02:22

… [Trackback]

[…] Find More on|Find More|Find More Informations here|Here you will find 8038 additional Informations|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by ipad sale2018/12/09 @ 04:59

… [Trackback]

[…] Read More here|Read More|Read More Informations here|Here you will find 89602 more Informations|Informations on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by litoshi2018/12/09 @ 08:58

… [Trackback]

[…] Read More here|Read More|Find More Informations here|There you will find 59066 more Informations|Informations on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by best affiliate network2018/12/09 @ 10:32

… [Trackback]

[…] Read More here|Read More|Find More Informations here|Here you will find 11763 more Informations|Informations to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by letmejerk.com2018/12/09 @ 13:05

… [Trackback]

[…] Find More on|Find More|Read More Informations here|There you will find 11148 additional Informations|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by btc2018/12/09 @ 13:08

… [Trackback]

[…] Find More on|Find More|Find More Infos here|Here you will find 27601 more Infos|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by earn by playing games2018/12/09 @ 15:17

… [Trackback]

[…] Read More on|Read More|Read More Informations here|There you will find 61209 more Informations|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by claim free litcoin2018/12/09 @ 18:24

… [Trackback]

[…] Find More on|Find More|Read More Informations here|Here you can find 1508 additional Informations|Infos to that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Trackback by betting bola2018/12/14 @ 02:23

… [Trackback]

[…] Read More here|Read More|Read More Informations here|Here you will find 93304 more Informations|Infos on that Topic: experilous.com/1/blog/post/cpp-prng-and-function-parameter-order-of-evaluation […]

Leave a comment