Skip to content

Commit

Permalink
Avoid to use std::to_string to pass floating point simulation parameters
Browse files Browse the repository at this point in the history
std::to_string truncates the values with 1e-6 precision, and is prone
to locale-related bug.

Fix robotology-legacy#245
  • Loading branch information
traversaro committed Sep 26, 2020
1 parent 0a3d865 commit 2f4ba4d
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions cpp/scenario/gazebo/src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <sdf/World.hh>

#include <cassert>
#include <sstream>

using namespace scenario::gazebo;

Expand Down Expand Up @@ -335,6 +336,27 @@ bool utils::renameSDFModel(sdf::Root& sdfRoot,
return true;
}

namespace scenario::gazebo::utils {
/**
* Convert a double to a string ignoring the current locale.
*
* Furthermore, set it with precision 25 to ensure that the
* string can be converted back exactly to the double that
* generated it.
*/
std::string toExactStringNoLocale(const double in);
}


std::string utils::toExactStringNoLocale(const double in)
{
std::ostringstream ss;
ss.imbue(std::locale::classic());
ss << std::setprecision(25) << in;
return ss.str();
}


bool utils::updateSDFPhysics(sdf::Root& sdfRoot,
const double maxStepSize,
const double rtf,
Expand Down Expand Up @@ -372,16 +394,16 @@ bool utils::updateSDFPhysics(sdf::Root& sdfRoot,
assert(physicsElement);

sdf::ElementPtr max_step_size = physicsElement->GetElement("max_step_size");
max_step_size->AddValue("double", std::to_string(maxStepSize), true);
max_step_size->AddValue("double", toExactStringNoLocale(maxStepSize), true);

sdf::ElementPtr real_time_update_rate =
physicsElement->GetElement("real_time_update_rate");
real_time_update_rate->AddValue(
"double", std::to_string(realTimeUpdateRate), true);
"double", toExactStringNoLocale(realTimeUpdateRate), true);

sdf::ElementPtr real_time_factor =
physicsElement->GetElement("real_time_factor");
real_time_factor->AddValue("double", std::to_string(rtf), true);
real_time_factor->AddValue("double", toExactStringNoLocale(rtf), true);

return true;
}
Expand Down

0 comments on commit 2f4ba4d

Please sign in to comment.