Boost C++ Libraries: Ticket #2703: lagged_fibonacci_01::seed() bug https://svn.boost.org/trac10/ticket/2703 <p> Hi, </p> <p> In &lt;boost/random/lagged_fibonacci_01.hpp&gt; </p> <p> line309: template&lt;class It&gt;<br /> line310: void seed(It&amp; first, It last)<br /> line311: {<br /> line312:#ifndef BOOST_NO_STDC_NAMESPACE<br /> line313: <em> allow for Koenig lookup<br /> line314: using std::fmod;<br /> line315: using std::pow;<br /> line316:#endif[[BR]] line317: unsigned long mask = ~((~0u) &lt;&lt; (w%32)); </em> now lowest w bits set<br /> line318: <a class="missing wiki">RealType</a> two32 = pow(<a class="missing wiki">RealType</a>(2), 32);<br /> line319: unsigned int j;<br /> line320: for(j = 0; j &lt; long_lag &amp;&amp; first != last; ++j, ++first) {<br /> line312: x[j] = <a class="missing wiki">RealType</a>(0);<br /> line313: for(int k = 0; k &lt; w/32 &amp;&amp; first != last; ++k, ++first)<br /> line314: x[j] += *first / pow(two32,k+1);<br /> line315: if(first != last &amp;&amp; mask != 0)<br /> line316: x[j] += fmod((*first &amp; mask) / _modulus, <a class="missing wiki">RealType</a>(1));<br /> line317: }<br /> line318: i = long_lag;<br /> line319: if(first == last &amp;&amp; j &lt; long_lag)<br /> line320: throw std::invalid_argument("lagged_fibonacci_01::seed");<br /> line321: }<br /> </p> <p> line 313 maybe rewrite to: </p> <blockquote> <p> for(int k = 0; k &lt; w/32 &amp;&amp; first != last; ++k) </p> </blockquote> <p> because of the first "for loop" can't be broken. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2703 Trac 1.4.3 rick68@… Sat, 31 Jan 2009 14:28:35 GMT attachment set https://svn.boost.org/trac10/ticket/2703 https://svn.boost.org/trac10/ticket/2703 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">lagged_fibonacci.hpp.diff</span> </li> </ul> <p> Patch </p> Ticket Steven Watanabe Sat, 31 Jan 2009 23:21:08 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2703#comment:1 https://svn.boost.org/trac10/ticket/2703#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> This is not a bug. The value_type of the iterator is assumed to be a 32 bit integer, while <a class="missing wiki">RealType</a> is usually a double. The loop is needed because <a class="missing wiki">RealType</a> may have more bits of precision than the iterator's value_type. Thus, more than one element of the input range may be needed to initialize a single element of the state. Therefore the iterator needs to be incremented in the inner loop. </p> Ticket rick68@… Sun, 01 Feb 2009 05:58:54 GMT cc, status changed; resolution deleted https://svn.boost.org/trac10/ticket/2703#comment:2 https://svn.boost.org/trac10/ticket/2703#comment:2 <ul> <li><strong>cc</strong> <span class="trac-author">rick68@…</span> added </li> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">invalid</span> </li> </ul> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/2703#comment:1" title="Comment 1">steven_watanabe</a>: </p> <blockquote class="citation"> <p> This is not a bug. The value_type of the iterator is assumed to be a 32 bit integer, while <a class="missing wiki">RealType</a> is usually a double. The loop is needed because <a class="missing wiki">RealType</a> may have more bits of precision than the iterator's value_type. Thus, more than one element of the input range may be needed to initialize a single element of the state. Therefore the iterator needs to be incremented in the inner loop. </p> </blockquote> <p> Hi, </p> <p> I tried this test code: </p> <p> ================================================================================<br /> #include &lt;iostream&gt;<br /> #include &lt;vector&gt;<br /> #include &lt;stdexcept&gt;<br /> #include &lt;boost/cstdlib.hpp&gt;<br /> #include &lt;boost/cstdint.hpp&gt;<br /> #include &lt;boost/random/lagged_fibonacci.hpp&gt;<br /> int main(void) { </p> <blockquote> <p> typedef std::vector&lt;boost::int32_t&gt; seed_type;<br /> </p> </blockquote> <p> </p> <blockquote> <p> boost::lagged_fibonacci607 gen;<br /> seed_type seed(gen.long_lag);<br /> seed_type::iterator first;<br /> seed_type::iterator second;<br /> </p> </blockquote> <p> </p> <blockquote> <p> std::cout &lt;&lt; "(second - first) = [" &lt;&lt; (second - first) &lt;&lt; ']' &lt;&lt; std::endl;<br /> </p> </blockquote> <p> </p> <blockquote> <p> first = seed.begin();<br /> second = seed.end();<br /> std::cout &lt;&lt; "(second - first) = [" &lt;&lt; (second - first) &lt;&lt; ']' &lt;&lt; std::endl;<br /> </p> </blockquote> <p> </p> <blockquote> <p> try { </p> <blockquote> <p> gen.seed(first, second);<br /> </p> </blockquote> <p> } catch (const std::invalid_argument&amp; e) { </p> <blockquote> <p> std::cout &lt;&lt; e.what() &lt;&lt; std::endl;<br /> </p> </blockquote> <p> } </p> </blockquote> <p> </p> <blockquote> <p> std::cout &lt;&lt; "(second - first) = [" &lt;&lt; (second - first) &lt;&lt; ']' &lt;&lt; std::endl;<br /> </p> </blockquote> <p> </p> <blockquote> <p> return boost::exit_success;<br /> </p> </blockquote> <p> } ================================================================================ </p> <p> And this is my result:<br /> =================================<br /> (second - first) = <a class="missing changeset" title="No changeset 0 in the repository">[0]</a> <br /> (second - first) = <a class="changeset" href="https://svn.boost.org/trac10/changeset/607" title="Genvocalese? ">[607]</a> <br /> (second - first) = [-607] <br /> =================================<br /> <br /> I can't catch any exception, and member function "gen.seed()" will out of range.<br /> </p> Ticket rick68@… Sun, 01 Feb 2009 15:47:59 GMT <link>https://svn.boost.org/trac10/ticket/2703#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2703#comment:3</guid> <description> <p> Hi, </p> <p> In &lt;boost/random/subtract_with_carry.hpp&gt;, I found member fucntion substract_with_carry::seed(), it is similar lagged_fibonacci_01::seed(). </p> <p> line295: template&lt;class It&gt;<br /> line296: void seed(It&amp; first, It last)<br /> line297: {<br /> line298:#ifndef BOOST_NO_STDC_NAMESPACE<br /> line299: <em> allow for Koenig lookup<br /> line300: using std::fmod;<br /> line301: using std::pow;<br /> line302:#endif[[BR]] line303: unsigned long mask = ~((~0u) &lt;&lt; (w%32)); </em> now lowest (w%32) bits set<br /> line304: <a class="missing wiki">RealType</a> two32 = pow(<a class="missing wiki">RealType</a>(2), 32);<br /> line305: unsigned int j;<br /> line306: for(j = 0; j &lt; long_lag &amp;&amp; first != last; ++j) {<br /> line307: x[j] = <a class="missing wiki">RealType</a>(0);<br /> line308: for(int i = 0; i &lt; w/32 &amp;&amp; first != last; ++i, ++first)<br /> line309: x[j] += *first / pow(two32,i+1);<br /> line310: if(first != last &amp;&amp; mask != 0) {<br /> line311: x[j] += fmod((*first &amp; mask) / _modulus, <a class="missing wiki">RealType</a>(1));<br /> line312: ++first;<br /> line313: }<br /> line314: }<br /> line315: if(first == last &amp;&amp; j &lt; long_lag)<br /> line316: throw std::invalid_argument("subtract_with_carry_01::seed");<br /> line317: carry = (x[long_lag-1] ? 0 : 1 / _modulus);<br /> line318: k = 0;<br /> line319: }<br /> </p> <p> Maybe lagged_fibonacci_01::seed() has mistake. </p> </description> <category>Ticket</category> </item> <item> <author>rick68@…</author> <pubDate>Sun, 01 Feb 2009 15:50:52 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/2703 https://svn.boost.org/trac10/ticket/2703 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">lagged_fibonacci.hpp.diff2</span> </li> </ul> <p> First patch file maybe has mistake. I put second patch file. </p> Ticket Steven Watanabe Tue, 03 Feb 2009 18:51:19 GMT <link>https://svn.boost.org/trac10/ticket/2703#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2703#comment:4</guid> <description> <p> Ok. I see. Yes, it's the outer increment that needs to be removed. This is probably too late for 1.38, but it should be in 1.39. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Sun, 08 Feb 2009 23:51:00 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2703#comment:5 https://svn.boost.org/trac10/ticket/2703#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/51120" title="correctly detect the end of the range in lagged_fibonacci_01::seed. ...">[51120]</a>) correctly detect the end of the range in lagged_fibonacci_01::seed. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2703" title="#2703: Bugs: lagged_fibonacci_01::seed() bug (closed: fixed)">#2703</a> </p> Ticket buy Cialis side effects interactions Mon, 08 Jun 2009 11:21:56 GMT <link>https://svn.boost.org/trac10/ticket/2703#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2703#comment:6</guid> <description> <p> You cannot kill time without injuring eternity. <a class="ext-link" href="http://forum.studenti.it/members/compraviagraonline.html"><span class="icon">​</span>ordina viagra on line</a> <a class="ext-link" href="http://riereta.net/moodle/user/view.php?id=4164&amp;course=1"><span class="icon">​</span>fioricet information</a> <a class="ext-link" href="http://buycialis.cc/"><span class="icon">​</span>usage of Cialis hcl acetaminophen par</a> yOmHZa. <a class="ext-link" href="http://www.dogomania.com/forum/showthread.php?p=276144"><span class="icon">​</span>cheap tramadol</a> </p> </description> <category>Ticket</category> </item> </channel> </rss>