diff --git a/TestConcoreHpp.cpp b/TestConcoreHpp.cpp index 72084be..a2cde98 100644 --- a/TestConcoreHpp.cpp +++ b/TestConcoreHpp.cpp @@ -134,6 +134,24 @@ static void test_write_FM_creates_file() { rm("out1/w_out"); } +static void test_write_FM_keeps_full_precision() { + setup_dirs(); + + Concore c; + c.delay = 0; + c.simtime = 2.0; + + c.write_FM(1, "w_prec", vector{3.141592653589793, 0.1, 1234567.0}); + + std::ifstream f("out1/w_prec"); + std::ostringstream ss; + ss << f.rdbuf(); + + check("write_FM keeps full precision", ss.str() == "[2,3.141592653589793,0.1,1234567]"); + + rm("out1/w_prec"); +} + // ------------- unchanged() ----------------------------------------------- // issue #608: write() leaves simtime alone, the next read moves it forward @@ -371,6 +389,7 @@ int main() { test_read_FM_missing_file_uses_initstr(); test_read_result_missing_file_status(); test_write_FM_creates_file(); + test_write_FM_keeps_full_precision(); test_write_moves_simtime_forward_on_next_read(); test_writes_in_one_step_advance_once(); diff --git a/TestConcoreZmq.cpp b/TestConcoreZmq.cpp index 094c82d..1452fd0 100644 --- a/TestConcoreZmq.cpp +++ b/TestConcoreZmq.cpp @@ -95,6 +95,16 @@ static void test_write_zmq_fixture_cases() { // Same scenario as the Python test for issue #598: the reply to the first // request never comes, the REQ port must still be able to send again. +static void test_write_zmq_keeps_full_precision() { + RepPeer peer("tcp://127.0.0.1:5594"); + Concore c; + c.init_zmq_port("p", "connect", "tcp://127.0.0.1:5594", "REQ"); + c.simtime = 2; + c.write("p", "data", std::vector{3.141592653589793, 0.1, 1234567.0}, 0); + check("list payload keeps full precision", + peer.recv() == "[2, 3.141592653589793, 0.1, 1234567]"); +} + static void test_req_recovers_after_missing_reply() { RepPeer peer("tcp://127.0.0.1:5592"); concore_base::ZeroMQPort req("connect", "tcp://127.0.0.1:5592", ZMQ_REQ); @@ -117,6 +127,7 @@ static void test_req_recovers_after_missing_reply() { int main() { test_round_trip(); test_write_zmq_fixture_cases(); + test_write_zmq_keeps_full_precision(); test_req_recovers_after_missing_reply(); std::cout << "\n" << passed << " passed, " << failed << " failed" << std::endl; return failed ? 1 : 0; diff --git a/concore.hpp b/concore.hpp index 6f2d7b7..d045d9f 100644 --- a/concore.hpp +++ b/concore.hpp @@ -788,8 +788,8 @@ class Concore{ val.insert(val.begin(),simtime+delta); outfile<<'['; for(int i=0;i= SHM_SIZE) { throw std::runtime_error( diff --git a/concore_base.hpp b/concore_base.hpp index 57a29fc..c12ef2b 100644 --- a/concore_base.hpp +++ b/concore_base.hpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include #ifdef CONCORE_USE_ZMQ #include @@ -41,6 +43,18 @@ inline std::string stripquotes(const std::string& str) { return str; } +// Shortest form that reads back as the same double, like Python's repr. +// A plain << only keeps 6 digits (3.141592653589793 -> 3.14159). +inline std::string format_double(double v) { + std::ostringstream ss; + for (int prec = 15; prec <= 17; ++prec) { + ss.str(""); + ss << std::setprecision(prec) << v; + if (std::strtod(ss.str().c_str(), nullptr) == v) break; + } + return ss.str(); +} + // =================================================================== // Parsing Utilities // =================================================================== @@ -452,7 +466,7 @@ class ZeroMQPort { ss << "["; for (size_t i = 0; i < payload.size(); ++i) { if (i) ss << ", "; - ss << payload[i]; + ss << format_double(payload[i]); } ss << "]"; std::string msg = ss.str(); diff --git a/concoredocker.hpp b/concoredocker.hpp index fc1ddb6..6331172 100644 --- a/concoredocker.hpp +++ b/concoredocker.hpp @@ -586,9 +586,9 @@ class Concore { return; } if (!val.empty()) { - outfile << "[" << simtime + delta << ", "; + outfile << "[" << concore_base::format_double(simtime + delta) << ", "; for (size_t i = 0; i < val.size(); ++i) { - outfile << val[i] << (i + 1 < val.size() ? ", " : ""); + outfile << concore_base::format_double(val[i]) << (i + 1 < val.size() ? ", " : ""); } outfile << "]"; // simtime must not be mutated here (issue #385). @@ -607,8 +607,8 @@ class Concore { std::ostringstream outfile; outfile << '['; for (size_t i = 0; i < val.size() - 1; i++) - outfile << val[i] << ','; - outfile << val[val.size() - 1] << ']'; + outfile << concore_base::format_double(val[i]) << ','; + outfile << concore_base::format_double(val[val.size() - 1]) << ']'; std::string result = outfile.str(); if (result.size() > SHM_PAYLOAD_MAX) { std::cerr << "ERROR: write_SM payload (" << result.size()