Skip to content

Commit

Permalink
Refs #8958 checks and outputs nan and inf consistently
Browse files Browse the repository at this point in the history
Nan and inf values are checked for and output in a constant format. (hopefully across platforms)
  • Loading branch information
keithnbrown committed Feb 13, 2014
1 parent 941feb5 commit 94bd6e9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
Expand Up @@ -31,9 +31,15 @@ namespace Mantid
void init();
/// Overwrites Algorithm method
void exec();
/// returns true if the value is NaN
bool checkIfNan(const double& value) const;
/// returns true if the value if + or - infinity
bool checkIfInfinite(const double& value) const;
/// print the appropriate value to file
void outputval (double val, std::ofstream & file, bool leadingSep = true);
///static reference to the logger class
static Kernel::Logger& g_log;

char m_sep;
API::MatrixWorkspace_const_sptr m_ws;
};

Expand Down
68 changes: 66 additions & 2 deletions Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp
Expand Up @@ -53,6 +53,7 @@ namespace Mantid
exts.push_back(".txt");
declareProperty(new FileProperty("Filename", "", FileProperty::Save, exts),
"The filename of the output ANSTO file.");
m_sep = '\t';
}

/**
Expand Down Expand Up @@ -80,16 +81,79 @@ namespace Mantid
}
const std::vector<double> & y1 = m_ws->readY(0);
const std::vector<double> & e1 = m_ws->readE(0);
char sep = '\t';
double qres = (X1[1]-X1[0])/X1[1];
g_log.information("Constant dq/q from file: " + boost::lexical_cast<std::string>(qres));
file << std::scientific;
for (size_t i = 0; i < xlength; ++i)
{
double dq = X1[i]*qres;
file << X1[i] << sep << y1[i] << sep << e1[i] << sep << dq << std::endl;
outputval(X1[i], file, false);
outputval(y1[i], file);
outputval(e1[i], file);
outputval(dq, file);
file << std::endl;
}
file.close();
}

void SaveANSTOAscii::outputval (double val, std::ofstream & file, bool leadingSep)
{
bool nancheck = checkIfNan(val);
bool infcheck = checkIfInfinite(val);
if (leadingSep)
{
if (!nancheck && !infcheck)
{
file << m_sep << val;
}
else if (nancheck)
{
//not a number - output nan
file << m_sep << "nan";
}
else if (infcheck)
{
//infinite - output 'inf'
file << m_sep << "inf";
}
else
{
//not valid, nan or inf - so output 'und'
file << m_sep << "und";
}
}
else
{
if (!nancheck && !infcheck)
{
file << val;
}
else if (nancheck)
{
//not a number - output nan
file << "nan";
}
else if (infcheck)
{
//infinite - output 'inf'
file << "inf";
}
else
{
//not valid, nan or inf - so output 'und'
file << "und";
}
}
}

bool SaveANSTOAscii::checkIfNan(const double& value) const
{
return (boost::math::isnan(value));
}

bool SaveANSTOAscii::checkIfInfinite(const double& value) const
{
return (std::abs(value) == std::numeric_limits<double>::infinity());
}
} // namespace DataHandling
} // namespace Mantid

0 comments on commit 94bd6e9

Please sign in to comment.