Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions TestConcoreHpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>{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
Expand Down Expand Up @@ -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();

Expand Down
11 changes: 11 additions & 0 deletions TestConcoreZmq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>{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);
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions concore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,8 @@ class Concore{
val.insert(val.begin(),simtime+delta);
outfile<<'[';
for(int 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])<<']';
outfile.close();
// simtime must not be mutated here (issue #385).
note_write(delta);
Expand Down Expand Up @@ -851,8 +851,8 @@ class Concore{
val.insert(val.begin(),simtime+delta);
outfile<<'[';
for(int 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_SIZE) {
throw std::runtime_error(
Expand Down
16 changes: 15 additions & 1 deletion concore_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <cstring>
#include <stdexcept>
#include <cctype>
#include <cstdlib>
#include <iomanip>

#ifdef CONCORE_USE_ZMQ
#include <zmq.hpp>
Expand All @@ -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
// ===================================================================
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions concoredocker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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()
Expand Down
Loading