[wpimath] C++: Assign zero in MakeWhiteNoiseVector if std-dev is zero (#2773)

A std-dev of zero is UB in C++. Java does not have this issue.

Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
Prateek Machiraju
2020-10-08 00:59:34 -04:00
committed by GitHub
parent 9058fe803d
commit a3e672f863

View File

@@ -220,8 +220,14 @@ Eigen::Matrix<double, N, 1> MakeWhiteNoiseVector(
Eigen::Matrix<double, N, 1> result;
for (int i = 0; i < N; ++i) {
std::normal_distribution<> distr{0.0, stdDevs[i]};
result(i) = distr(gen);
// Passing a standard deviation of 0.0 to std::normal_distribution is
// undefined behavior
if (stdDevs[i] == 0.0) {
result(i) = 0.0;
} else {
std::normal_distribution distr{0.0, stdDevs[i]};
result(i) = distr(gen);
}
}
return result;
}