fabs.hpp
Go to the documentation of this file.
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>
9 #include <cmath>
10 
11 namespace stan {
12 namespace math {
13 
14 template <typename T>
15 inline 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
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
const double NOT_A_NUMBER
Definition: constants.hpp:53
T value_of(const fvar< T > &v)
Definition: value_of.hpp:16
var fabs(const var &a)
Definition: fabs.hpp:49
#define unlikely(expr)
Definition: lz4.cxx:121
int is_nan(const fvar< T > &x)
Definition: is_nan.hpp:19