Boost C++ Libraries: Ticket #9042: boost::math, cdf(complement(normal_distribution<>(...)) fails to catch invalid scale and location parameters https://svn.boost.org/trac10/ticket/9042 <p> In boost/math/distributions/normal.hpp: </p> <pre class="wiki">template &lt;class RealType, class Policy&gt; inline RealType cdf(const complemented2_type&lt;normal_distribution&lt;RealType, Policy&gt;, RealType&gt;&amp; c) { BOOST_MATH_STD_USING // for ADL of std functions RealType sd = c.dist.standard_deviation(); RealType mean = c.dist.mean(); RealType x = c.param; static const char* function = "boost::math::cdf(const complement(normal_distribution&lt;%1%&gt;&amp;), %1%)"; if((boost::math::isinf)(x)) { if(x &lt; 0) return 1; // cdf complement -infinity is unity. return 0; // cdf complement +infinity is zero } // These produce MSVC 4127 warnings, so the above used instead. //if(std::numeric_limits&lt;RealType&gt;::has_infinity &amp;&amp; x == std::numeric_limits&lt;RealType&gt;::infinity()) //{ // cdf complement +infinity is zero. // return 0; //} //if(std::numeric_limits&lt;RealType&gt;::has_infinity &amp;&amp; x == -std::numeric_limits&lt;RealType&gt;::infinity()) //{ // cdf complement -infinity is unity. // return 1; //} RealType result = 0; if(false == detail::check_scale(function, sd, &amp;result, Policy())) return result; if(false == detail::check_location(function, mean, &amp;result, Policy())) return result; if(false == detail::check_x(function, x, &amp;result, Policy())) return result; RealType diff = (x - mean) / (sd * constants::root_two&lt;RealType&gt;()); result = boost::math::erfc(diff, Policy()) / 2; return result; } // cdf complement </pre><p> The test if((boost::math::isinf)(x)) occurs before check_scale() or check_location() is called, returning 1 or 0 for infinite x, even if sd or mean are invalid. </p> <p> The calls to check_scale() and check_location() should be moved up just before the test if((boost::math::isinf)(x)). </p> <p> See, for example, how the tests are ordered here: </p> <pre class="wiki">template &lt;class RealType, class Policy&gt; inline RealType cdf(const normal_distribution&lt;RealType, Policy&gt;&amp; dist, const RealType&amp; x) { BOOST_MATH_STD_USING // for ADL of std functions RealType sd = dist.standard_deviation(); RealType mean = dist.mean(); static const char* function = "boost::math::cdf(const normal_distribution&lt;%1%&gt;&amp;, %1%)"; RealType result = 0; if(false == detail::check_scale(function, sd, &amp;result, Policy())) { return result; } if(false == detail::check_location(function, mean, &amp;result, Policy())) { return result; } if((boost::math::isinf)(x)) { if(x &lt; 0) return 0; // -infinity return 1; // + infinity } // These produce MSVC 4127 warnings, so the above used instead. //if(std::numeric_limits&lt;RealType&gt;::has_infinity &amp;&amp; x == std::numeric_limits&lt;RealType&gt;::infinity()) //{ // cdf +infinity is unity. // return 1; //} //if(std::numeric_limits&lt;RealType&gt;::has_infinity &amp;&amp; x == -std::numeric_limits&lt;RealType&gt;::infinity()) //{ // cdf -infinity is zero. // return 0; //} if(false == detail::check_x(function, x, &amp;result, Policy())) { return result; } RealType diff = (x - mean) / (sd * constants::root_two&lt;RealType&gt;()); result = boost::math::erfc(-diff, Policy()) / 2; return result; } // cdf </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9042 Trac 1.4.3 John Maddock Sun, 08 Sep 2013 08:46:19 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/9042#comment:1 https://svn.boost.org/trac10/ticket/9042#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">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/85601" title="Fix order of error checks. Fixes #9042.">[85601]</a>) Fix order of error checks. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9042" title="#9042: Bugs: boost::math, cdf(complement(normal_distribution&lt;&gt;(...)) fails to catch ... (closed: fixed)">#9042</a>. </p> Ticket John Maddock Sat, 28 Sep 2013 16:19:56 GMT <link>https://svn.boost.org/trac10/ticket/9042#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9042#comment:2</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/85987" title="Merge accumulated patches from Trunk. Refs #8384, Refs #8855, refs ...">[85987]</a>) Merge accumulated patches from Trunk. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8384" title="#8384: Feature Requests: Make shared_ptr movable (closed: invalid)">#8384</a>, Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8855" title="#8855: Bugs: [math] GCC 4.8+ warns unused local typedef... (closed: fixed)">#8855</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9107" title="#9107: Bugs: unused parameter warning in fraction with clang (closed: fixed)">#9107</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9109" title="#9109: Bugs: Warning in bessel with -Wshadow (closed: fixed)">#9109</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8333" title="#8333: Bugs: [math] PGI 11.3 problems (sph_bessel.cpp, sph_bessel.cpp) (closed: fixed)">#8333</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8621" title="#8621: Bugs: erf function gives wrong results with pgcpp - PGI 10.4 (closed: fixed)">#8621</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8732" title="#8732: Bugs: Need to protect calls against C99 math macro expansion (closed: fixed)">#8732</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8733" title="#8733: Feature Requests: Testing for unprotected references to C99 math macros (closed: fixed)">#8733</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8837" title="#8837: Bugs: boost::math::students_t quantile() fails for huge degrees of freedom (closed: fixed)">#8837</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8940" title="#8940: Bugs: Argument promotion fails dependent libs on platforms not supporting ... (closed: fixed)">#8940</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9042" title="#9042: Bugs: boost::math, cdf(complement(normal_distribution&lt;&gt;(...)) fails to catch ... (closed: fixed)">#9042</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9087" title="#9087: Support Requests: [boost math] error: no operator &#34;=&#34; matches these operands in ... (closed: fixed)">#9087</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9104" title="#9104: Bugs: boost::math::ellint_2 bug in x86_64 double precision (closed: fixed)">#9104</a>, refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9126" title="#9126: Bugs: Logistic distribution pdf() and cdf(complement()) fail to catch ... (closed: fixed)">#9126</a>. </p> </description> <category>Ticket</category> </item> </channel> </rss>