Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement scientific notation for double-to-string
Also fixes tests for 1e5 expecting fixed precision rather than scientific notation (stringstream gives scientific notation indeed) The only still failing test now is the one having less than 16 significant digits of precision, due to the boost bug: https://svn.boost.org/trac/boost/ticket/7785
- Loading branch information
Sandro Santilli
committed
Dec 17, 2012
1 parent
19522ac
commit 17dd295
Showing
2 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include <boost/version.hpp> | ||
#include <mapnik/util/conversions.hpp> | ||
#include <boost/detail/lightweight_test.hpp> | ||
#include <iostream> | ||
|
||
int main( int, char*[] ) | ||
{ | ||
using mapnik::util::to_string; | ||
|
||
try | ||
{ | ||
std::string out; | ||
|
||
// Test double | ||
to_string(out, double(0)); | ||
BOOST_TEST_EQ( out, "0" ); | ||
out.clear(); | ||
|
||
to_string(out, double(1)); | ||
BOOST_TEST_EQ( out, "1" ); | ||
out.clear(); | ||
|
||
to_string(out, double(-1)); | ||
BOOST_TEST_EQ( out, "-1" ); | ||
out.clear(); | ||
|
||
to_string(out, double(0.1)); | ||
BOOST_TEST_EQ( out, "0.1" ); | ||
out.clear(); | ||
|
||
to_string(out, double(-0.1)); | ||
BOOST_TEST_EQ( out, "-0.1" ); | ||
out.clear(); | ||
|
||
to_string(out, double(0.123)); | ||
BOOST_TEST_EQ( out, "0.123" ); | ||
out.clear(); | ||
|
||
to_string(out, double(-0.123)); | ||
BOOST_TEST_EQ( out, "-0.123" ); | ||
out.clear(); | ||
|
||
to_string(out, double(1e-06)); | ||
BOOST_TEST_EQ( out, "1e-06" ); | ||
out.clear(); | ||
|
||
to_string(out, double(-1e-06)); | ||
BOOST_TEST_EQ( out, "-1e-06" ); | ||
out.clear(); | ||
|
||
to_string(out, double(1e-05)); | ||
BOOST_TEST_EQ( out, "1e-05" ); | ||
out.clear(); | ||
|
||
to_string(out, double(-1e-05)); | ||
BOOST_TEST_EQ( out, "-1e-05" ); | ||
out.clear(); | ||
|
||
to_string(out, double(0.0001)); | ||
BOOST_TEST_EQ( out, "0.0001" ); | ||
out.clear(); | ||
|
||
to_string(out, double(-0.0001)); | ||
BOOST_TEST_EQ( out, "-0.0001" ); | ||
out.clear(); | ||
|
||
to_string(out, double(0.0001234567890123456)); | ||
BOOST_TEST_EQ( out, "0.0001234567890123456" ); | ||
out.clear(); | ||
|
||
to_string(out, double(-0.0001234567890123456)); | ||
BOOST_TEST_EQ( out, "-0.0001234567890123456" ); | ||
out.clear(); | ||
|
||
to_string(out, double(1000000000000000)); | ||
BOOST_TEST_EQ( out, "1000000000000000" ); | ||
out.clear(); | ||
|
||
to_string(out, double(-1000000000000000)); | ||
BOOST_TEST_EQ( out, "-1000000000000000" ); | ||
out.clear(); | ||
|
||
to_string(out, double(100000000000000.1)); | ||
BOOST_TEST_EQ( out, "100000000000000.1" ); | ||
out.clear(); | ||
|
||
to_string(out, double(1.00001)); | ||
BOOST_TEST_EQ( out, "1.00001" ); | ||
out.clear(); | ||
|
||
to_string(out, double(1234000000000000)); | ||
BOOST_TEST_EQ( out, "1234000000000000" ); | ||
out.clear(); | ||
|
||
to_string(out, double(1.234e+16)); | ||
BOOST_TEST_EQ( out, "1.234e+16" ); | ||
out.clear(); | ||
|
||
to_string(out, double(-1.234e+16)); | ||
BOOST_TEST_EQ( out, "-1.234e+16" ); | ||
out.clear(); | ||
|
||
// Test int | ||
|
||
to_string(out, int(2)); | ||
BOOST_TEST_EQ( out, "2" ); | ||
out.clear(); | ||
} | ||
catch (std::exception const & ex) | ||
{ | ||
std::clog << "C++ type conversions problem: " << ex.what() << "\n"; | ||
BOOST_TEST(false); | ||
} | ||
|
||
if (!::boost::detail::test_errors()) { | ||
std::clog << "C++ type conversions: \x1b[1;32m✓ \x1b[0m\n"; | ||
#if BOOST_VERSION >= 104600 | ||
::boost::detail::report_errors_remind().called_report_errors_function = true; | ||
#endif | ||
} else { | ||
return ::boost::report_errors(); | ||
} | ||
} |