Skip to content

Commit

Permalink
Fix WKTWriter for small precisions and with trim enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Nov 20, 2024
1 parent 9687512 commit dac7f97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/io/WKTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,10 @@ WKTWriter::writeTrimmedNumber(double d, uint32_t precision, char* buf)
// most real-world coordinates, use positional notation
if ( (precision < 4) && (da < 1.0) ) {
// adjust precision to avoid rounding to zero
precision = static_cast<std::uint32_t>(-floor(log10(da)));
const auto higher_prec = static_cast<std::uint32_t>(-floor(log10(da)));
if (higher_prec > precision) {
precision = higher_prec;
}
}
return geos_d2sfixed_buffered_n(d, precision, buf);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/capi/GEOS_printDoubleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ void object::test<1>()
TESTCASE(12, 1.2345678901234e+17, "1.234567890123e+17"),
TESTCASE(13, 1.2345678901234e+17, "1.2345678901234e+17"),
TESTCASE(14, 1.2345678901234e+17, "1.2345678901234e+17"),
TESTCASE(0, 0.0123456789, "0.01"),
TESTCASE(1, 0.0123456789, "0.01"),
TESTCASE(2, 0.0123456789, "0.01"),
TESTCASE(3, 0.0123456789, "0.012"),
TESTCASE(4, 0.0123456789, "0.0123"),
TESTCASE(0, 0.123456789, "0.1"),
TESTCASE(1, 0.123456789, "0.1"),
TESTCASE(2, 0.123456789, "0.12"),
TESTCASE(3, 0.123456789, "0.123"),
TESTCASE(4, 0.123456789, "0.1235"),
TESTCASE(0, 1.23456789, "1"),
TESTCASE(1, 1.23456789, "1.2"),
TESTCASE(2, 1.23456789, "1.23"),
TESTCASE(3, 1.23456789, "1.235"),
TESTCASE(4, 1.23456789, "1.2346"),
};
for (const auto& testcase : testcase_l) {
char buf[28];
Expand Down

0 comments on commit dac7f97

Please sign in to comment.