forked from sammy-tri/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap_to.h
33 lines (27 loc) · 1.01 KB
/
wrap_to.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#pragma once
#include <cmath>
#include "drake/common/double_overloads.h"
#include "drake/common/drake_assert.h"
namespace drake {
namespace math {
/// For variables that are meant to be periodic, (e.g. over a 2π interval),
/// wraps `value` into the interval `[low, high)`. Precisely, `wrap_to`
/// returns:
/// value + k*(high-low)
/// for the unique integer value `k` that lands the output in the desired
/// interval. @p low and @p high must be finite, and low < high.
///
template <class T1, class T2>
T1 wrap_to(const T1& value, const T2& low, const T2& high) {
DRAKE_ASSERT(low < high);
const T2 range = high - low;
return value - range * floor((value - low) / range);
// TODO(russt): jwnimmer preferred the following implementation (which may be
// numerically better), but fmod is not supported yet by autodiff nor
// symbolic:
// using std::fmod;
// const T1 rem = fmod(value - low, high - low);
// return if_then_else(rem >= T1(0), low + rem, high + rem);
}
} // namespace math
} // namespace drake