Note that there are some explanatory texts on larger screens.

plurals
  1. POBaffling Inline Behaviour from Random Number Generator Wrapper (C++)
    primarykey
    data
    text
    <p>I have a simple wrapper for an Mersenne twister random number generator. The purpose is to scale the number returned by the generator (between 0 and 1) to between argument defined limits (begin and end).</p> <p>So my function is </p> <pre><code>inline float xlRandomFloat(float begin, float end) {return (begin+((end-begin)*genrand_real2()));} </code></pre> <p>I don't believe the implementation of genrand_real2() function is important, but if I am wrong it can be found <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c" rel="nofollow noreferrer">here</a></p> <p>The problem is the function does not return the translated result. The scaling (multiplying by (begin-end) seems to work correctly, but the addition of begin does not seem to be returned.</p> <p>So if I call xlRandomFloat(5,10) - I get values between 0 and 5.</p> <p>If I debug with GDB, and use the print function then it shows the correct result. So then I tried separating things into lines to see what happens</p> <pre><code>inline float xlRandomFloat(float begin, float end) { float ret; ret=(((end-begin)*genrand_real2())); ret+=begin; return ret;}; </code></pre> <p>When debugging, it jumped straight from the first line into the genrand_real2() function and skipped out every thing else entirely. That was really confusing so I thought it may have something to do with the inlining. I moved the file from this .hpp file to the .cpp and removed the inline keyword and everything works correctly.</p> <p>But why does this behavior occur, and how can I inline this function? Also, I am not sure if this is relevant, but often when I made changes to the sources, my Make compilation would say there is nothing to be done. Which is unusual since normally I expect make to pick up on changes in the sources and rebuild accordingly.</p> <p>Any ideas.</p> <p>Thanks</p> <p>Zenna</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload