Skip to content

Commit 92a9261

Browse files
j0hnnybashalandefreitas
authored andcommitted
fix(util): make linspace(a,b,1) return a instead of NaN
This underlying issue caused polarhistograms with more than 89 bins to not be displayed at all. Previously passing n=1 to linspace resulted in a one element vector containing NaN. With this change it will result in a one element vector containing the first ("lower") bound. This is the natural solution given the current implementation and matches the behavior of numpy.linspace, unfortunately however it does not match the behavior of MATLAB linspace which returns the "upper" bound instead.
1 parent 62ef6b0 commit 92a9261

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

source/matplot/util/common.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ namespace matplot {
8686

8787
std::vector<double> linspace(double d1, double d2, size_t n) {
8888
std::vector<double> x(n);
89+
// avoid division by zero
90+
double step = n > 1? (d2 - d1) / static_cast<double>(n - 1) : 0.;
8991
for (size_t i = 0; i < x.size(); ++i) {
90-
x[i] = d1 + static_cast<double>(i) * (d2 - d1) /
91-
static_cast<double>(n - 1);
92+
x[i] = d1 + static_cast<double>(i) * step;
9293
}
9394
return x;
9495
}
@@ -1270,4 +1271,4 @@ namespace matplot {
12701271
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
12711272
}
12721273

1273-
} // namespace matplot
1274+
} // namespace matplot

0 commit comments

Comments
 (0)