Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion stan/math/prim/fun/square.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace math {
*/
template <typename T, require_arithmetic_t<T>* = nullptr>
inline double square(const T x) {
return std::pow(x, 2);
double x_dbl = x;
return x_dbl * x_dbl;
Comment thread
WardBrian marked this conversation as resolved.
}

/**
Expand Down
6 changes: 4 additions & 2 deletions stan/math/rev/fun/squared_distance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
Expand Down