1 #ifndef STAN_MATH_PRIM_FUN_TRUNC_HPP 2 #define STAN_MATH_PRIM_FUN_TRUNC_HPP 3 4 #include <stan/math/prim/meta.hpp> 5 #include <stan/math/prim/functor/apply_scalar_unary.hpp> 6 #include <cmath> 7 8 namespace stan { 9 namespace math { 10 11 /** 12 * Structure to wrap `trunc()` so it can be vectorized. 13 */ 14 struct trunc_fun { 15 /** 16 * Return the truncation of the specified argument to the 17 * nearest value. 18 * 19 * @tparam T type of argument 20 * @param x argument 21 * @return truncation of the argument 22 */ 23 template <typename T> funstan::math::trunc_fun24 static inline T fun(const T& x) { 25 using std::trunc; 26 return trunc(x); 27 } 28 }; 29 30 /** 31 * Return the elementwise application of `trunc()` to 32 * specified argument container. The return type promotes the 33 * underlying scalar argument type to double if it is an integer, 34 * and otherwise is the argument type. 35 * 36 * @tparam T type of container 37 * @param x container 38 * @return elementwise trunc of container elements 39 */ 40 template <typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t< 41 T>* = nullptr> trunc(const T & x)42inline auto trunc(const T& x) { 43 return apply_scalar_unary<trunc_fun, T>::apply(x); 44 } 45 46 } // namespace math 47 } // namespace stan 48 49 #endif 50