When saving XMLs, don't round floats that are bigger than INT_MAX. Fixes #1278
PiperOrigin-RevId: 590692020 Change-Id: Icd11d20f47bc4163eb4378f4cb031b8fd9d13612
This commit is contained in:
committed by
Copybara-Service
parent
0d37670698
commit
5b2c98f8f1
+3
-1
@@ -14,6 +14,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
@@ -1004,7 +1005,8 @@ void mjXUtil::WriteAttr(XMLElement* elem, string name, int n, const T* data, con
|
||||
}
|
||||
|
||||
// append number
|
||||
if (isint(data[i])) {
|
||||
double doubledata = static_cast<double>(data[i]);
|
||||
if (doubledata < INT_MAX && doubledata > -INT_MAX && isint(data[i])) {
|
||||
stream << Round(data[i]);
|
||||
} else {
|
||||
stream << data[i];
|
||||
|
||||
@@ -1178,5 +1178,29 @@ TEST_F(DecompilerTest, DoesntSaveInferredStatitics) {
|
||||
mj_deleteModel(model);
|
||||
}
|
||||
|
||||
TEST_F(DecompilerTest, VeryLargeNumbers) {
|
||||
static constexpr char xml[] = R"(
|
||||
<mujoco>
|
||||
<compiler angle="radian"/>
|
||||
<worldbody>
|
||||
<camera focal="16777217 1" sensorsize="1 1" resolution="100 100"/>
|
||||
<body pos="1e+20 0 0">
|
||||
<geom size="1"/>
|
||||
<joint axis="1 0 0" range="-1e+10 1e+10"/>
|
||||
</body>
|
||||
</worldbody>
|
||||
</mujoco>
|
||||
)";
|
||||
std::array<char, 1024> error;
|
||||
mjModel* model = LoadModelFromString(xml, error.data(), error.size());
|
||||
ASSERT_THAT(model, NotNull()) << error.data();
|
||||
std::string saved_xml = SaveAndReadXml(model);
|
||||
// note, focal is float and loses precision 16777217 -> 16777216
|
||||
EXPECT_THAT(saved_xml, HasSubstr("focal=\"16777216 1\""));
|
||||
EXPECT_THAT(saved_xml, HasSubstr("pos=\"1e+20 0 0\""));
|
||||
EXPECT_THAT(saved_xml, HasSubstr("range=\"-1e+10 1e+10\""));
|
||||
mj_deleteModel(model);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace mujoco
|
||||
|
||||
Reference in New Issue
Block a user