diff --git a/stan/math/prim/fun/square.hpp b/stan/math/prim/fun/square.hpp index 0ed671bb96d..9ab83c70691 100644 --- a/stan/math/prim/fun/square.hpp +++ b/stan/math/prim/fun/square.hpp @@ -25,7 +25,8 @@ namespace math { */ template * = nullptr> inline double square(const T x) { - return std::pow(x, 2); + double x_dbl = x; + return x_dbl * x_dbl; } /** diff --git a/stan/math/rev/fun/squared_distance.hpp b/stan/math/rev/fun/squared_distance.hpp index 584114ee2c7..42042193f01 100644 --- a/stan/math/rev/fun/squared_distance.hpp +++ b/stan/math/rev/fun/squared_distance.hpp @@ -21,7 +21,8 @@ namespace math { inline var squared_distance(const var& a, const var& b) { check_finite("squared_distance", "a", a); check_finite("squared_distance", "b", b); - return make_callback_vari(std::pow(a.val() - b.val(), 2), + double difference = a.val() - b.val(); + return make_callback_vari(difference * difference, [a, b](const auto& vi) mutable { const double diff = 2.0 * (a.val() - b.val()); a.adj() += vi.adj_ * diff; @@ -35,7 +36,8 @@ inline var squared_distance(const var& a, const var& b) { inline var squared_distance(const var& a, double b) { check_finite("squared_distance", "a", a); check_finite("squared_distance", "b", b); - return make_callback_vari(std::pow(a.val() - b, 2), + double difference = a.val() - b; + return make_callback_vari(difference * difference, [a, b](const auto& vi) mutable { a.adj() += vi.adj_ * 2.0 * (a.val() - b); });