Skip to content

Commit dd1da17

Browse files
committed
Fix precision-losing type conversion
1 parent f407f74 commit dd1da17

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -967,9 +967,10 @@ namespace
967967
{
968968
T *casted = static_cast<T *>(ptr);
969969
size_t flattenedExtent = std::accumulate(
970-
extent.begin(), extent.end(), 1, [](size_t left, size_t right) {
971-
return left * right;
972-
});
970+
extent.begin(),
971+
extent.end(),
972+
size_t(1),
973+
[](size_t left, size_t right) { return left * right; });
973974
std::fill_n(casted, flattenedExtent, T{});
974975
}
975976

@@ -1537,14 +1538,16 @@ std::shared_ptr<nlohmann::json> JSONIOHandlerImpl::obtainJsonContents(File file)
15371538
}
15381539
// read from file
15391540
auto fh = getFilehandle(file, Access::READ_ONLY);
1541+
auto &fh_with_precision =
1542+
*fh >> std::setprecision(std::numeric_limits<double>::digits10 + 1);
15401543
std::shared_ptr<nlohmann::json> res = std::make_shared<nlohmann::json>();
15411544
switch (m_fileFormat)
15421545
{
15431546
case FileFormat::Json:
1544-
*fh >> *res;
1547+
fh_with_precision >> *res;
15451548
break;
15461549
case FileFormat::Toml:
1547-
*res = openPMD::json::tomlToJson(toml::parse(*fh, *file));
1550+
*res = openPMD::json::tomlToJson(toml::parse(fh_with_precision, *file));
15481551
break;
15491552
}
15501553
VERIFY(fh->good(), "[JSON] Failed reading from a file.");

test/SerialIOTest.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,9 +1240,9 @@ inline void dtype_test(
12401240
std::optional<std::string> activateTemplateMode = {})
12411241
{
12421242
bool test_long_double =
1243-
(backend != "json" && backend != "toml") || sizeof(long double) <= 8;
1243+
(backend != "json" || sizeof(long double) <= 8) && backend != "toml";
12441244
bool test_long_long =
1245-
(backend != "json" && backend != "toml") || sizeof(long long) <= 8;
1245+
(backend != "json" || sizeof(long long) <= 8) && backend != "toml";
12461246
{
12471247
Series s = activateTemplateMode.has_value()
12481248
? Series(
@@ -1437,7 +1437,10 @@ inline void dtype_test(
14371437
REQUIRE(s.getAttribute("short").dtype == Datatype::SHORT);
14381438
REQUIRE(s.getAttribute("int").dtype == Datatype::INT);
14391439
REQUIRE(s.getAttribute("long").dtype == Datatype::LONG);
1440-
REQUIRE(s.getAttribute("longlong").dtype == Datatype::LONGLONG);
1440+
if (test_long_long)
1441+
{
1442+
REQUIRE(s.getAttribute("longlong").dtype == Datatype::LONGLONG);
1443+
}
14411444
REQUIRE(s.getAttribute("ushort").dtype == Datatype::USHORT);
14421445
REQUIRE(s.getAttribute("uint").dtype == Datatype::UINT);
14431446
REQUIRE(s.getAttribute("ulong").dtype == Datatype::ULONG);
@@ -1449,7 +1452,10 @@ inline void dtype_test(
14491452
REQUIRE(s.getAttribute("vecShort").dtype == Datatype::VEC_SHORT);
14501453
REQUIRE(s.getAttribute("vecInt").dtype == Datatype::VEC_INT);
14511454
REQUIRE(s.getAttribute("vecLong").dtype == Datatype::VEC_LONG);
1452-
REQUIRE(s.getAttribute("vecLongLong").dtype == Datatype::VEC_LONGLONG);
1455+
if(test_long_long)
1456+
{
1457+
REQUIRE(s.getAttribute("vecLongLong").dtype == Datatype::VEC_LONGLONG);
1458+
}
14531459
REQUIRE(s.getAttribute("vecUShort").dtype == Datatype::VEC_USHORT);
14541460
REQUIRE(s.getAttribute("vecUInt").dtype == Datatype::VEC_UINT);
14551461
REQUIRE(s.getAttribute("vecULong").dtype == Datatype::VEC_ULONG);

0 commit comments

Comments
 (0)