Automatic Differentiation
 
Loading...
Searching...
No Matches
fmod.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_FMOD_HPP
2#define STAN_MATH_FWD_FUN_FMOD_HPP
3
9#include <cmath>
10
11namespace stan {
12namespace math {
13
14template <typename T>
15inline fvar<T> fmod(const fvar<T>& x1, const fvar<T>& x2) {
16 using std::fmod;
17 using std::trunc;
18 return fvar<T>(fmod(x1.val_, x2.val_),
19 x1.d_ - x2.d_ * trunc(x1.val_ / x2.val_));
20}
21
22template <typename T>
23inline fvar<T> fmod(const fvar<T>& x1, double x2) {
24 using std::fmod;
25 if (unlikely(is_any_nan(value_of(x1.val_), x2))) {
26 return fvar<T>(fmod(x1.val_, x2), NOT_A_NUMBER);
27 } else {
28 return fvar<T>(fmod(x1.val_, x2), x1.d_);
29 }
30}
31
32template <typename T>
33inline fvar<T> fmod(double x1, const fvar<T>& x2) {
34 using std::fmod;
35 using std::trunc;
36 return fvar<T>(fmod(x1, x2.val_), -x2.d_ * trunc(x1 / x2.val_));
37}
38
39} // namespace math
40} // namespace stan
41#endif
#define unlikely(x)
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition constants.hpp:56
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
Definition fmod.hpp:15
bool is_any_nan(const T &x)
Returns true if the input is NaN and false otherwise.
fvar< T > trunc(const fvar< T > &x)
Return the nearest integral value that is not larger in magnitude than the specified argument.
Definition trunc.hpp:20
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Scalar val_
The value of this variable.
Definition fvar.hpp:49
Scalar d_
The tangent (derivative) of this variable.
Definition fvar.hpp:61
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40