3.5 Logical functions
Like C++, BUGS, and R, Stan uses 0 to encode false, and 1 to encode true. Stan supports the usual boolean comparison operations and boolean operators. These all have the same syntax and precedence as in C++; for the full list of operators and precedences, see the reference manual.
3.5.1 Comparison operators
All comparison operators return boolean values, either 0 or 1. Each operator has two signatures, one for integer comparisons and one for floating-point comparisons. Comparing an integer and real value is carried out by first promoting the integer value.
int
operator<
(int x, int y)
int
operator<
(real x, real y)
Return 1 if x is less than y and 0 otherwise. \[ \text{operator<}(x,y)
= \begin{cases} 1 & \text{if $x < y$} \\ 0 & \text{otherwise}
\end{cases} \]
Available since 2.0
int
operator<=
(int x, int y)
int
operator<=
(real x, real y)
Return 1 if x is less than or equal y and 0 otherwise. \[
\text{operator<=}(x,y) = \begin{cases} 1 & \text{if $x \leq y$} \\ 0 &
\text{otherwise} \end{cases} \]
Available since 2.0
int
operator>
(int x, int y)
int
operator>
(real x, real y)
Return 1 if x is greater than y and 0 otherwise. \[ \text{operator>} =
\begin{cases} 1 & \text{if $x > y$} \\ 0 & \text{otherwise}
\end{cases} \]
Available since 2.0
int
operator>=
(int x, int y)
int
operator>=
(real x, real y)
Return 1 if x is greater than or equal to y and 0 otherwise. \[
\text{operator>=} = \begin{cases} 1 & \text{if $x \geq y$} \\ 0 &
\text{otherwise} \end{cases} \]
Available since 2.0
int
operator==
(int x, int y)
int
operator==
(real x, real y)
Return 1 if x is equal to y and 0 otherwise. \[ \text{operator==}(x,y)
= \begin{cases} 1 & \text{if $x = y$} \\ 0 & \text{otherwise}
\end{cases} \]
Available since 2.0
int
operator!=
(int x, int y)
int
operator!=
(real x, real y)
Return 1 if x is not equal to y and 0 otherwise. \[
\text{operator!=}(x,y) = \begin{cases} 1 & \text{if $x \neq y$} \\ 0 &
\text{otherwise} \end{cases} \]
Available since 2.0
3.5.2 Boolean operators
Boolean operators return either 0 for false or 1 for true. Inputs may
be any real or integer values, with non-zero values being treated as
true and zero values treated as false. These operators have the usual
precedences, with negation (not) binding the most tightly, conjunction
the next and disjunction the weakest; all of the operators bind more
tightly than the comparisons. Thus an expression such as !a && b
is
interpreted as (!a) && b
, and a < b || c >= d && e != f
as (a < b) || (((c >= d) && (e != f)))
.
int
operator!
(int x)
Return 1 if x is zero and 0 otherwise. \[ \text{operator!}(x) =
\begin{cases} 0 & \text{if $x \neq 0$} \\ 1 & \text{if $x = 0$}
\end{cases} \]
Available since 2.0
int
operator!
(real x)
Return 1 if x is zero and 0 otherwise. \[ \text{operator!}(x) =
\begin{cases} 0 & \text{if $x \neq 0.0$} \\ 1 & \text{if $x = 0.0$}
\end{cases} \]
deprecated; - use operator==
instead.
Available since 2.0, deprecated in 2.31
int
operator&&
(int x, int y)
Return 1 if x is unequal to 0 and y is unequal to 0. \[
\mathrm{operator\&\&}(x,y) = \begin{cases} 1 & \text{if $x \neq 0$}
\text{ and } y \neq 0\\ 0 & \text{otherwise} \end{cases} \]
Available since 2.0
int
operator&&
(real x, real y)
Return 1 if x is unequal to 0.0 and y is unequal to 0.0. \[
\mathrm{operator\&\&}(x,y) = \begin{cases} 1 & \text{if $x \neq 0.0$}
\text{ and } y \neq 0.0\\ 0 & \text{otherwise} \end{cases} \]
deprecated
Available since 2.0, deprecated in 2.31
int
operator||
(int x, int y)
Return 1 if x is unequal to 0 or y is unequal to 0. \[
\text{operator||}(x,y) = \begin{cases} 1 & \text{if $x \neq 0$}
\textrm{ or } y \neq 0\\ 0 & \text{otherwise} \end{cases} \]
Available since 2.0
int
operator||
(real x, real y)
Return 1 if x is unequal to 0.0 or y is unequal to 0.0. \[
\text{operator||}(x,y) = \begin{cases} 1 & \text{if $x \neq 0.0$}
\textrm{ or } y \neq 0.0\\ 0 & \text{otherwise} \end{cases} \]
deprecated
Available since 2.0, deprecated in 2.31
3.5.2.1 Boolean operator short circuiting
Like in C++, the boolean operators &&
and ||
and are implemented
to short circuit directly to a return value after evaluating the first
argument if it is sufficient to resolve the result. In evaluating a || b
, if a
evaluates to a value other than zero, the expression
returns the value 1 without evaluating the expression b
. Similarly,
evaluating a && b
first evaluates a
, and if the result is zero,
returns 0 without evaluating b
.
3.5.3 Logical functions
The logical functions introduce conditional behavior functionally and are primarily provided for compatibility with BUGS and JAGS.
real
step
(real x)
Return 1 if x is positive and 0 otherwise. \[ \text{step}(x) =
\begin{cases} 0 & \text{if } x < 0 \\ 1 & \text{otherwise} \end{cases}
\] Warning: int_step(0)
and int_step(NaN)
return 0 whereas
step(0)
and step(NaN)
return 1.
The step function is often used in BUGS to perform conditional
operations. For instance, step(a-b)
evaluates to 1 if a
is
greater than b
and evaluates to 0 otherwise. step
is a step-like
functions; see the warning in section step functions applied to
expressions dependent on parameters.
Available since 2.0
int
is_inf
(real x)
Return 1 if x is infinite (positive or negative) and 0 otherwise.
Available since 2.5
int
is_nan
(real x)
Return 1 if x is NaN and 0 otherwise.
Available since 2.5
Care must be taken because both of these indicator functions are step-like and thus can cause discontinuities in gradients when applied to parameters; see section step-like functions for details.