Skip to content

Commit 85a426e

Browse files
committed
Fix overflow at project::operator()
1 parent 32020bd commit 85a426e

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

include/mapbox/geojsonvt/convert.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ struct project {
2323
vt_point operator()(const geometry::point<double>& p) {
2424
const double sine = std::sin(p.y * M_PI / 180);
2525
const double x = p.x / 360 + 0.5;
26-
const double y =
27-
std::max(std::min(0.5 - 0.25 * std::log((1 + sine) / (1 - sine)) / M_PI, 1.0), 0.0);
26+
27+
double oneMinusSine = 1.0 - sine;
28+
const double epsilon = 0.0000001;
29+
if (oneMinusSine < epsilon) { // Avoid overflow.
30+
oneMinusSine = epsilon;
31+
}
32+
const double y = std::max(std::min(0.5 - 0.25 * std::log((1 + sine) / oneMinusSine) / M_PI, 1.0), 0.0);
33+
2834
return { x, y, 0.0 };
2935
}
3036

0 commit comments

Comments
 (0)