C++, PRNGs, and Function Parameter Order of Evaluation

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.

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 back together — 2018/09/18 @ 21:04

breakup relationship Support

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

Trackback by how to stop thinking about a guy — 2018/09/24 @ 06:17

how to stop thinking about A guy

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

Trackback by couples relationship help — 2018/09/24 @ 14:38

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

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

Trackback by daily life — 2018/09/27 @ 02:22

just click the following web page

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

Trackback by One day at a time lyrics — 2018/09/28 @ 07:36

Flyinghonda.De

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

Trackback by back together — 2018/10/06 @ 14:33

To relationship Counseling

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

Trackback by Homepage — 2018/10/14 @ 06:27

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

Trackback by Relationship help — 2018/10/15 @ 05:27

http://Nobodysproperty.com

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

Trackback by save relationship — 2018/10/18 @ 07:43

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

Trackback by abusive relationship signs — 2018/11/03 @ 10:23

Want Ex Back

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

Trackback by Sac hiep vien — 2018/12/03 @ 00:20

sachiepvien.net

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

Trackback by phong kham phu khoa — 2018/12/03 @ 03:30

phong kham phu khoa

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

Trackback by flight network, Flight Network — 2018/12/04 @ 17:29

[…] 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 estate — 2018/12/04 @ 17:32

[…] 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 hen party events — 2018/12/07 @ 17:36

[…] 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 food packaging machine — 2018/12/09 @ 02:22

[…] 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 same time — 2018/12/20 @ 12:20

Personal Life

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

Trackback by dating book for men — 2019/01/06 @ 06:29

go to this website

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

Trackback by Exercises for arms — 2019/01/07 @ 07:47

Herzlos-online.De

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

Trackback by know if your boyfriend really loves you — 2019/01/09 @ 15:56

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

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

Trackback by Personal beliefs — 2019/01/21 @ 23:08

news.reddif.Info

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

Trackback by stop smoking — 2019/01/29 @ 03:03

discover this info here

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

Trackback by relationship breakups — 2019/02/15 @ 05:46

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

Trackback by personal beliefs — 2019/02/23 @ 10:02

self Development

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

Trackback by I want luv — 2019/03/13 @ 14:19

http://Www.Daforumanau.net

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

Trackback by Amateur porn videos — 2019/03/25 @ 08:04

HottyDesire

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

Trackback by amateur bbw porn — 2019/03/26 @ 05:38

hotty Desire

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

Trackback by Best amateur porn — 2019/03/26 @ 11:42

hottydesire

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

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

Trackback by Life changes — 2019/04/02 @ 05:00

En.Recidemia.com

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

dating Men

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

https://www.pornouni.com

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

Trackback by Datomg. dating tips — 2019/04/16 @ 12:58

Raunitschke.eu

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

Trackback by daily life — 2019/04/30 @ 18:51

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

Trackback by Amateur home porn — 2019/05/03 @ 19:49

HottyDesire

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

saltriverbg.com

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

Trackback by amateur porn tube — 2019/05/12 @ 08:44

Hotty Desire

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

Trackback by same marriage — 2019/05/15 @ 17:44

top relationship expert

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

Trackback by find luv — 2019/05/16 @ 21:44

Save Relationship

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

Trackback by amateur couple porn — 2019/05/17 @ 13:47

amateur milf porn

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

Trackback by find luv — 2019/05/18 @ 02:00

online Dating

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

Trackback by make him commi — 2019/05/18 @ 20:16

simply click the following website page

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

Trackback by Free porn tube — 2019/05/19 @ 14:00

jnk-ent.jp

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

Trackback by abusive relationships — 2019/05/20 @ 18:46

dating And women

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

Trackback by free porn videos — 2019/06/04 @ 19:45

Hotty Desire

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

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

Trackback by Highly recommended Website — 2019/06/19 @ 06:18

Highly recommended Website

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

Trackback by Daily life — 2019/06/19 @ 18:11

Daily life

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

Trackback by Broken relationship — 2019/06/25 @ 10:55

Broken relationship

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

Trackback by yogakiddoswithgaileee.Com — 2019/06/26 @ 14:31

yogakiddoswithgaileee.Com

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

Trackback by one day At A time cast — 2019/06/29 @ 06:27

one day At A time cast

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

Trackback by www.losport.tv — 2019/07/24 @ 22:27

http://www.losport.tv

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

Trackback by Hotty Desire — 2019/07/30 @ 16:15

Hotty Desire

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

Trackback by shakimuddin.com — 2019/08/11 @ 03:19

shakimuddin.com

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

Trackback by hottydesire — 2019/08/16 @ 21:48

hottydesire

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

Trackback by luv Issues — 2019/09/08 @ 09:34

luv Issues

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

Trackback by Life schedule — 2019/09/14 @ 18:54

Life schedule

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

Trackback by купить квартиру в москве новостройка от застройщика — 2019/10/30 @ 04:31

купить квартиру в москве новостройка от застройщика

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

Trackback by Games At Work — 2019/12/27 @ 15:26

Games At Work

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

Trackback by Banging On Police Interceptor — 2020/01/02 @ 00:12

Banging On Police Interceptor

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

I found a great…

Comment by Rauchermaus — 2020/01/09 @ 10:42

Günstige und qualitativ hochwertige Räuchermischungen und Kräutermischungen auf rauchkutsche.eu bestellen. Der Versand ist innerhalb von 48 Stunden. Räuchermischungen und Spice von guter Qualität hier kaufen und genießen. Die Mischungen werden auch legal-Highs genannt und sind auch vollkommen legal. Steig ein in die Kutsche und genieß die Fahrt! Unsere Mischungen können auch in keinem Test oder MPU nachgewiesen werden. Ihr müsst euch also keine Sorgen machen.
#Räuschermischungen #Räuschermischung #Räuchermischungen #Räuchermischung #günstig #zügig #kaufen #bestellen #Spice #Kräutermischung #Kräutermischungen #Service #Badesalz #Badesalze #legal #Legal High #Legal-High #Party #Spaß
f

Trackback by deltasone generic — 2020/03/09 @ 17:57

deltasone generic

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

Trackback by inhalers online — 2020/03/21 @ 16:56

inhalers online

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

Trackback by chloroquine cost — 2020/03/29 @ 16:50

chloroquine cost

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

Trackback by ciprofloxacin for sale — 2020/04/23 @ 01:04

ciprofloxacin for sale

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

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

Trackback by careprost — 2020/05/04 @ 23:51

careprost

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

Trackback by hydroxychloroquine order — 2020/05/05 @ 03:51

hydroxychloroquine order

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