Skip to content

Commit 4885e2c

Browse files
hjmjohnsonclaude
andcommitted
ENH: Use NumberToString for lossless float metadata in NRRD IO
_dump_metadata_to_stream<double> and <float> used default stream precision (6 significant digits), silently truncating user-defined float/double metadata stored in NRRD key/value fields. Add explicit template specializations for double and float that call itk::ConvertNumberToString() for shortest-round-trip serialization. Array<double> and Array<float> were already handled losslessly via the specialized operator<< in itkArrayOutputSpecialization.cxx. See: #3249 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 62952dd commit 4885e2c

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

Modules/IO/NRRD/src/itkNrrdImageIO.cxx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#include "itkIOCommon.h"
2424
#include "itkFloatingPointExceptions.h"
2525
#include "itkNumericLocale.h"
26+
#include "itkNumberToString.h"
2627

2728
#include <sstream>
29+
#include <type_traits>
2830

2931
namespace
3032
{
@@ -1053,7 +1055,14 @@ _dump_metadata_to_stream(MetaDataDictionary & thisDic, const std::string & key,
10531055
T value;
10541056
if (ExposeMetaData<T>(thisDic, key, value))
10551057
{
1056-
buffer << value;
1058+
if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double>)
1059+
{
1060+
buffer << ConvertNumberToString(value);
1061+
}
1062+
else
1063+
{
1064+
buffer << value;
1065+
}
10571066
return true;
10581067
}
10591068
return false;

0 commit comments

Comments
 (0)