Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check input conditions for log1m() before delegating to log1p(). #725

Merged
merged 12 commits into from
Feb 3, 2018
10 changes: 7 additions & 3 deletions stan/math/prim/scal/fun/log1m.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define STAN_MATH_PRIM_SCAL_FUN_LOG1M_HPP

#include <stan/math/prim/scal/fun/log1p.hpp>
#include <stan/math/prim/scal/err/check_less_or_equal.hpp>

namespace stan {
namespace math {
Expand Down Expand Up @@ -33,10 +34,13 @@ namespace math {
*
* @param[in] x Argument.
* @return Natural log of one minus the argument.
* @throw std::domain_error If the argument is greater than 1.
* @throw std::overflow_error If the computation overflows.
* @throw <code>std::domain_error</code> If the argument is greater than 1.
* @throw <code>std::overflow_error</code> If the computation overflows.
*/
inline double log1m(double x) { return stan::math::log1p(-x); }
inline double log1m(double x) {
check_less_or_equal("log1m(x)", "x", x, 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace "log1m(x)" with `"log1m". You'll need to update the test to match. (The rest of our error messages just have the function name followed by colon, no arguments in the function)

return stan::math::log1p(-x);
}

} // namespace math
} // namespace stan
Expand Down
4 changes: 3 additions & 1 deletion test/unit/math/prim/scal/fun/log1m_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stan/math/prim/scal.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <gtest/gtest.h>
#include <test/unit/util.hpp>
#include <limits>

TEST(MathFunctions, log1m) {
Expand All @@ -13,7 +14,8 @@ TEST(MathFunctions, log1mOverflow) {
}

TEST(MathFunctions, log1m_exception) {
EXPECT_THROW(stan::math::log1m(10.0), std::domain_error);
EXPECT_THROW_MSG(stan::math::log1m(10.0), std::domain_error,
"log1m(x): x is 10, but must be less than or equal to 1");
}

TEST(MathFunctions, log1m_nan) {
Expand Down