1 #ifndef STAN_MATH_FWD_SCAL_FUN_FABS_HPP 2 #define STAN_MATH_FWD_SCAL_FUN_FABS_HPP 3 4 #include <stan/math/fwd/core.hpp> 5 #include <stan/math/prim/scal/fun/constants.hpp> 6 #include <stan/math/prim/scal/fun/is_nan.hpp> 7 #include <stan/math/prim/scal/fun/value_of.hpp> 8 #include <stan/math/prim/scal/meta/likely.hpp> 9 #include <cmath> 10 11 namespace stan { 12 namespace math { 13 14 template <typename T> fabs(const fvar<T> & x)15inline fvar<T> fabs(const fvar<T>& x) { 16 using std::fabs; 17 18 if (unlikely(is_nan(value_of(x.val_)))) 19 return fvar<T>(fabs(x.val_), NOT_A_NUMBER); 20 else if (x.val_ > 0.0) 21 return x; 22 else if (x.val_ < 0.0) 23 return fvar<T>(-x.val_, -x.d_); 24 else 25 return fvar<T>(0, 0); 26 } 27 28 } // namespace math 29 } // namespace stan 30 #endif 31