Browse Source

Libeval: consistent formatting for NaN

On some platforms like MSVC, NaN prints as "-nan(ind)". This
is a bit needlessly ugly, so print "NaN" on all platforms
consistently.

This fixes a test failure on MSVC.
pull/13/head
John Beard 7 years ago
parent
commit
dd17f24c04
  1. 12
      common/libeval/numeric_evaluator.cpp
  2. 2
      qa/common/libeval/test_numeric_evaluator.cpp

12
common/libeval/numeric_evaluator.cpp

@ -104,7 +104,17 @@ void NUMERIC_EVALUATOR::parseOk()
void NUMERIC_EVALUATOR::parseSetResult( double val )
{
snprintf( m_token.token, m_token.OutLen, "%.10g", val );
if( std::isnan( val ) )
{
// Naively printing this with %g produces "nan" on some platforms
// and "-nan(ind)" on others (e.g. MSVC). So force a "standard" string.
snprintf( m_token.token, m_token.OutLen, "%s", "NaN" );
}
else
{
// Can be printed as a floating point
snprintf( m_token.token, m_token.OutLen, "%.10g", val );
}
}

2
qa/common/libeval/test_numeric_evaluator.cpp

@ -117,7 +117,7 @@ static const std::vector<EVAL_CASE> eval_cases_valid = {
{ "1.5", "1.5" },
{ "1,5", "1.5" },
// Semicolon is valid, but the result is NaN
{ "1;", "nan" },
{ "1;", "NaN" },
// With own unit
{ "1mm", "1" },
// Unit that's not the evaluator's unit

Loading…
Cancel
Save