1 #ifndef STAN_MATH_PRIM_FUN_FINITE_DIFF_STEPSIZE_HPP
2 #define STAN_MATH_PRIM_FUN_FINITE_DIFF_STEPSIZE_HPP
3 
4 #include <stan/math/prim/fun/constants.hpp>
5 #include <stan/math/prim/fun/fabs.hpp>
6 #include <cmath>
7 
8 namespace stan {
9 namespace math {
10 
11 /**
12  * Return the stepsize for finite difference evaluations at the
13  * specified scalar.
14  *
15  * <p>The formula used is `stepsize(u) = cbrt(epsilon) * max(1,
16  * abs(u)).`
17  *
18  * @param u initial value to increment
19  * @return stepsize away from u for finite differences
20  */
finite_diff_stepsize(double u)21 inline double finite_diff_stepsize(double u) {
22   using std::fabs;
23   static const double cbrt_epsilon = std::cbrt(EPSILON);
24   return cbrt_epsilon * std::fmax(1, fabs(u));
25 }
26 
27 }  // namespace math
28 }  // namespace stan
29 #endif
30