From dd17f24c04f40865d23ea7f0537a7fec024d3ba4 Mon Sep 17 00:00:00 2001 From: John Beard Date: Mon, 8 Apr 2019 16:21:21 +0100 Subject: [PATCH] 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. --- common/libeval/numeric_evaluator.cpp | 12 +++++++++++- qa/common/libeval/test_numeric_evaluator.cpp | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/common/libeval/numeric_evaluator.cpp b/common/libeval/numeric_evaluator.cpp index 8f7be2ca8f..a70bf915d1 100644 --- a/common/libeval/numeric_evaluator.cpp +++ b/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 ); + } } diff --git a/qa/common/libeval/test_numeric_evaluator.cpp b/qa/common/libeval/test_numeric_evaluator.cpp index 3eeb9aef97..6128810377 100644 --- a/qa/common/libeval/test_numeric_evaluator.cpp +++ b/qa/common/libeval/test_numeric_evaluator.cpp @@ -117,7 +117,7 @@ static const std::vector 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