Skip to content

Commit

Permalink
ENH: WriteParameterFile write floating points without rounding errors
Browse files Browse the repository at this point in the history
For numeric parameter values, `ParameterObject::WriteParameterFile` did an unnecessary conversion from string to `float` and back, before writing the value to file, which potentially introduced rounding errors, as well as overly verbose string representations of floating point numbers. These are avoided now, by internally using the lossless `Conversion::ParameterMapToString` helper function.
  • Loading branch information
N-Dekker committed Jul 12, 2023
1 parent 99f2eda commit 5c450b6
Showing 1 changed file with 2 additions and 25 deletions.
27 changes: 2 additions & 25 deletions Core/Main/elxParameterObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "elxParameterObject.h"
#include "elxConversion.h"

#include "itkParameterFileParser.h"

Expand Down Expand Up @@ -279,31 +280,7 @@ ParameterObject::WriteParameterFile(const ParameterMapType & parameterMap,

try
{
ParameterMapConstIterator parameterMapIterator = parameterMap.begin();
ParameterMapConstIterator parameterMapIteratorEnd = parameterMap.end();
while (parameterMapIterator != parameterMapIteratorEnd)
{
parameterFile << "(" << parameterMapIterator->first;

ParameterValueVectorType parameterMapValueVector = parameterMapIterator->second;
for (unsigned int i = 0; i < parameterMapValueVector.size(); ++i)
{
std::istringstream stream(parameterMapValueVector[i]);
float number;
stream >> number;
if (stream.fail() || stream.bad())
{
parameterFile << " \"" << parameterMapValueVector[i] << "\"";
}
else
{
parameterFile << " " << number;
}
}

parameterFile << ")" << std::endl;
++parameterMapIterator;
}
parameterFile << Conversion::ParameterMapToString(parameterMap);
}
catch (const std::ios_base::failure & e)
{
Expand Down

0 comments on commit 5c450b6

Please sign in to comment.