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
11#include <cmath>
12
13namespace stan {
14namespace math {
15
16template <typename T>
17inline fvar<T> fmod(const fvar<T>& x1, const fvar<T>& x2) {
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 if (unlikely(is_any_nan(value_of(x1.val_), x2))) {
25 return fvar<T>(fmod(x1.val_, x2), NOT_A_NUMBER);
26 } else {
27 return fvar<T>(fmod(x1.val_, x2), x1.d_);
28 }
29}
30
31template <typename T>
32inline fvar<T> fmod(double x1, const fvar<T>& x2) {
33 return fvar<T>(fmod(x1, x2.val_), -x2.d_ * trunc(x1 / x2.val_));
34}
35
36} // namespace math
37} // namespace stan
38#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:17
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